C#在using System.Security.Cryptography下有 DESCryptoServiceProvider RSACryptoServiceProvider? DESCryptoServiceProvider 是用于对称加密 RSACryptoServiceProvider是用于非对称加密? 对称加密的意思:有一个密钥 相当于加密算法,加密用它来加密,解密也需要用到它。因为加密解密都是用同一个密钥所以叫对称加密。 对称加密有一个坏处只要拥有密钥的人都可以解密。? 非对称加密:就是有2个密钥,一个是公钥,一个是私钥,私钥是自己的,不能随便给人,公钥随便给,无所谓。一般是别人用你的公钥加密,然后把密文给你,你用你的私钥解密,这样一样加密和解密不是同一个密钥,所以叫非对称。
1.rsa
/// <summary>
/// 获取加密所使用的key,RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密。
/// </summary>
public static void GetKey(ref string PublicKey ,fer stringPrivateKey )
{
string PublicKey = string.Empty;
string PrivateKey = string.Empty;
RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider();
PublicKey = rSACryptoServiceProvider.ToXmlString(false); // 获取公钥,用于加密
PrivateKey = rSACryptoServiceProvider.ToXmlString(true); // 私匙,
用于解密
}
?/// <summary> ? ? ? ? /// RSA加密 ? ? ? ? /// </summary> ? ? ? ? /// <param name="publickey">公钥</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="privatekey">私钥</param> ? ? ? ? /// <param name="content">要解密的内容</param> ? ? ? ? /// <returns>解密后的内容</returns> ? ? ? ? public static string RSADecrypt(string privatekey, string content) ? ? ? ? { ? ? ? ? ? ? RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); ? ? ? ? ? ? byte[] cipherbytes; ? ? ? ? ? ? rsa.FromXmlString(privatekey); ? ? ? ? ? ? cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
? ? ? ? ? ? return Encoding.UTF8.GetString(cipherbytes); ? ? ? ? }
2.DES
/// <summary>
/// DES加密/解密
/// </summary>
/// <param name="data">加密/解密数据</param>
/// <param name="key">秘钥</param>
/// <param name="keyIV">向量</param>
/// <param name="isEncrypt">true加密,false解密</param>
/// <returns></returns>
public static byte[] EncryptOrDecrypt(byte[] data, byte[] key, byte[] keyIV, bool isEncrypt)
{
DESCryptoServiceProvider desP = new DESCryptoServiceProvider();
if (isEncrypt)// 加密
{
desP.Key = key;
desP.IV = keyIV;
ICryptoTransform desencrypt = desP.CreateEncryptor(key, keyIV);
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return result;
}
else // 解密
{
desP.Key = key;
desP.IV = keyIV;
ICryptoTransform desencrypt = desP.CreateDecryptor(key, keyIV);
byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return result;
}
}
/// <summary>
/// 创建随机秘钥
/// </summary>
/// <returns></returns>
public static byte[] CreateKey()
{
DESCryptoServiceProvider desP = new DESCryptoServiceProvider();
desP.GenerateKey();
return desP.Key;
}
/// <summary>
/// 创建随机向量
/// </summary>
/// <returns></returns>
public static byte[] CreateIV()
{
DESCryptoServiceProvider desP = new DESCryptoServiceProvider();
desP.GenerateIV();
return desP.IV;
}
注:自己做的项目时? 是自己创建自己的 公钥和私钥? ? ?当对接别人项目时? 或许别人给你的是PEM文件? ?格式? ? ? -----BEGIN PUBLIC KEY-----? ? ?公钥的 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ------------------- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?-----END PUBLIC KEY-----? ? ?
需要提取其中的公钥
/// <summary> ?? ??? ?/// 用PEM格式密钥对创建RSA,支持PKCS#1、PKCS#8格式的PEM ?? ??? ?/// 出错将会抛出异常 ?? ??? ?/// </summary> ?? ??? ?/// <param name="pem">读出的pen文本</param> ?? ??? ?/// <returns>返回公钥文本</returns> ?? ??? ?static public RSA_PEM FromPEM(string pem) ?? ??? ?{ ?? ??? ??? ?RSA_PEM param = new RSA_PEM();
?? ??? ??? ?var base64 = _PEMCode.Replace(pem, ""); ?? ??? ??? ?byte[] data = null; ?? ??? ??? ?try { data = Convert.FromBase64String(base64); } catch { } ?? ??? ??? ?if (data == null) ?? ??? ??? ?{ ?? ??? ??? ??? ?throw new Exception("PEM内容无效"); ?? ??? ??? ?} ?? ??? ??? ?var idx = 0;
?? ??? ??? ?//读取长度 ?? ??? ??? ?Func<byte, int> readLen = (first) => { ?? ??? ??? ??? ?if (data[idx] == first) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?idx++; ?? ??? ??? ??? ??? ?if (data[idx] == 0x81) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?idx++; ?? ??? ??? ??? ??? ??? ?return data[idx++]; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?else if (data[idx] == 0x82) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?idx++; ?? ??? ??? ??? ??? ??? ?return (((int)data[idx++]) << 8) + data[idx++]; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?else if (data[idx] < 0x80) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?return data[idx++]; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ??? ?throw new Exception("PEM未能提取到数据"); ?? ??? ??? ?}; ?? ??? ??? ?//读取块数据 ?? ??? ??? ?Func<byte[]> readBlock = () => { ?? ??? ??? ??? ?var len = readLen(0x02); ?? ??? ??? ??? ?if (data[idx] == 0x00) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?idx++; ?? ??? ??? ??? ??? ?len--; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?var val = new byte[len]; ?? ??? ??? ??? ?for (var i = 0; i < len; i++) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?val[i] = data[idx + i]; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?idx += len; ?? ??? ??? ??? ?return val; ?? ??? ??? ?}; ?? ??? ??? ?//比较data从idx位置开始是否是byts内容 ?? ??? ??? ?Func<byte[], bool> eq = (byts) => { ?? ??? ??? ??? ?for (var i = 0; i < byts.Length; i++, idx++) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?if (idx >= data.Length) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?return false; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?if (byts[i] != data[idx]) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?return false; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ??? ?return true; ?? ??? ??? ?};
?? ??? ??? ?if (pem.Contains("PUBLIC KEY")) ?? ??? ??? ?{ ?? ??? ??? ??? ?/****使用公钥****/ ?? ??? ??? ??? ?//读取数据总长度 ?? ??? ??? ??? ?readLen(0x30);
?? ??? ??? ??? ?//检测PKCS8 ?? ??? ??? ??? ?var idx2 = idx; ?? ??? ??? ??? ?if (eq(_SeqOID)) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?//读取1长度 ?? ??? ??? ??? ??? ?readLen(0x03); ?? ??? ??? ??? ??? ?idx++;//跳过0x00 ?? ??? ??? ??? ??? ??? ? ?//读取2长度 ?? ??? ??? ??? ??? ?readLen(0x30); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?else ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?idx = idx2; ?? ??? ??? ??? ?}
?? ??? ??? ??? ?//Modulus ?? ??? ??? ??? ?param.Key_Modulus = readBlock();
?? ??? ??? ??? ?//Exponent ?? ??? ??? ??? ?param.Key_Exponent = readBlock(); ?? ??? ??? ?} ?? ??? ??? ?else if (pem.Contains("PRIVATE KEY")) ?? ??? ??? ?{ ?? ??? ??? ??? ?/****使用私钥****/ ?? ??? ??? ??? ?//读取数据总长度 ?? ??? ??? ??? ?readLen(0x30);
?? ??? ??? ??? ?//读取版本号 ?? ??? ??? ??? ?if (!eq(_Ver)) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?throw new Exception("PEM未知版本"); ?? ??? ??? ??? ?}
?? ??? ??? ??? ?//检测PKCS8 ?? ??? ??? ??? ?var idx2 = idx; ?? ??? ??? ??? ?if (eq(_SeqOID)) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?//读取1长度 ?? ??? ??? ??? ??? ?readLen(0x04); ?? ??? ??? ??? ??? ?//读取2长度 ?? ??? ??? ??? ??? ?readLen(0x30);
?? ??? ??? ??? ??? ?//读取版本号 ?? ??? ??? ??? ??? ?if (!eq(_Ver)) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?throw new Exception("PEM版本无效"); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ??? ?else ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?idx = idx2; ?? ??? ??? ??? ?}
?? ??? ??? ??? ?//读取数据 ?? ??? ??? ??? ?param.Key_Modulus = readBlock(); ?? ??? ??? ??? ?param.Key_Exponent = readBlock(); ?? ??? ??? ??? ?int keyLen = param.Key_Modulus.Length; ?? ??? ??? ??? ?param.Key_D = BigL(readBlock(), keyLen); ?? ??? ??? ??? ?keyLen = keyLen / 2; ?? ??? ??? ??? ?param.Val_P = BigL(readBlock(), keyLen); ?? ??? ??? ??? ?param.Val_Q = BigL(readBlock(), keyLen); ?? ??? ??? ??? ?param.Val_DP = BigL(readBlock(), keyLen); ?? ??? ??? ??? ?param.Val_DQ = BigL(readBlock(), keyLen); ?? ??? ??? ??? ?param.Val_InverseQ = BigL(readBlock(), keyLen); ?? ??? ??? ?} ?? ??? ??? ?else ?? ??? ??? ?{ ?? ??? ??? ??? ?throw new Exception("pem需要BEGIN END标头"); ?? ??? ??? ?}
?? ??? ??? ?return param; ?? ??? ?}
|