C#使用RSA进行私钥加密公钥解密

RSACryptoServiceProvider,这个类提供了好用的加密方法,但是只提供了公钥加密,私钥解密,没有提供私钥加密,公钥解密的方法,没办法,只能在网上搜索,还是找到了一段代码,自己修改成想要的实现了。

大数类,BigInteger,这个自己在网上找吧。这里给出只提供私钥加密,公钥解密的自己写的类。

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Collections;

namespace Security
{
    public class RSAPrivateKeyCrypto : IDisposable
    {
        RSAParameters paramsters;
        public RSAPrivateKeyCrypto(RSAParameters paramsters)
        {
            this.paramsters = paramsters;
        }
        public byte[] Encrypt(byte[] source)
        {
            BigInteger d = new BigInteger(paramsters.D);
            BigInteger n = new BigInteger(paramsters.Modulus);
            int sug = 127;
            int len = source.Length;
            int cycle = 0;
            if ((len % sug) == 0) cycle = len / sug; else cycle = len / sug + 1;

            ArrayList temp = new ArrayList();
            int blockLen = 0;
            for (int i = 0; i < cycle; i++)
            {
                if (len >= sug) blockLen = sug; else blockLen = len;

                byte[] context = new byte[blockLen];
                int po = i * sug;
                Array.Copy(source, po, context, 0, blockLen);

                BigInteger biText = new BigInteger(context);
                BigInteger biEnText = biText.modPow(d, n);

                byte[] b = biEnText.getBytes();
                temp.AddRange(b);
                len -= blockLen;
            }
            return (byte[])temp.ToArray(typeof(byte));
        }
        public byte[] Decrypt(byte[] source)
        {
            BigInteger e = new BigInteger(paramsters.Exponent);
            BigInteger n = new BigInteger(paramsters.Modulus);

            int bk = 128;
            int len = source.Length;
            int cycle = 0;
            if ((len % bk) == 0) cycle = len / bk; else cycle = len / bk + 1;

            ArrayList temp = new ArrayList();
            int blockLen = 0;
            for (int i = 0; i < cycle; i++)
            {
                if (len >= bk) blockLen = bk; else blockLen = len;

                byte[] context = new byte[blockLen];
                int po = i * bk;
                Array.Copy(source, po, context, 0, blockLen);

                BigInteger biText = new BigInteger(context);
                BigInteger biEnText = biText.modPow(e, n);

                byte[] b = biEnText.getBytes();
                temp.AddRange(b);
                len -= blockLen;
            }
            return (byte[])temp.ToArray(typeof(byte));
        }

        #region IDisposable 成员

        public void Dispose()
        {

        }

        #endregion
    }
}
本条目发布于 2013年10月22日。属于 技术分类。 作者是minibear
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值