/// <summary>
/// 生成RSA公钥和私钥
/// </summary>
public static bool CreateRSAKey()
{
bool result = false;
try
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
//生成私有密钥,这个文件是要保密的
using (StreamWriter writer = new StreamWriter("PrivateKey.xml"))
{
writer.WriteLine(rsa.ToXmlString(true));
writer.Close();
}
//生成公有密钥,可以公开
using (StreamWriter writer = new StreamWriter("PublicKey.xml"))
{
writer.WriteLine(rsa.ToXmlString(false));
writer.Close();
}
return true;
//这个可以没有
// MessageBox.Show("公有密钥和私有密钥生成成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
}
return result;
}
/// <summary>
/// RSA加密(使用公有密钥进行加密)
/// </summary>
/// <param name="privatekey">私有密钥</param>
/// <param name="content">要加密的字符串</param>
/// <returns>加密后的字符串</returns>
public static string RSAEncrypt(string publickey, string content)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(publickey);
cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
return Convert.ToBase64String(cipherbytes);
}
/// <summary>
/// RSA解密
/// </summary>
/// <param name="publickey">私有密钥</param>
/// <param name="content">私有密钥加密过的字符串</param>
/// <returns>解密后的字符串</returns>
public static string RSADecrypt(string privatekey, string content)
{
try
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(privatekey);
cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
return Encoding.UTF8.GetString(cipherbytes);
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
/// AES加密算法
/// </summary>
/// <param name="plainText">明文字符串</param>
/// <param name="strKey">密钥</param>
/// <returns>返回加密后的密文字节数组</returns>
public static byte[] AESEncrypt(string plainText, byte[] strKey, byte[] IV)
{
//分组加密算法
SymmetricAlgorithm des = Rijndael.Create();
byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组
//设置密钥及密钥向量
//des.Key = Encoding.UTF8.GetBytes(strKey);
des.Key = strKey;
des.IV = IV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
byte[] cipherBytes = ms.ToArray();//得到加密后的字节数组
cs.Close();
ms.Close();
return cipherBytes;
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="cipherText">密文字节数组</param>
/// <param name="strKey">密钥</param>
/// <returns>返回解密后的字符串</returns>
public static byte[] AESDecrypt(byte[] cipherText, byte[] strKey, byte[] IV)
{
SymmetricAlgorithm des = Rijndael.Create();
// des.Key = Encoding.UTF8.GetBytes(strKey);
// des.IV = _key1;
des.Key = strKey;
des.IV = IV;
byte[] decryptBytes = new byte[cipherText.Length];
MemoryStream ms = new MemoryStream(cipherText);
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);
cs.Read(decryptBytes, 0, decryptBytes.Length);
cs.Close();
ms.Close();
return decryptBytes;
}
public static byte[] GetRandomCharArray(int count)
{
int number;
string checkCode = String.Empty; //存放随机码的字符串
System.Random random = new Random();
for (int i = 0; i < count; i++) //产生4位校验码
{
number = random.Next();
number = number % 36;
if (number < 10)
{
number += 48; //数字0-9编码在48-57
}
else
{
number += 55; //字母A-Z编码在65-90
}
checkCode += ((char)number).ToString();
}
return System.Text.Encoding.Default.GetBytes(checkCode);
}
public static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
public static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
public static string GetModuleFromXML(string pubKey) //获取密钥的module部分
{
int firstIndex = pubKey.IndexOf("<Modulus>");
int lastIndex = pubKey.LastIndexOf("</Modulus>");
string module = pubKey.Substring(firstIndex + 9, (lastIndex - firstIndex - 9));
return module;
}
public static string GetExponentFromXML(string pubKey) // 获取密钥的Exponent部分
{
int firstIndex = pubKey.IndexOf("<Exponent>");
int lastIndex = pubKey.LastIndexOf("</Exponent>");
string exponent = pubKey.Substring(firstIndex + 10, (lastIndex - firstIndex - 10));
return exponent;
}
public static string GetClientPubKeyXML(string module, string exponent)
{
string keyString = "<RSAKeyValue><Modulus>" + module + "</Modulus><Exponent>" + exponent + "</Exponent></RSAKeyValue>";
return keyString;
}
public static string FromByteTo16String(byte[] b)
{
string ss = "";
for (int i = 0; i < b.Length; i++)
{
ss += Convert.ToString(b[i], 16);
}
return ss;
}