非对称加密及数字签名RSA算法的实现(公钥加密->私钥解密、私钥加密->公钥解密)

来源:互联网 发布:it有哪些职业 编辑:程序博客网 时间:2024/05/17 03:41

RSA算法是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。它的安全性是基于大整数素因子分解的困难性,而大整数因子分解问题是数学上的著名难题,至今没有有效的方法予以解决,因此可以确保RSA算法的安全性。

 

  RSA算法实现主要分为三部分:包括公钥和私钥的产生,非对称加密和解密,数字签名和验证,下面将逐个介绍RSA算法的工作原理及我的实现方法。

 

 

    1.公钥和私钥的产生

    随意选择两个大素数p、q,p不等于q,计算n = p * q。 
    随机选择一个整数e,满足e和( p – 1 ) * ( q – 1 )互质。(注:e很容易选择,如3, 17, 65537等都可以。.NET Framework中e默认选择的就是65537)
利用Euclid算法计算解密密钥d,满足
      e * d ≡ 1 ( mod ( p - 1 ) * ( q - 1 ) ) 
    其中n和d也要互质。

    其中e和n就是公钥,d和n就是私钥。P、q销毁。

 

    在.NET Framework的RSA算法中,e对应RSAParameters.Exponent;d对应RSAParameters.D;n对应RSAParameters.ModulusExponent。.NET Framework中的RSA算法默认使用1024位长的密钥。公钥和私钥是利用.NET Framework的RSACryptoServiceProvider生成公钥xml文件和私钥xml文件来实现的。生成公钥和私钥xml文件的程序。

 

  1. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
  2.   
  3.     //生成公钥XML字符串  
  4.     string publicKeyXmlString = rsa.ToXmlString(false);  
  5.   
  6.     //生成私钥XML字符串  
  7.     string privateKeyXmlString = rsa.ToXmlString(true);  

 

公钥和私钥将从生成的公钥xml文件和私钥xml文件中导入。

 

  1. public class RSAPublicKey  
  2.     {  
  3.         public byte[] Modulus;  
  4.         public byte[] Exponent;  
  5.   
  6.         public static RSAPublicKey FromXmlString(string xmlString)  
  7.         {  
  8.             if (string.IsNullOrEmpty(xmlString))  
  9.             {  
  10.                 return null;  
  11.             }  
  12.   
  13.             try  
  14.             {  
  15.                 using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))  
  16.                 {  
  17.                     if (!reader.ReadToFollowing("RSAKeyValue"))  
  18.                     {  
  19.                         return null;  
  20.                     }  
  21.   
  22.                     if (reader.LocalName != "Modulus" && !reader.ReadToFollowing("Modulus"))  
  23.                     {  
  24.                         return null;  
  25.                     }  
  26.                     string modulus = reader.ReadElementContentAsString();  
  27.   
  28.                     if (reader.LocalName != "Exponent" && !reader.ReadToFollowing("Exponent"))  
  29.                     {  
  30.                         return null;  
  31.                     }  
  32.                     string exponent = reader.ReadElementContentAsString();  
  33.   
  34.                     RSAPublicKey publicKey = new RSAPublicKey();  
  35.                     publicKey.Modulus = Convert.FromBase64String(modulus);  
  36.                     publicKey.Exponent = Convert.FromBase64String(exponent);  
  37.   
  38.                     return publicKey;  
  39.                 }  
  40.             }  
  41.             catch  
  42.             {  
  43.                 return null;  
  44.             }  
  45.         }          
  46.     }  

 

  1. public class RSAPrivateKey  
  2.     {  
  3.         public byte[] Modulus;  
  4.         public byte[] D;  
  5.   
  6.         public static RSAPrivateKey FromXmlString(string xmlString)  
  7.         {  
  8.             if (string.IsNullOrEmpty(xmlString))  
  9.             {  
  10.                 return null;  
  11.             }  
  12.   
  13.             try  
  14.             {  
  15.                 using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))  
  16.                 {  
  17.                     if (!reader.ReadToFollowing("RSAKeyValue"))  
  18.                     {  
  19.                         return null;  
  20.                     }  
  21.   
  22.                     if (reader.LocalName != "Modulus" && !reader.ReadToFollowing("Modulus"))  
  23.                     {  
  24.                         return null;  
  25.                     }  
  26.                     string modulus = reader.ReadElementContentAsString();  
  27.   
  28.                     if (reader.LocalName != "D" && !reader.ReadToFollowing("D"))  
  29.                     {  
  30.                         return null;  
  31.                     }  
  32.                     string d = reader.ReadElementContentAsString();  
  33.   
  34.                     RSAPrivateKey privateKey = new RSAPrivateKey();  
  35.                     privateKey.Modulus = Convert.FromBase64String(modulus);  
  36.                     privateKey.D = Convert.FromBase64String(d);  
  37.   
  38.                     return privateKey;  
  39.                 }  
  40.             }  
  41.             catch  
  42.             {  
  43.                 return null;  
  44.             }  
  45.         }          
  46.     }  


