////// RSA注册码生成验证帮助类 /// public class RSASignature { ////// 生成密匙 /// /// 私匙 /// 公匙> public static void GenerateKeys(out string privateKey, out string publicKey) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { privateKey = rsa.ToXmlString(true); publicKey = rsa.ToXmlString(false); } } ////// RSA签名 /// /// 明文 /// 私匙 ///密文 public static string CreateSignature(string sSource, string sPrivateKey) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(sPrivateKey); RSAPKCS1SignatureFormatter sf = new RSAPKCS1SignatureFormatter(rsa); sf.SetHashAlgorithm("SHA256"); byte[] source = System.Text.ASCIIEncoding.ASCII.GetBytes(sSource); SHA256Managed sha2 = new SHA256Managed(); byte[] result = sha2.ComputeHash(source); byte[] signature = sf.CreateSignature(result); return Convert.ToBase64String(signature); } } ////// RSA签名验证 /// /// 密文 /// 需要比较的明文字符串 /// 公匙 ///是否相同 public static bool VerifySignature(string sEncryptSource, string sCompareString, string sPublicKey) { try { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(sPublicKey); RSAPKCS1SignatureDeformatter df = new RSAPKCS1SignatureDeformatter(rsa); df.SetHashAlgorithm("SHA256"); byte[] signature = Convert.FromBase64String(sEncryptSource); SHA256Managed sha2 = new SHA256Managed(); byte[] compareByte = sha2.ComputeHash(ASCIIEncoding.ASCII.GetBytes(sCompareString)); return df.VerifySignature(compareByte, signature); } } catch (Exception) { return false; } } }
改进:将SHA1改为SHA256,提高安全性
测试string publicKey = ""; string privateKey = " 2DF7PLozXLLJb2vTknucTOK8+ucjYlVFOThnRwqULjeSuA2ZlReaBVXRy8PonVkXXfN2tjosy2ToTwHNX4UQP5kkEbBWYk8knOZPF/96OI2GfjGYnwNUT0CIKHda8B8ztfh1LQanNOCrrByHIuGTORL1WR2hlbaIHRrOIRkeOMc= AQAB "; private void btnCreateRegister_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(txtComputerInfo.Text)) { txtRegister.Text = RSASignature.CreateSignature(txtComputerInfo.Text.Trim(), privateKey); } else { MessageBox.Show("请输入机器码!"); txtComputerInfo.Focus(); } } private void button1_Click(object sender, EventArgs e) { bool result = RSASignature.VerifySignature(txtRegister.Text, txtComputerInfo.Text.Trim(), publicKey); MessageBox.Show(result.ToString()); } 2DF7PLozXLLJb2vTknucTOK8+ucjYlVFOThnRwqULjeSuA2ZlReaBVXRy8PonVkXXfN2tjosy2ToTwHNX4UQP5kkEbBWYk8knOZPF/96OI2GfjGYnwNUT0CIKHda8B8ztfh1LQanNOCrrByHIuGTORL1WR2hlbaIHRrOIRkeOMc= AQAB +WLUzPp7LeMmXaZ3Ebcw6+Ks1HA6XlkpfhLS4DOnViw2O1V/8EXFO77KwFLSwlCzfXo1LN0JuTEsacGdU3f5yw==
3e1K+J8Ns4UrbhQzgIbyGf8JX+VaETWClJSq9vEXelHGfG9/+5bbGvnF4Wo1c/Kdkuz5IQHfYbuTgMU4LpVNdQ==bV/6IrhMo/B22CX1HNDyZNIyqK1b/BswxnltASXGY1XapyU1imSfPzfLZpQsmUqZnhOmFLoQ1KcLaoqe/IenBw== J3CndT3XEoNlL3/5kyroVcRkZx18fzhyR3OQWqiIEp3711t9dDngIJApIaFddIi2hkkFEV+i0lyinyztiiHQjQ== Fdk/2JOc/gj7YKsKgMEEVC0+X+WlAMhF+PJVgs188vIQSuXJTRi8Z4A214fvX49s2G81Th7KdYUWCvBAeD153Q== wgJFkY2XIgzfb7nXt5BuByFCbfaWblwPLvZK2BnKOF9urvSlaoIvmbIV/0HjsbCNo6YkVguGv0/imWQIpTuIA6O5S1NX/dbYLp2/rSNl0/xSB5ZjX06VAYbeIsB7YWw87+35K8DrIrlZDMP4Mk6h+pawdgLTtIo/tjTpBhCfB+E=