C#完全實現AES算法加密解密函數
C#完全實現AES算法加密解密函數
/// <summary> /// Aes加密、解密,密鑰長度256,密鑰長度不大于32個字節 /// </summary> public class AesCryptoHelper { public const string RET_ERROR = "x07x07x07x07x07"; private const string CRYPTO_IV = "XIANJIAN"; private const string CRYPTO_KEY = "XIANJIAN"; private const int CRYPTO_KEY_LENGTH = 32; private const int CRYPTO_IV_LENGTH = 16;private AesCryptoServiceProvider m_aesCryptoServiceProvider; private string m_message; public string Message { get { return m_message; } set { m_message = value; } } private bool m_containKey; /// <summary> /// True:密文中包含密鑰 /// False:密文中不包含密鑰 /// </summary> public bool ContainKey { get { return m_containKey; } set { m_containKey = value; } } public AesCryptoHelper() { m_aesCryptoServiceProvider = new AesCryptoServiceProvider(); m_containKey = true; m_message = string.Empty; } public AesCryptoHelper(bool containKey) : this() { m_containKey = containKey; } private string Encrypt(string s_crypto, byte[] key, byte[] iv) { string s_encryped = string.Empty; byte[] crypto, encrypted; ICryptoTransform ct; try { crypto = string2Byte(s_crypto); m_aesCryptoServiceProvider.Key = key; m_aesCryptoServiceProvider.IV = iv; ct = m_aesCryptoServiceProvider.CreateEncryptor(); encrypted = ct.TransformFinalBlock(crypto, 0, crypto.Length); if (m_containKey) { s_encryped += byte2HexString(key); } s_encryped += byte2HexString(encrypted); return s_encryped; } catch (Exception ex) { m_message = ex.ToString(); return RET_ERROR; } } /// <summary> /// 指定密鑰對明文進行AES加密 /// </summary> /// <param name="s_crypto">明文</param> /// <param name="s_key">加密密鑰</param> /// <returns></returns> public string Encrypt(string s_crypto, string s_key) { byte[] key = new byte[CRYPTO_KEY_LENGTH], iv = new byte[CRYPTO_IV_LENGTH]; byte[] temp = string2Byte(s_key); if (temp.Length > key.Length) { m_message = "Key too long,need less than 32 Bytes key."; return RET_ERROR; } key = string2Byte(s_key.PadRight(key.Length)); iv = string2Byte(CRYPTO_IV.PadRight(iv.Length)); return Encrypt(s_crypto, key, iv); } /// <summary> /// 動態生成密鑰,并對明文進行AES加密 /// </summary> /// <param name="s_crypto">明文</param> /// <returns></returns> public string Encrypt(string s_crypto) { byte[] key = new byte[CRYPTO_KEY_LENGTH], iv = new byte[CRYPTO_IV_LENGTH]; m_aesCryptoServiceProvider.GenerateKey(); key = m_aesCryptoServiceProvider.Key; iv = string2Byte(CRYPTO_IV.PadRight(iv.Length)); return Encrypt(s_crypto, key, iv); } private string Decrypt(string s_encrypted, byte[] key, byte[] iv) { string s_decrypted = string.Empty; byte[] encrypted, decrypted; ICryptoTransform ct; try { encrypted = hexString2Byte(s_encrypted); m_aesCryptoServiceProvider.Key = key; m_aesCryptoServiceProvider.IV = iv; ct = m_aesCryptoServiceProvider.CreateDecryptor(); decrypted = ct.TransformFinalBlock(encrypted, 0, encrypted.Length); s_decrypted += byte2String(decrypted); return s_decrypted; } catch (Exception ex) { m_message = ex.ToString(); m_message = "Decrypt fail."; return RET_ERROR; } } /// <summary> /// 從密文中解析出密鑰,并對密文進行解密 /// </summary> /// <param name="s_encrypted">密文</param> /// <returns></returns> public string Decrypt(string s_encrypted) { string s_key = string.Empty; byte[] key = new byte[CRYPTO_KEY_LENGTH], iv = new byte[CRYPTO_IV_LENGTH]; if (s_encrypted.Length <= CRYPTO_KEY_LENGTH * 2) { m_message = "Encrypted string invalid."; return RET_ERROR; } if (m_containKey) { s_key = s_encrypted.Substring(0, CRYPTO_KEY_LENGTH * 2); s_encrypted = s_encrypted.Substring(CRYPTO_KEY_LENGTH * 2); } key = hexString2Byte(s_key); iv = string2Byte(CRYPTO_IV.PadRight(iv.Length)); return Decrypt(s_encrypted, key, iv); } /// <summary> /// 指定密鑰,并對密文進行解密 /// </summary> /// <param name="s_encrypted">密文</param> /// <param name="s_key">密鑰</param> /// <returns></returns> public string Decrypt(string s_encrypted, string s_key) { byte[] key = new byte[CRYPTO_KEY_LENGTH], iv = new byte[CRYPTO_IV_LENGTH]; byte[] temp = string2Byte(s_key); if (temp.Length > key.Length) { m_message = "Key invalid.too long,need less than 32 Bytes"; return RET_ERROR; } key = string2Byte(s_key.PadRight(key.Length)); iv = string2Byte(CRYPTO_IV.PadRight(iv.Length)); if (m_containKey) { s_encrypted = s_encrypted.Substring(CRYPTO_KEY_LENGTH * 2); } return Decrypt(s_encrypted, key, iv); } private string byte2HexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); foreach (byte b in bytes) { sb.AppendFormat("{0:X2}", b); } return sb.ToString(); } private byte[] hexString2Byte(string hex) { int len = hex.Length / 2; byte[] bytes = new byte[len]; for (int i = 0; i < len; i++) { bytes[i] = (byte)(Convert.ToInt32(hex.Substring(i * 2, 2), 16)); } return bytes; } private byte[] string2Byte(string str) { return Encoding.UTF8.GetBytes(str); } private string byte2String(byte[] bytes) { return Encoding.UTF8.GetString(bytes); }
}</pre>
本文由用戶 mb78 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!