找回密码
 立即注册

QQ登录

只需一步,快速开始

graper

高级会员

45

主题

63

帖子

1348

积分

高级会员

积分
1348

活字格认证

graper
高级会员   /  发表于:2010-1-4 09:35  /   查看:8281  /  回复:4
RSA算法是第一个既能用于数据加密也能用于数字签名的算法。它易于理解和操作,也很流行。它的安全性是基于大整数素因子分解的困难性,而大整数因子分解问题是数学上的著名难题,至今没有有效的方法予以解决,因此可以确保RSA算法的安全性。

    到目前Silverlight4 Beta发布为止,Silverlight中仍然没有提供非对称加密及数字签名相关的算法。而.NET Framework中提供的RSA等算法,都是通过操作系统提供的相关API实现的,没法移植到Silverlight中使用。因此很难实现一个健壮点的Silverlight纯客户端的注册验证算法。这几天抽空写了个Silverlight下可用的RSA算法,使用非对称加密和数字签名使Silverlight纯客户端的注册验证算法健壮了不少。关于这个Silverlight下可用的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文件的程序本身不是Silverlight程序。

  1.     RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

  2.     //生成公钥XML字符串
  3.     string publicKeyXmlString = rsa.ToXmlString(false);

  4.     //生成私钥XML字符串
  5.     string privateKeyXmlString = rsa.ToXmlString(true);
复制代码
公钥和私钥将从生成的公钥xml文件和私钥xml文件中导入。

  1.     public class RSAPublicKey
  2.     {
  3.         public byte[] Modulus;
  4.         public byte[] Exponent;

  5.         public static RSAPublicKey FromXmlString(string xmlString)
  6.         {
  7.             if (string.IsNullOrEmpty(xmlString))
  8.             {
  9.                 return null;
  10.             }

  11.             try
  12.             {
  13.                 using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
  14.                 {
  15.                     if (!reader.ReadToFollowing("RSAKeyValue"))
  16.                     {
  17.                         return null;
  18.                     }

  19.                     if (reader.LocalName != "Modulus" && !reader.ReadToFollowing("Modulus"))
  20.                     {
  21.                         return null;
  22.                     }
  23.                     string modulus = reader.ReadElementContentAsString();

  24.                     if (reader.LocalName != "Exponent" && !reader.ReadToFollowing("Exponent"))
  25.                     {
  26.                         return null;
  27.                     }
  28.                     string exponent = reader.ReadElementContentAsString();

  29.                     RSAPublicKey publicKey = new RSAPublicKey();
  30.                     publicKey.Modulus = Convert.FromBase64String(modulus);
  31.                     publicKey.Exponent = Convert.FromBase64String(exponent);

  32.                     return publicKey;
  33.                 }
  34.             }
  35.             catch
  36.             {
  37.                 return null;
  38.             }
  39.         }        
  40.     }
复制代码

  1.     public class RSAPrivateKey
  2.     {
  3.         public byte[] Modulus;
  4.         public byte[] D;

  5.         public static RSAPrivateKey FromXmlString(string xmlString)
  6.         {
  7.             if (string.IsNullOrEmpty(xmlString))
  8.             {
  9.                 return null;
  10.             }

  11.             try
  12.             {
  13.                 using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
  14.                 {
  15.                     if (!reader.ReadToFollowing("RSAKeyValue"))
  16.                     {
  17.                         return null;
  18.                     }

  19.                     if (reader.LocalName != "Modulus" && !reader.ReadToFollowing("Modulus"))
  20.                     {
  21.                         return null;
  22.                     }
  23.                     string modulus = reader.ReadElementContentAsString();

  24.                     if (reader.LocalName != "D" && !reader.ReadToFollowing("D"))
  25.                     {
  26.                         return null;
  27.                     }
  28.                     string d = reader.ReadElementContentAsString();

  29.                     RSAPrivateKey privateKey = new RSAPrivateKey();
  30.                     privateKey.Modulus = Convert.FromBase64String(modulus);
  31.                     privateKey.D = Convert.FromBase64String(d);

  32.                     return privateKey;
  33.                 }
  34.             }
  35.             catch
  36.             {
  37.                 return null;
  38.             }
  39.         }        
  40.     }
