这几天一直在处理不同算法的需求,前面俩篇有des、3des的基础源码。
因为初涉算法时间不长、不敢妄下定论,只能提供单一版。对于不同算法在不同语言、跨平台、弱密钥等等情况。我会尽自己最大努力学习、研究。
这几篇希望可以帮助初学者。
常见的算法模式一般为:
CBC模式 每个纯文本块在加密前,通过按位“异或”操作与前一个块的密码文本结合。这样确保了即使纯文本包含许多相同的块,这些块中的每一个也会加密为不同的密码文本块。
ECB模式 是分组密码的一种最基本的工作模式。在该模式下,待处理信息被分为大小合适的分组,然后分别对每一分组独立进行加密或解密处理。ECB模式一般只适用于小数据量的字符信息的安全性保护,例如密钥保护。
当然key、iv在加密解密的时候是需要保持一致的。
下面的源码是C#版本的AES算法、CBC模式、128位。
1 public class _AESEncrypt 2 { 3 /// <summary> 4 /// 有密码的AES加密 5 /// </summary> 6 /// <param name="text">加密字符</param> 7 /// <param name="key">加密的密码</param> 8 /// <param name="iv">密钥</param> 9 /// <returns></returns> 10 public static string AESEncrypt(string text, string key, string iv) 11 { 12 RijndaelManaged rijndaelCipher = new RijndaelManaged(); 13 14 rijndaelCipher.Mode = CipherMode.CBC; 15 16 rijndaelCipher.Padding = PaddingMode.PKCS7; 17 18 rijndaelCipher.KeySize = 128; 19 20 rijndaelCipher.BlockSize = 128; 21 22 byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key); 23 24 byte[] keyBytes = new byte[16]; 25 26 int len = pwdBytes.Length; 27 28 if (len > keyBytes.Length) len = keyBytes.Length; 29 30 System.Array.Copy(pwdBytes, keyBytes, len); 31 32 rijndaelCipher.Key = keyBytes; 33 34 35 byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv); 36 rijndaelCipher.IV = ivBytes; 37 38 ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); 39 40 byte[] plainText = Encoding.UTF8.GetBytes(text); 41 42 byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length); 43 44 return Convert.ToBase64String(cipherBytes); 45 46 } 47 48 /// <summary> 49 /// 随机生成密钥 50 /// </summary> 51 /// <returns></returns> 52 public static string GetIv(int n) 53 { 54 char[] arrChar = new char[]{ 55 'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x', 56 '0','1','2','3','4','5','6','7','8','9', 57 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z' 58 }; 59 60 StringBuilder num = new StringBuilder(); 61 62 Random rnd = new Random(DateTime.Now.Millisecond); 63 for (int i = 0; i < n; i++) 64 { 65 num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString()); 66 67 } 68 69 return num.ToString(); 70 } 71 72 /// <summary> 73 /// AES解密 74 /// </summary> 75 /// <param name="text"></param> 76 /// <param name="key"></param> 77 /// <param name="iv"></param> 78 /// <returns></returns> 79 public static string AESDecrypt(string text, string key, string iv) 80 { 81 RijndaelManaged rijndaelCipher = new RijndaelManaged(); 82 83 rijndaelCipher.Mode = CipherMode.CBC; 84 85 rijndaelCipher.Padding = PaddingMode.PKCS7; 86 87 rijndaelCipher.KeySize = 128; 88 89 rijndaelCipher.BlockSize = 128; 90 91 byte[] encryptedData = Convert.FromBase64String(text); 92 93 byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key); 94 95 byte[] keyBytes = new byte[16]; 96 97 int len = pwdBytes.Length; 98 99 if (len > keyBytes.Length) len = keyBytes.Length; 100 101 System.Array.Copy(pwdBytes, keyBytes, len); 102 103 rijndaelCipher.Key = keyBytes; 104 105 byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv); 106 rijndaelCipher.IV = ivBytes; 107 108 ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); 109 110 byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); 111 112 return Encoding.UTF8.GetString(plainText); 113 114 } 115 }