当前位置:编程学习 > 网站相关 >>

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

 

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文件的程序。

 

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 

 

    //生成公钥XML字符串 

    string publicKeyXmlString = rsa.ToXmlString(false); 

 

    //生成私钥XML字符串 

    string privateKeyXmlString = rsa.ToXmlString(true); 

 

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

public class RSAPublicKey 

    { 

        public byte[] Modulus; 

        public byte[] Exponent; 

 

        public static RSAPublicKey FromXmlString(string xmlString) 

        { 

            if (string.IsNullOrEmpty(xmlString)) 

            { 

                return null; 

            } 

 

            try 

            { 

                using (XmlReader reader = XmlReader.Create(new StringReader(xmlString))) 

                { 

                    if (!reader.ReadToFollowing("RSAKeyValue")) 

                    { 

                        return null; 

                    } 

 

                    if (reader.LocalName != "Modulus" && !reader.ReadToFollowing("Modulus")) 

                    { 

                        return null; 

                    } 

                    string modulus = reader.ReadElementContentAsString(); 

 

                    if (reader.LocalName != "Exponent" && !reader.ReadToFollowing("Exponent")) 

                    { 

                        return null; 

                    } 

                    string exponent = reader.ReadElementContentAsString(); 

 

                    RSAPublicKey publicKey = new RSAPublicKey(); 

                    publicKey.Modulus = Convert.FromBase64String(modulus); 

                    publicKey.Exponent = Convert.FromBase64String(exponent); 

 

                    return publicKey; 

                } 

            } 

            catch 

            { 

                return null; 

            } 

        }         

    } 

 

public class RSAPrivateKey 

    { 

        public byte[] Modulus; 

        public byte[] D; 

 

        public static RSAPrivateKey FromXmlString(string xmlString) 

        { 

            if (string.IsNullOrEmpty(xmlString)) 

            { 

                return null; 

            } 

 

            try 

            { 

                usin

补充:综合编程 , 安全编程 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,