2.非对称加密和解密
    私钥加密m(二进制表示)时,首先把m分成长s的数据块 m1, m2 ... mi,其中 2^s <= n, s 尽可能的大。执行如下计算:
        ci = mi ^ d (mod n)
    公钥解密c(二进制表示)时,也需要将c分成长s的数据块c1, c2 ... ci,执行如下计算:
        mi = ci ^ e (mod n)

 

    在某些情况下,也会使用公钥加密->私钥解密。原理和私钥加密->公钥解密一样。下面是私钥计算和公钥计算的算法。其中利用到了Chew Keong TAN的BigInteger类。.NET Framework 4中提供的BigInteger.ModPow方法好像有问题。 

  1. private static byte[] Compute(byte[] data, RSAPublicKey publicKey, int blockSize)  
  2.         {  
  3.             //  
  4.             // 公钥加密/解密公式为:ci = mi^e ( mod n )              
  5.             //   
  6.             // 先将 m(二进制表示)分成数据块 m1, m2, ..., mi ,然后进行运算。  
  7.             //  
  8.             BigInteger e = new BigInteger(publicKey.Exponent);  
  9.             BigInteger n = new BigInteger(publicKey.Modulus);  
  10.   
  11.             int blockOffset = 0;  
  12.             using (MemoryStream stream = new MemoryStream())  
  13.             {  
  14.                 while (blockOffset < data.Length)  
  15.                 {  
  16.                     int blockLen = Math.Min(blockSize, data.Length - blockOffset);  
  17.                     byte[] blockData = new byte[blockLen];  
  18.                     Buffer.BlockCopy(data, blockOffset, blockData, 0, blockLen);  
  19.   
  20.                     BigInteger mi = new BigInteger(blockData);  
  21.                     BigInteger ci = mi.modPow(e, n);//ci = mi^e ( mod n )  
  22.   
  23.                     byte[] block = ci.getBytes();  
  24.                     stream.Write(block, 0, block.Length);  
  25.                     blockOffset += blockLen;  
  26.                 }  
  27.   
  28.                 return stream.ToArray();  
  29.             }  
  30.         }  
  31.   
  32.         private static byte[] Compute(byte[] data, RSAPrivateKey privateKey, int blockSize)  
  33.         {  
  34.             //  
  35.             // 私钥加密/解密公式为:mi = ci^d ( mod n )  
  36.             //   
  37.             // 先将 c(二进制表示)分成数据块 c1, c2, ..., ci ,然后进行运算。              
  38.             //  
  39.             BigInteger d = new BigInteger(privateKey.D);  
  40.             BigInteger n = new BigInteger(privateKey.Modulus);  
  41.   
  42.             int blockOffset = 0;  
  43.   
  44.             using (MemoryStream stream = new MemoryStream())  
  45.             {  
  46.                 while (blockOffset < data.Length)  
  47.                 {  
  48.                     int blockLen = Math.Min(blockSize, data.Length - blockOffset);  
  49.                     byte[] blockData = new byte[blockLen];  
  50.                     Buffer.BlockCopy(data, blockOffset, blockData, 0, blockLen);  
  51.   
  52.                     BigInteger ci = new BigInteger(blockData);  
  53.                     BigInteger mi = ci.modPow(d, n);//mi = ci^d ( mod n )  
  54.   
  55.                     byte[] block = mi.getBytes();  
  56.                     stream.Write(block, 0, block.Length);  
  57.                     blockOffset += blockLen;  
  58.                 }  
  59.   
  60.                 return stream.ToArray();  
  61.             }  
  62.         }  


