标签:
RSA加解密
using System.Security.Cryptography;
private void button18_Click(object sender, EventArgs e)//RSA加密 { bool RSA_Mode = radioButton13.Checked; //填充方式true 为oaep false为pkcs#1 1.5 string RSA_Message = richTextBox15.Text; richTextBox12.Text = ""; byte[] source = Encoding.Default.GetBytes(RSA_Message); //明文转换为byte byte[] ciphertext; //密文byte数组 string publickey = richTextBox17.Text; //string密钥 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); try { rsa.FromXmlString(publickey); //导入string密钥 ciphertext = rsa.Encrypt(source , RSA_Mode); //加密 StringBuilder sb = new StringBuilder(); foreach (byte b in ciphertext) { sb.AppendFormat("{0:X2}", b); } richTextBox12.Text = sb.ToString(); } catch { MessageBox.Show("加密失败,请检查密钥"); } } }
private void button17_Click(object sender, EventArgs e)//RSA解密 { bool RSA_Mode = radioButton13.Checked; string RSA_Ciphertext = richTextBox12.Text; richTextBox15.Text = ""; string privatekey = richTextBox16.Text; byte[] ciphertext = new byte[RSA_Ciphertext.Length / 2]; try { for (int x = 0; x < RSA_Ciphertext.Length / 2; x++) { int i = (Convert.ToInt32(RSA_Ciphertext.Substring(x * 2, 2), 16)); ciphertext[x] = (byte)i; } } catch { MessageBox.Show("密文不正确!"); } byte[] source; //原文byte数组 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); try { rsa.FromXmlString(privatekey); //设置私钥 source = rsa.Decrypt(ciphertext, RSA_Mode); //解密,得到byte数组 richTextBox15.Text = Encoding.Default.GetString(source); //返回结果 } catch { MessageBox.Show("密钥不正确"); } }
c#中RSA密钥不是数字,是XML格式的密钥
是用特殊的BASE64加密的
http://www.cnblogs.com/midea0978/archive/2007/05/22/755826.html
C#默认公钥是65537也就是AQAB,不知道怎么改掉
标签:
原文地址:http://www.cnblogs.com/xzhblogs/p/5799057.html