复制代码
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.             int blockOffset = 0;
  11.             using (MemoryStream stream = new MemoryStream())
  12.             {
  13.                 while (blockOffset < data.Length)
  14.                 {
  15.                     int blockLen = Math.Min(blockSize, data.Length - blockOffset);
  16.                     byte[] blockData = new byte[blockLen];
  17.                     Buffer.BlockCopy(data, blockOffset, blockData, 0, blockLen);

  18.                     BigInteger mi = new BigInteger(blockData);
  19.                     BigInteger ci = mi.modPow(e, n);//ci = mi^e ( mod n )

  20.                     byte[] block = ci.getBytes();
  21.                     stream.Write(block, 0, block.Length);
  22.                     blockOffset += blockLen;
  23.                 }

  24.                 return stream.ToArray();
  25.             }
  26.         }

  27.         private static byte[] Compute(byte[] data, RSAPrivateKey privateKey, int blockSize)
  28.         {
  29.             //
  30.             // 私钥加密/解密公式为:mi = ci^d ( mod n )
  31.             //
  32.             // 先将 c(二进制表示)分成数据块 c1, c2, ..., ci ,然后进行运算。            
  33.             //
  34.             BigInteger d = new BigInteger(privateKey.D);
  35.             BigInteger n = new BigInteger(privateKey.Modulus);

  36.             int blockOffset = 0;

  37.             using (MemoryStream stream = new MemoryStream())
  38.             {
  39.                 while (blockOffset < data.Length)
  40.                 {
  41.                     int blockLen = Math.Min(blockSize, data.Length - blockOffset);
  42.                     byte[] blockData = new byte[blockLen];
  43.                     Buffer.BlockCopy(data, blockOffset, blockData, 0, blockLen);

  44.                     BigInteger ci = new BigInteger(blockData);
  45.                     BigInteger mi = ci.modPow(d, n);//mi = ci^d ( mod n )

  46.                     byte[] block = mi.getBytes();
  47.                     stream.Write(block, 0, block.Length);
  48.                     blockOffset += blockLen;
  49.                 }

  50.                 return stream.ToArray();
  51.             }
  52.         }
复制代码
下面是私钥加密->公钥解密的实现。

  1.         public static byte[] Encrypt(byte[] data, RSAPublicKey publicKey)
  2.         {
  3.             if (data == null)
  4.             {
  5.                 throw new ArgumentNullException("data");
  6.             }

  7.             if (publicKey == null)
  8.             {
  9.                 throw new ArgumentNullException("publicKey");
  10.             }

  11.             int blockSize = publicKey.Modulus.Length - 1;
  12.             return Compute(data, publicKey, blockSize);
  13.         }

  14.         public static byte[] Decrypt(byte[] data, RSAPrivateKey privateKey)
  15.         {
  16.             if (data == null)
  17.             {
  18.                 throw new ArgumentNullException("data");
  19.             }

  20.             if (privateKey == null)
  21.             {
  22.                 throw new ArgumentNullException("privateKey");
  23.             }

  24.             int blockSize = privateKey.Modulus.Length;
  25.             return Compute(data, privateKey, blockSize);
  26.         }
复制代码
下面是公钥加密->私钥解密的实现。

  1.         public static byte[] Encrypt(byte[] data, RSAPrivateKey privateKey)
  2.         {
  3.             if (data == null)
  4.             {
  5.                 throw new ArgumentNullException("data");
  6.             }

  7.             if (privateKey == null)
  8.             {
  9.                 throw new ArgumentNullException("privateKey");
  10.             }

  11.             int blockSize = privateKey.Modulus.Length - 1;
  12.             return Compute(data, privateKey, blockSize);
  13.         }

  14.         public static byte[] Decrypt(byte[] data, RSAPublicKey publicKey)
  15.         {
  16.             if (data == null)
  17.             {
  18.                 throw new ArgumentNullException("data");
  19.             }

  20.             if (publicKey == null)
  21.             {
  22.                 throw new ArgumentNullException("publicKey");
  23.             }

  24.             int blockSize = publicKey.Modulus.Length;
  25.             return Compute(data, publicKey, blockSize);
  26.         }  