下面是私钥加密->公钥解密的实现:

 

  1. public static byte[] Encrypt(byte[] data, RSAPublicKey publicKey)  
  2.         {  
  3.             if (data == null)  
  4.             {  
  5.                 throw new ArgumentNullException("data");  
  6.             }  
  7.   
  8.             if (publicKey == null)  
  9.             {  
  10.                 throw new ArgumentNullException("publicKey");  
  11.             }  
  12.   
  13.             int blockSize = publicKey.Modulus.Length - 1;  
  14.             return Compute(data, publicKey, blockSize);  
  15.         }  
  16.   
  17.         public static byte[] Decrypt(byte[] data, RSAPrivateKey privateKey)  
  18.         {  
  19.             if (data == null)  
  20.             {  
  21.                 throw new ArgumentNullException("data");  
  22.             }  
  23.   
  24.             if (privateKey == null)  
  25.             {  
  26.                 throw new ArgumentNullException("privateKey");  
  27.             }  
  28.   
  29.             int blockSize = privateKey.Modulus.Length;  
  30.             return Compute(data, privateKey, blockSize);  
  31.         }  


下面是公钥加密->私钥解密的实现:

 

 

  1. public static byte[] Encrypt(byte[] data, RSAPrivateKey privateKey)  
  2.         {  
  3.             if (data == null)  
  4.             {  
  5.                 throw new ArgumentNullException("data");  
  6.             }  
  7.   
  8.             if (privateKey == null)  
  9.             {  
  10.                 throw new ArgumentNullException("privateKey");  
  11.             }  
  12.   
  13.             int blockSize = privateKey.Modulus.Length - 1;  
  14.             return Compute(data, privateKey, blockSize);  
  15.         }  
  16.   
  17.         public static byte[] Decrypt(byte[] data, RSAPublicKey publicKey)  
  18.         {  
  19.             if (data == null)  
  20.             {  
  21.                 throw new ArgumentNullException("data");  
  22.             }  
  23.   
  24.             if (publicKey == null)  
  25.             {  
  26.                 throw new ArgumentNullException("publicKey");  
  27.             }  
  28.   
  29.             int blockSize = publicKey.Modulus.Length;  
  30.             return Compute(data, publicKey, blockSize);  
  31.         }  


