前面已经把使用OpenSSL.Net环境准备好了,现在来调用几个常用算法的实现
- MD5,SHA1
在这只需要注意下OpenSSL.Crypto.MessageDiges后面签名算法会用到。
1 ????class Program_Hash 2 ????{ 3 ????????static void Main(string[] args) 4 ????????{ 5 ????????????var ciphertext = MD5("Md5加密。", Encoding.UTF8); 6 ????????????ciphertext = SHA1("SHA1加密。", Encoding.UTF8); 7 ????????} 8 ?9 ????????public static string MD5(string text, Encoding encoding)10 ????????{11 ????????????return HashDigest(text, encoding, MessageDigest.MD5);12 ????????}13 14 ????????public static string SHA1(string text, Encoding encoding)15 ????????{16 ????????????return HashDigest(text, encoding, MessageDigest.SHA1);17 ????????}18 19 ????????private static string HashDigest(string text, Encoding encoding, MessageDigest hashType)20 ????????{21 ????????????using (MessageDigestContext hashDigest = new MessageDigestContext(hashType))22 ????????????{23 ????????????????byte[] hashBytes = encoding.GetBytes(text);24 ????????????????byte[] signByte = hashDigest.Digest(hashBytes);25 ????????????????return BitConverter.ToString(signByte).Replace("-", "").ToLower();26 ????????????}27 ????????}28 ????}
- AES
1、在这里SymmetricCrypt是之前项目封装的System.Security.Cryptography.RijndaelManaged
2、Key与IV是有其规律的因此IV是可以去掉
3、在string与byte[]之间转换时要注意编码,什么时候用Convert.FromBase64String,什么时候用Encoding
4、可以查看下Cipher封装,里面有不同的算法
1 ????class Program_Symmetric 2 ????{ 3 ????????static void Main(string[] args) 4 ????????{ 5 ????????????SymmetricCrypt symmetric = new SymmetricCrypt(CryptType.Rijndael); 6 ????????????string key = "PGKQBXNCiuwKmlIUThSy1h+ZHMAN+HytbZny/FepkvM=", 7 ????????????????iv = "aqauVvyO7qvAbaDsdOeFsA==", 8 ????????????????text = "AES256加解密。"; 9 10 ????????????var ctext = symmetric.Encrypt(text, key, iv);11 12 ????????????var ctext2 = symmetric.Decrypt(ctext, key, iv);13 14 ????????????var ctext3 = Encrypt(ctext2, key, iv);15 16 ????????????Decrypt(ctext3, key, iv);17 ????????}18 19 ????????public static string Decrypt(string text, string key, string iv)20 ????????{21 ????????????byte[] keyBytes = Convert.FromBase64String(key);22 ????????????byte[] ivBytes = Convert.FromBase64String(iv);23 ????????????byte[] textBytes = Convert.FromBase64String(text);24 ????????????using (CipherContext cipher = new CipherContext(Cipher.AES_256_CBC))25 ????????????{26 ????????????????byte[] output = cipher.Decrypt(textBytes, keyBytes, ivBytes);27 ????????????????var result = Encoding.UTF8.GetString(output);28 ????????????????return result;29 ????????????}30 ????????}31 32 ????????public static string Encrypt(string text, string key, string iv)33 ????????{34 ????????????byte[] keyBytes = Convert.FromBase64String(key);35 ????????????byte[] ivBytes = Convert.FromBase64String(iv);36 ????????????byte[] textBytes = Encoding.UTF8.GetBytes(text);37 ????????????using (CipherContext cipher = new CipherContext(Cipher.AES_256_CBC))38 ????????????{39 ????????????????byte[] output = cipher.Encrypt(textBytes, keyBytes, ivBytes);40 ????????????????var result = Convert.ToBase64String(output);41 ????????????????return result;42 ????????????}43 ????????}44 ????}
- RSA
1、公钥与私钥可以用OpenSSL命令行随意生成
2、OpenSSL.Core.BIO简单理解是用于装载密钥的容器
3、注意OpenSSL.Crypto.RSA的静态方法
4、OpenSSL.Crypto.CryptoKey用于装载BIO能把密钥转换成具体的算法对象,这个类的作用很大,涉及到签名验签都会用到
?1 class Program_RSA ?2 ????{ ?3 ????????static void Main(string[] args) ?4 ????????{ ?5 ????????????string privateKey = "", publicKey = "", text = "RSA-1024加解密。", ctext = ""; ?6 ????????????int padding = 1; ?7 ????????????Encoding encoding = Encoding.UTF8; ?8 ????????????using (RSA rsa = new RSA()) ?9 ????????????{ 10 ????????????????rsa.GenerateKeys(1024, BigNumber.One, null, null); 11 ????????????????privateKey = rsa.PrivateKeyAsPEM; 12 ????????????????publicKey = rsa.PublicKeyAsPEM; 13 ????????????} 14 ?15 ????????????ctext = PrivateEncrypt(privateKey, text, encoding, padding); 16 ????????????text = PublicDecrypt(publicKey, ctext, encoding, padding); 17 ?18 ????????????ctext = PublicEncrypt(publicKey, text, encoding, padding); 19 ????????????text = PrivateDecrypt(privateKey, ctext, encoding, padding); 20 ?21 ????????????var signText = Sign(privateKey, text, encoding); 22 ????????????var signTag = Verify(publicKey, text, signText, encoding); 23 ?24 ????????} 25 ?26 ????????/// <summary> 27 ????????/// 私钥解密 28 ????????/// </summary> 29 ????????public static string PrivateDecrypt(string privateKey, string text, Encoding encoding, int padding) 30 ????????{ 31 ????????????byte[] textBytes = Convert.FromBase64String(text); 32 ????????????using (BIO bio = new BIO(privateKey)) 33 ????????????{ 34 ????????????????using (RSA rsa = RSA.FromPrivateKey(bio)) 35 ????????????????{ 36 ????????????????????textBytes = rsa.PrivateDecrypt(textBytes, (RSA.Padding)padding); 37 ????????????????} 38 ????????????} 39 ????????????return encoding.GetString(textBytes); 40 ????????} 41 ?42 ????????/// <summary> 43 ????????/// 私钥加密 44 ????????/// </summary> 45 ????????public static string PrivateEncrypt(string privateKey, string text, Encoding encoding, int padding) 46 ????????{ 47 ????????????byte[] textBytes = encoding.GetBytes(text); 48 ????????????using (BIO bio = new BIO(privateKey)) 49 ????????????{ 50 ????????????????using (RSA rsa = RSA.FromPrivateKey(bio)) 51 ????????????????{ 52 ????????????????????textBytes = rsa.PrivateEncrypt(textBytes, (RSA.Padding)padding); 53 ????????????????} 54 ????????????} 55 ????????????return Convert.ToBase64String(textBytes); 56 ????????} 57 ?58 ????????/// <summary> 59 ????????/// 公钥解密 60 ????????/// </summary> 61 ????????public static string PublicDecrypt(string publicKey, string text, Encoding encoding, int padding) 62 ????????{ 63 ????????????byte[] textBytes = Convert.FromBase64String(text); 64 ????????????using (BIO bio = new BIO(publicKey)) 65 ????????????{ 66 ????????????????using (RSA rsa = RSA.FromPublicKey(bio)) 67 ????????????????{ 68 ????????????????????textBytes = rsa.PublicDecrypt(textBytes, (RSA.Padding)padding); 69 ????????????????} 70 ????????????} 71 ????????????return encoding.GetString(textBytes); 72 ????????} 73 ?74 ????????/// <summary> 75 ????????/// 公钥加密 76 ????????/// </summary> 77 ????????public static string PublicEncrypt(string publicKey, string text, Encoding encoding, int padding) 78 ????????{ 79 ????????????byte[] textBytes = encoding.GetBytes(text); 80 ????????????using (BIO bio = new BIO(publicKey)) 81 ????????????{ 82 ????????????????using (RSA rsa = RSA.FromPublicKey(bio)) 83 ????????????????{ 84 ????????????????????textBytes = rsa.PublicEncrypt(textBytes, (RSA.Padding)padding); 85 ????????????????????rsa.Dispose(); 86 ????????????????} 87 ????????????????bio.Dispose(); 88 ????????????} 89 ????????????return Convert.ToBase64String(textBytes); 90 ????????} 91 ?92 ????????/// <summary> 93 ????????/// 私钥签名 94 ????????/// </summary> 95 ????????public static string Sign(string privateKey, string text, Encoding encoding) 96 ????????{ 97 ????????????using (BIO bio = new BIO(privateKey)) 98 ????????????{ 99 ????????????????using (CryptoKey cryptoKey = CryptoKey.FromPrivateKey(bio, null))100 ????????????????{101 ????????????????????using (MessageDigestContext sha256 = new MessageDigestContext(MessageDigest.SHA256))102 ????????????????????{103 ????????????????????????byte[] msgByte = encoding.GetBytes(text);104 ????????????????????????byte[] signByte = sha256.Sign(msgByte, cryptoKey);105 ????????????????????????return Convert.ToBase64String(signByte);106 ????????????????????}107 ????????????????}108 ????????????}109 ????????}110 111 ????????/// <summary>112 ????????/// 公钥验签113 ????????/// </summary>114 ????????public static bool Verify(string publicKey, string text, string sign, Encoding encoding)115 ????????{116 ????????????using (BIO bio = new BIO(publicKey))117 ????????????{118 ????????????????using (CryptoKey cryptoKey = CryptoKey.FromPublicKey(bio, null))119 ????????????????{120 ????????????????????using (MessageDigestContext sha256 = new MessageDigestContext(MessageDigest.SHA256))121 ????????????????????{122 ????????????????????????byte[] msgByte = encoding.GetBytes(text);123 ????????????????????????byte[] signByte = Convert.FromBase64String(sign);124 ????????????????????????return sha256.Verify(msgByte, signByte, cryptoKey);125 ????????????????????}126 ????????????????}127 ????????????}128 ????????}129 ????}
OpenSSL.Net使用随记(二)
原文地址:https://www.cnblogs.com/azeri/p/8973166.html