C# 3Des两种加密方式 (对应java中的desede/CBC/PKCS5Padding加密)

1、3Des两种加密方式

 

    //3DES的cbc加密[24位密钥对应192位加密]
        public static string TripleDesEncryptorCBC(string text, string key, string iv)
        {
            var tripleDESCipher = new TripleDESCryptoServiceProvider();
            tripleDESCipher.Mode = CipherMode.CBC;
            tripleDESCipher.Padding = PaddingMode.PKCS7;
            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);
            byte[] keyBytes = new byte[24];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length)
                len = keyBytes.Length;
            System.Array.Copy(pwdBytes, keyBytes, len);
            tripleDESCipher.Key = keyBytes;
            tripleDESCipher.IV = Encoding.ASCII.GetBytes(iv);
 
            ICryptoTransform transform = tripleDESCipher.CreateEncryptor();
            byte[] plainText = Encoding.UTF8.GetBytes(text);
            byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
            return Convert.ToBase64String(cipherBytes);
        }
 
        //3DES的cbc解密
        public static string TripleDesDecryptorCBC(string text, string key, string iv)
        {
            var tripleDESCipher = new TripleDESCryptoServiceProvider();
            tripleDESCipher.Mode = CipherMode.CBC;
            tripleDESCipher.Padding = PaddingMode.PKCS7;
 
            byte[] encryptedData = Convert.FromBase64String(text);
            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(key);
            byte[] keyBytes = new byte[24];
            byte[] ivBytes = Encoding.ASCII.GetBytes(iv);
            int len = pwdBytes.Length;
            if (len > keyBytes.Length)
                len = keyBytes.Length;
            System.Array.Copy(pwdBytes, keyBytes, len);
            tripleDESCipher.Key = keyBytes;
            tripleDESCipher.IV = ivBytes;
            ICryptoTransform transform = tripleDESCipher.CreateDecryptor();
            byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
            return Encoding.UTF8.GetString(plainText);
        }
 
 
        //加密
        public string DESEncrypt(string paymentCode, string key, string iv)
        {
            SymmetricAlgorithm symmetric;
            ICryptoTransform iCrypto;
            MemoryStream memory;
            CryptoStream crypto;
            byte[] byt;
            symmetric = new TripleDESCryptoServiceProvider();
            symmetric.Key = Encoding.UTF8.GetBytes(key);
            symmetric.IV = Encoding.UTF8.GetBytes(iv);
            iCrypto = symmetric.CreateEncryptor();
            byt = Encoding.UTF8.GetBytes(paymentCode);
            memory = new MemoryStream();
            crypto = new CryptoStream(memory, iCrypto, CryptoStreamMode.Write);
            crypto.Write(byt, 0, byt.Length);
            crypto.FlushFinalBlock();
            crypto.Close();
            return Convert.ToBase64String(memory.ToArray());
        }
 
        //解密
        public static string DESDecrypst(string data, string key, string iv)
        {
            SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
 
            mCSP.Key = Encoding.UTF8.GetBytes(key);
            mCSP.IV = Encoding.UTF8.GetBytes(iv);
            ICryptoTransform iCrypto;
            MemoryStream memory;
            CryptoStream crypto;
            byte[] byt;
            iCrypto = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
 
            byt = Convert.FromBase64String(data);
            memory = new MemoryStream();
            crypto = new CryptoStream(memory, iCrypto, CryptoStreamMode.Write);
            crypto.Write(byt, 0, byt.Length);
            crypto.FlushFinalBlock();
            crypto.Close();
            return Encoding.UTF8.GetString(memory.ToArray());
        }

 

posted @ 2020-04-04 16:35  kevin860  阅读(1987)  评论(0编辑  收藏  举报