C#中RSA加密解密和签名与验证的实现

RSA加密算法是一种非对称加密算法。在公钥加密标准和电子商业中RSA被广泛使用。RSA是1977年由罗纳德•李维斯特(Ron Rivest)、阿迪•萨莫尔(Adi Shamir)和伦纳德•阿德曼(Leonard Adleman)一起提出的。当时他们三人都在麻省理工学院工作。RSA就是他们三人姓氏开头字母拼在一起组成的。.Net的推出,我们能够利用.Net Framework中的类提供的加密服务来保证数据安全。目前应用较为广泛的加密方法是使用RSA算法进行加密。在.Net Framework中与RSA加密算法相关的类主要有两个:RSA 类和RSACryptoServiceProvider 类。按照MSDN的说法RSA 类是“表示 RSA 算法的所有实现均从中继承的基类”,而RSACryptoServiceProvider 类是“使用加密服务提供程序 (CSP) 提供的 RSA 算法的实现执行不对称加密和解密”。另外,“表示 RSA 算法的标准参数”的RSAParameters 结构也是很重要的,它保存了RSA算法的参数。
这里具体讲述一下在C#中如何使用框架提供的RSA算法来对我们的信息加密、签名、验证签名、解密的这个几个步骤的实现

        using System.Security.Cryptography;

        using System.Management;

        using Microsoft.Win32;

        /// <summary>

        /// 生成公私钥

        /// </summary>

        /// <param name="PrivateKeyPath"></param>

        /// <param name="PublicKeyPath"></param>

        public void RSAKey(string PrivateKeyPath, string PublicKeyPath)

        {

            try

            {

                RSACryptoServiceProvider provider = new RSACryptoServiceProvider();

                this.CreatePrivateKeyXML(PrivateKeyPath, provider.ToXmlString(true));

                this.CreatePublicKeyXML(PublicKeyPath, provider.ToXmlString(false));

            }

            catch (Exception exception)

            {

                throw exception;

            }

        }

        /// <summary>

        /// 对原始数据进行MD5加密

        /// </summary>

        /// <param name="m_strSource">待加密数据</param>

        /// <returns>返回机密后的数据</returns>

        public string GetHash(string m_strSource)

        {

            HashAlgorithm algorithm = HashAlgorithm.Create("MD5");

            byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(m_strSource);

            byte[] inArray = algorithm.ComputeHash(bytes);

            return Convert.ToBase64String(inArray);

        }

        /// <summary>

        /// RSA加密

        /// </summary>

        /// <param name="xmlPublicKey">公钥</param>

        /// <param name="m_strEncryptString">MD5加密后的数据</param>

        /// <returns>RSA公钥加密后的数据</returns>

        public string RSAEncrypt(string xmlPublicKey, string m_strEncryptString)

        {

            string str2;

            try

            {

                RSACryptoServiceProvider provider = new RSACryptoServiceProvider();

                provider.FromXmlString(xmlPublicKey);

                byte[] bytes = new UnicodeEncoding().GetBytes(m_strEncryptString);

                str2 = Convert.ToBase64String(provider.Encrypt(bytes, false));

            }

            catch (Exception exception)

            {

                throw exception;

            }

            return str2;

        }

        /// <summary>

        /// RSA解密

        /// </summary>

        /// <param name="xmlPrivateKey">私钥</param>

        /// <param name="m_strDecryptString">待解密的数据</param>

        /// <returns>解密后的结果</returns>

        public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString)

        {

            string str2;

            try

            {

                RSACryptoServiceProvider provider = new RSACryptoServiceProvider();

                provider.FromXmlString(xmlPrivateKey);

                byte[] rgb = Convert.FromBase64String(m_strDecryptString);

                byte[] buffer2 = provider.Decrypt(rgb, false);

                str2 = new UnicodeEncoding().GetString(buffer2);

            }

            catch (Exception exception)

            {

                throw exception;

            }

            return str2;

        }

        /// <summary>

        /// MD5加密后的密文进行签名

        /// </summary>

        /// <param name="p_strKeyPrivate">私钥</param>

        /// <param name="m_strHashbyteSignature">MD5加密后的密文</param>

        /// <returns></returns>

        public string SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature)

        {

            byte[] rgbHash = Convert.FromBase64String(m_strHashbyteSignature);

            RSACryptoServiceProvider key = new RSACryptoServiceProvider();

            key.FromXmlString(p_strKeyPrivate);

            RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);

            formatter.SetHashAlgorithm("MD5");

            byte[] inArray = formatter.CreateSignature(rgbHash);

            return Convert.ToBase64String(inArray);

        }

        /// <summary>

        /// 签名验证

        /// </summary>

        /// <param name="p_strKeyPublic">公钥</param>

        /// <param name="p_strHashbyteDeformatter">待验证的用户名</param>

        /// <param name="p_strDeformatterData">注册码</param>

        /// <returns></returns>

        public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)

        {

            try

            {

                byte[] rgbHash = Convert.FromBase64String(p_strHashbyteDeformatter);

                RSACryptoServiceProvider key = new RSACryptoServiceProvider();

                key.FromXmlString(p_strKeyPublic);

                RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);

                deformatter.SetHashAlgorithm("MD5");

                byte[] rgbSignature = Convert.FromBase64String(p_strDeformatterData);

                if (deformatter.VerifySignature(rgbHash, rgbSignature))

                {

                    return true;

                }

                return false;

            }

            catch

            {

                return false;

            }

        }

        /// <summary>

        /// 获取硬盘ID

        /// </summary>

        /// <returns>硬盘ID</returns>

        public string GetHardID()

        {

            string HDInfo = "";

            ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");

            ManagementObjectCollection moc1 = cimobject1.GetInstances();

            foreach (ManagementObject mo in moc1)

            {

                HDInfo = (string)mo.Properties["Model"].Value;

            }

            return HDInfo;

        }

        /// <summary>

        /// 读注册表中指定键的值

        /// </summary>

        /// <param name="key">键名</param>

        /// <returns>返回键值</returns>

        private string ReadReg(string key)

        {

            string temp = "";

            try

            {

                RegistryKey myKey = Registry.LocalMachine;

                RegistryKey subKey = myKey.OpenSubKey(@"SOFTWARE/JX/Register");

 

                temp = subKey.GetValue(key).ToString();

                subKey.Close();

                myKey.Close();

                return temp;

            }

            catch (Exception)

            {

                throw;//可能没有此注册项;

            }

 

        } 

        /// <summary>

        /// 创建注册表中指定的键和值

        /// </summary>

        /// <param name="key">键名</param>

        /// <param name="value">键值</param>

        private void WriteReg(string key, string value)

        {

            try

            {

                RegistryKey rootKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE/JX/Register");

                rootKey.SetValue(key, value);

                rootKey.Close();

            }

            catch (Exception)

            {

                throw;

            }

        }

        /// <summary>

        /// 创建公钥文件

        /// </summary>

        /// <param name="path"></param>

        /// <param name="publickey"></param>

        public void CreatePublicKeyXML(string path, string publickey)

        {

            try

            {

                FileStream publickeyxml = new FileStream(path, FileMode.Create);

                StreamWriter sw = new StreamWriter(publickeyxml);

                sw.WriteLine(publickey);

                sw.Close();

                publickeyxml.Close();

            }

            catch

            {

                throw;

            }

        }

        /// <summary>

        /// 创建私钥文件

        /// </summary>

        /// <param name="path"></param>

        /// <param name="privatekey"></param>

        public void CreatePrivateKeyXML(string path, string privatekey)

        {

            try

            {

                FileStream privatekeyxml = new FileStream(path, FileMode.Create);

                StreamWriter sw = new StreamWriter(privatekeyxml);

                sw.WriteLine(privatekey);

                sw.Close();

                privatekeyxml.Close();

            }

            catch

            {

                throw;

            }

        }

        /// <summary>

        /// 读取公钥

        /// </summary>

        /// <param name="path"></param>

        /// <returns></returns>

        public string ReadPublicKey(string path)

        {

            StreamReader reader = new StreamReader(path);

            string publickey = reader.ReadToEnd();

            reader.Close();

            return publickey;

        }

        /// <summary>

        /// 读取私钥

        /// </summary>

        /// <param name="path"></param>

        /// <returns></returns>

        public string ReadPrivateKey(string path)

        {

            StreamReader reader = new StreamReader(path);

            string privatekey = reader.ReadToEnd();

            reader.Close();

            return privatekey;

        }

        /// <summary>

        /// 初始化注册表,程序运行时调用,在调用之前更新公钥xml

        /// </summary>

        /// <param name="path">公钥路径</param>

        public void InitialReg(string path)

        {

            Registry.LocalMachine.CreateSubKey(@"SOFTWARE/JX/Register");

            Random ra = new Random();

            string publickey = this.ReadPublicKey(path);

            if (Registry.LocalMachine.OpenSubKey(@"SOFTWARE/JX/Register").ValueCount <= 0)

            {

                this.WriteReg("RegisterRandom", ra.Next(1,100000).ToString());

                this.WriteReg("RegisterPublicKey", publickey);

            }

            else

            {

                this.WriteReg("RegisterPublicKey", publickey);

            }

        } 

如果是要对发送的消息进行加密和解密,加密时用公钥,解密时用私钥,即使密文被窃取也无法破解。

如果是要对软件进行注册,生成注册码,则服务端将用户的硬盘号用私钥加密,客户端用公钥解密,解密后将客户端的硬盘号进行MD5加密,将得到的结果和解密后的结果进行比较,如果相同,说明是注册用户,否则为非注册用户。

发表评论

您的电子邮箱地址不会被公开。