AES加密及MD5加密

AES加密


 private static string AES_KEY = "80f4kwSR45gY4rPU6YWxnIbqKT87lF93"; //传入加密方法中
 private static string AES_IV = ""; //传入加密方法中
 /// 
 /// AES加密
 /// 
 /// 明文
 /// 密钥
 /// 矢量,暂时无用
 /// 返回BASE64的密文
 private static string AesEncrypt(string msg,string key,string iv) {
            string encryptMsg = "";
            byte[] byteKey = Encoding.UTF8.GetBytes(key);
            //byte[] byteIv = Encoding.UTF8.GetBytes(iv);
            byte[] encrypted;
            using (Aes aes = Aes.Create()) {
                aes.Key = byteKey;
                //aes.IV = byteIv;
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;
                ICryptoTransform encryptor = aes.CreateEncryptor();
                using (MemoryStream msEncrypt = new MemoryStream()) {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, 
                    encryptor, CryptoStreamMode.Write)) {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)){
                            swEncrypt.Write(msg);
                        }
                        encrypted = msEncrypt.ToArray();
                        encryptMsg = Convert.ToBase64String(encrypted);
                    }
                }
            }
            return encryptMsg;
        }

AES解密


 private static string AES_KEY = "80f4kwSR45gY4rPU6YWxnIbqKT87lF93"; //传入解密方法中
 private static string AES_IV = ""; //传入解密方法中
 /// 
 /// AES解密
 /// 
 /// 密文,格式需要BASE64的格式
 /// 密钥
 /// 矢量,暂时无用
 /// 解密后的明文
 private static string AesDecrypt(string msg, string key, string iv)
        {
            string dncryptMsg = "";
            byte[] byteKey = Encoding.UTF8.GetBytes(key);
            //byte[] byteIv = Encoding.UTF8.GetBytes(iv);
            byte[] byteMsg = Convert.FromBase64String(msg) ;
            using (Aes aes = Aes.Create())
            {
                aes.Key = byteKey;
                //aes.IV = byteIv;
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.PKCS7;
                ICryptoTransform decryptor = aes.CreateDecryptor();
                
                using (MemoryStream msDecrypt = new MemoryStream(byteMsg))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
                    decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            dncryptMsg = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            return dncryptMsg;
        }

MD5加密


/// 
/// MD5加密
/// 
/// 明文
/// 密文
 private static string Md5Encrypt(string msg) {
            string encryptMsg = "";
            MD5 md5 = MD5.Create();
            byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(msg));
            //BitConverter.ToString(bytes,4,8);生成的是16位密文,不使用ToLower()则是大写
            encryptMsg = BitConverter.ToString(bytes).Replace("-","").ToLower();
            return encryptMsg;
        }

你可能感兴趣的:(c#)