复制代码
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.             if (publicKey == null)
  8.             {
  9.                 throw new ArgumentNullException("publicKey");
  10.             }

  11.             if (hash == null)
  12.             {
  13.                 throw new ArgumentNullException("hash");
  14.             }

  15.             byte[] hashData = hash.ComputeHash(data);
  16.             byte[] signature = Encrypt(hashData, publicKey);
  17.             return signature;
  18.         }

  19.         public static bool Verify(byte[] data, RSAPrivateKey privateKey, HashAlgorithm hash, byte[] signature)
  20.         {
  21.             if (data == null)
  22.             {
  23.                 throw new ArgumentNullException("data");
  24.             }

  25.             if (privateKey == null)
  26.             {
  27.                 throw new ArgumentNullException("privateKey");
  28.             }

  29.             if (hash == null)
  30.             {
  31.                 throw new ArgumentNullException("hash");
  32.             }

  33.             if (signature == null)
  34.             {
  35.                 throw new ArgumentNullException("signature");
  36.             }

  37.             byte[] hashData = hash.ComputeHash(data);
  38.             byte[] signatureHashData = Decrypt(signature, privateKey);

  39.             if (signatureHashData != null &amp;&amp; signatureHashData.Length == hashData.Length)
  40.             {
  41.                 for (int i = 0; i < signatureHashData.Length; i++)
  42.                 {
  43.                     if (signatureHashData[i] != hashData[i])
  44.                     {
  45.                         return false;
  46.                     }
  47.                 }
  48.                 return true;
  49.             }

  50.             return false;
  51.         }
复制代码
下面是公钥签名->私钥验证的实现。

  1.         public static byte[] Sign(byte[] data, RSAPrivateKey privateKey, HashAlgorithm hash)
  2.         {
  3.             if (data == null)
  4.             {
  5.                 throw new ArgumentNullException("data");
  6.             }

  7.             if (privateKey == null)
  8.             {
  9.                 throw new ArgumentNullException("privateKey");
  10.             }

  11.             if (hash == null)
  12.             {
  13.                 throw new ArgumentNullException("hash");
  14.             }

  15.             byte[] hashData = hash.ComputeHash(data);
  16.             byte[] signature = Encrypt(hashData, privateKey);
  17.             return signature;
  18.         }

  19.         public static bool Verify(byte[] data, RSAPublicKey publicKey, HashAlgorithm hash, byte[] signature)
  20.         {
  21.             if (data == null)
  22.             {
  23.                 throw new ArgumentNullException("data");
  24.             }

  25.             if (publicKey == null)
  26.             {
  27.                 throw new ArgumentNullException("publicKey");
  28.             }

  29.             if (hash == null)
  30.             {
  31.                 throw new ArgumentNullException("hash");
  32.             }

  33.             if (signature == null)
  34.             {
  35.                 throw new ArgumentNullException("signature");
  36.             }

  37.             byte[] hashData = hash.ComputeHash(data);

  38.             byte[] signatureHashData = Decrypt(signature, publicKey);

  39.             if (signatureHashData != null &amp;&amp; signatureHashData.Length == hashData.Length)
  40.             {
  41.                 for (int i = 0; i < signatureHashData.Length; i++)
  42.                 {
  43.                     if (signatureHashData[i] != hashData[i])
  44.                     {
  45.                         return false;
  46.                     }
  47.                 }
  48.                 return true;
  49.             }

  50.             return false;
  51.         }  
复制代码
本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名KevinShan(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言



源代码下载:

RSAManaged.zip

4 个回复

倒序浏览
winking
葡萄城公司职员   /  发表于:2010-1-4 11:41:00
沙发
貌似借助这个可以实现:
1、对程序(控件)的授权
2、对从服务器上获取敏感的资源保护,防止篡改
3、对提交的数据做封包,防止截获和篡改

只是不知道性能如何?
回复 使用道具 举报
graper
高级会员   /  发表于:2010-1-4 12:26:00
板凳
可能在控件的纯客户端注册验证算法上有点用处。前几天我在尝试写个Silverlight纯客户端注册验证算法,找不到现成的Silverlight上的RSA算法,才写的一个。

性能公钥计算还可以,大约十几ms。私钥计算可能是公钥计算的三四十倍。如果设计成私钥加密Licence文件及签名,公钥解密及验证签名,就没有问题。
回复 使用道具 举报
winking
葡萄城公司职员   /  发表于:2010-1-4 15:38:00
地板
中午看了一下实现,不错的说。
再顶一个……

[quote]前几天我在尝试写个Silverlight纯客户端注册验证算法[/quote]
如果只是这样Feature,考虑使用AES代替RSA,毕竟RSA消耗相对AES来说要大得多,无论从理论算法还是实现,当然安全系数也降低一些。

另外,如果只是做验证签名的话,可以考虑SL本身提供的X509Certificate(X.509 Ver3,支持ASN.1 DER格式验证),貌似也可以作出和RSA类似效果。
回复 使用道具 举报
graper
高级会员   /  发表于:2010-1-4 17:40:00
5#

回复 4# winking 的帖子

AES是对称加密喔,SL本身提供X509Certificate类,不过好像X509Certificate类只包含公钥和私钥等信息,没涉及加密算法。X509Certificate具体怎么用还没太搞懂:~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部