WEB开发网
开发学院软件开发C语言 C# 基于大整数类的RSA算法实现(公钥加密解密,私... 阅读

C# 基于大整数类的RSA算法实现(公钥加密解密,私钥加密解密)

 2009-04-27 08:27:42 来源:WEB开发网   
核心提示: 三、算法实现加密/// <summary> /// 加密字符串 /// </summary> /// <param name="dataStr">待加密字符串</param> /// <param name=&quo

三、算法实现

加密

/// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="dataStr">待加密字符串</param>
        /// <param name="keyNmu">密钥大素数</param>
        /// <param name="nNum">大整数N</param>
        /// <returns>加密结果</returns>
        private byte[] EncryptString(string dataStr, BigInteger keyNum, BigInteger nNum)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(dataStr);
            int len = bytes.Length;
            int len1 = 0;
            int blockLen = 0;
            if ((len % 120) == 0)
                len1 = len / 120;
            else
                len1 = len / 120 + 1;
            List<byte> tempbytes = new List<byte>();
            for (int i = 0; i < len1; i++)
            {
                if (len >= 120)
                {
                    blockLen = 120;
                }
                else
                {
                    blockLen = len;
                }
                byte[] oText = new byte[blockLen];
                Array.Copy(bytes, i * 120, oText, 0, blockLen);
                string res = Encoding.UTF8.GetString(oText);
                BigInteger biText = new BigInteger(oText);
                BigInteger biEnText = biText.modPow(keyNum, nNum);
                //补位
                byte[] testbyte = null;
                string resultStr = biEnText.ToHexString();
                if (resultStr.Length < 256)
                {
                    while (resultStr.Length != 256)
                    {
                        resultStr = "0" + resultStr;
                    }
                }
                byte[] returnBytes = new byte[128];
                for (int j = 0; j < returnBytes.Length; j++)
                    returnBytes[j] = Convert.ToByte(resultStr.Substring(j * 2, 2), 16);
                tempbytes.AddRange(returnBytes);
                len -= blockLen;
            }
            return tempbytes.ToArray();
        }

上一页  2 3 4 5 6 7 8  下一页

Tags:基于 整数 RSA

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接