3.数字签名和验证
    私钥签名数据m时,先对m进行hash计算,得到计算结果h。然后将h使用私钥加密,得到加密后的密文s即为签名。
    公钥验证签名s时,先将m进行hash计算,得到计算结果h。然后使用公钥解密s得到结果h’。如果h==h’即验证成功,否则验证失败。

 

    在某些情况下,也会使用公钥签名->私钥验证。原理和私钥签名->公钥验证一样。

    下面是私钥签名->公钥验证的实现。 

  1. public static byte[] Sign(byte[] data, RSAPublicKey publicKey, HashAlgorithm hash)  
  2.         {  
  3.             if (data == null)  
  4.             {  
  5.                 throw new ArgumentNullException("data");  
  6.             }  
  7.   
  8.             if (publicKey == null)  
  9.             {  
  10.                 throw new ArgumentNullException("publicKey");  
  11.             }  
  12.   
  13.             if (hash == null)  
  14.             {  
  15.                 throw new ArgumentNullException("hash");  
  16.             }  
  17.   
  18.             byte[] hashData = hash.ComputeHash(data);  
  19.             byte[] signature = Encrypt(hashData, publicKey);  
  20.             return signature;  
  21.         }  
  22.   
  23.         public static bool Verify(byte[] data, RSAPrivateKey privateKey, HashAlgorithm hash, byte[] signature)  
  24.         {  
  25.             if (data == null)  
  26.             {  
  27.                 throw new ArgumentNullException("data");  
  28.             }  
  29.   
  30.             if (privateKey == null)  
  31.             {  
  32.                 throw new ArgumentNullException("privateKey");  
  33.             }  
  34.   
  35.             if (hash == null)  
  36.             {  
  37.                 throw new ArgumentNullException("hash");  
  38.             }  
  39.   
  40.             if (signature == null)  
  41.             {  
  42.                 throw new ArgumentNullException("signature");  
  43.             }  
  44.   
  45.             byte[] hashData = hash.ComputeHash(data);  
  46.             byte[] signatureHashData = Decrypt(signature, privateKey);  
  47.   
  48.             if (signatureHashData != null && signatureHashData.Length == hashData.Length)  
  49.             {  
  50.                 for (int i = 0; i < signatureHashData.Length; i++)  
  51.                 {  
  52.                     if (signatureHashData[i] != hashData[i])  
  53.                     {  
  54.                         return false;  
  55.                     }  
  56.                 }  
  57.                 return true;  
  58.             }  
  59.   
  60.             return false;  
  61.         }  


下面是公钥签名->私钥验证的实现:

 

  1. public static byte[] Sign(byte[] data, RSAPrivateKey privateKey, HashAlgorithm hash)  
  2.         {  
  3.             if (data == null)  
  4.             {  
  5.                 throw new ArgumentNullException("data");  
  6.             }  
  7.   
  8.             if (privateKey == null)  
  9.             {  
  10.                 throw new ArgumentNullException("privateKey");  
  11.             }  
  12.   
  13.             if (hash == null)  
  14.             {  
  15.                 throw new ArgumentNullException("hash");  
  16.             }  
  17.   
  18.             byte[] hashData = hash.ComputeHash(data);  
  19.             byte[] signature = Encrypt(hashData, privateKey);  
  20.             return signature;  
  21.         }  
  22.   
  23.         public static bool Verify(byte[] data, RSAPublicKey publicKey, HashAlgorithm hash, byte[] signature)  
  24.         {  
  25.             if (data == null)  
  26.             {  
  27.                 throw new ArgumentNullException("data");  
  28.             }  
  29.   
  30.             if (publicKey == null)  
  31.             {  
  32.                 throw new ArgumentNullException("publicKey");  
  33.             }  
  34.   
  35.             if (hash == null)  
  36.             {  
  37.                 throw new ArgumentNullException("hash");  
  38.             }  
  39.   
  40.             if (signature == null)  
  41.             {  
  42.                 throw new ArgumentNullException("signature");  
  43.             }  
  44.   
  45.             byte[] hashData = hash.ComputeHash(data);  
  46.   
  47.             byte[] signatureHashData = Decrypt(signature, publicKey);  
  48.   
  49.             if (signatureHashData != null && signatureHashData.Length == hashData.Length)  
  50.             {  
  51.                 for (int i = 0; i < signatureHashData.Length; i++)  
  52.                 {  
  53.                     if (signatureHashData[i] != hashData[i])  
  54.                     {  
  55.                         return false;  
  56.                     }  
  57.                 }  
  58.                 return true;  
  59.             }  
  60.   
  61.             return false;  
  62.         }