RSA 加解密

        #region RSA
        public static byte[] GetBytes(String num)
        {
            BigInteger n = new BigInteger(num, 10);
            String s = n.ToString(2);
            if (s.Length % 8 > 0)
            {
                s = new String('0', 8 - s.Length % 8) + s;
            }
            byte[] data = new byte[s.Length / 8];
            String ocetstr;
            for (int i = 0; i < data.Length; i++)
            {
                ocetstr = s.Substring(8 * i, 8);
                data[i] = Convert.ToByte(ocetstr, 2);
            }
            return data;
        }

        /// <summary> 
        /// 字节数组转16进制字符串 
        /// </summary> 
        /// <param name="bytes"></param> 
        /// <returns></returns> 
        public static String ConvByteArrayToHex(byte[] data)
        {
            String s = "";
            for (int i = 0; i < data.Length; i++)
            {
                s += Convert.ToString(data[i], 16);
            }
            return s.ToUpper();
        }
        public static string RSAEncrypt(string Modulus, string Exponent, string content)
        {
            var param = new RSAParameters();
            param.Exponent = GetBytes(Exponent);
            param.Modulus = GetBytes(Modulus);
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.ImportParameters(param);

            int CellLength = new BigInteger(param.Modulus).bitCount() / 8;

            byte[] bInput = Encoding.UTF8.GetBytes(content);


            int MaxLength = CellLength - 11;
            int GroupLength = (int)Math.Ceiling(bInput.Length * 1.00 / MaxLength);

            byte[] cipherText = new byte[GroupLength * CellLength];
            for (int i = 0; i < GroupLength; i++)
            {
                int len = MaxLength < bInput.Length - MaxLength * i ? MaxLength
                        : bInput.Length - MaxLength * i;
                byte[] inByte = new byte[len];
                Buffer.BlockCopy(bInput, MaxLength * i, inByte, 0, len);

                byte[] temp = rsa.Encrypt(inByte, false);
                Buffer.BlockCopy(temp, 0, cipherText, i * CellLength, CellLength);
            }
            //cipherText = rsa.Encrypt(bInput, false);
            //return Convert.ToBase64String(cipherText);
            //return toHex(cipherText);
            return BitConverter.ToString(cipherText).Replace("-", "");

        }


        #endregion

 

转载于:https://www.cnblogs.com/sdwdjzhy/p/4173292.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值