Java版
public static void main(String[] args) throws Exception { byte[] text = "test".getBytes(); //待加/解密的數據 //密鑰數據 byte[] keyData = Base64.decode("Uqt3frmnmVQQgU7S4wUJnKBrQ0CypPii"); String algorithm = "DESede"; //算法名稱 String fullAlg = algorithm + "/CBC/PKCS5Padding"; Cipher cipher1 = null; try { cipher1 = Cipher.getInstance(fullAlg); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } int blockSize = cipher1.getBlockSize(); byte[] iv = new byte[blockSize]; for (int i = 0; i < blockSize; ++i) { iv[i] = 0; } System.out.println("iv==>"+new String(iv)+"<=="); Cipher cipher = Cipher.getInstance(fullAlg); SecretKey secretKey = new SecretKeySpec(keyData, algorithm); IvParameterSpec ivSpec = new IvParameterSpec(iv); /**加密*/ cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec); byte[] cipherBytes = cipher.doFinal(text); String cipherString = Base64.encode(cipherBytes); System.out.println("加密後==>" + cipherString); /**解密*/ cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec); byte[] resultBytes = cipher.doFinal(Base64.decode(cipherString)); System.out.println("解密==>" + new String(resultBytes)); }
C#版
/// <summary> /// 3des加密IV /// </summary> private static readonly byte[] TripleDesIV = { 0, 0, 0, 0, 0, 0, 0, 0 }; /// <summary> /// 3DES的cbc模式加密 /// </summary> /// <param name="text">待加密文本</param> /// <param name="key">密鑰</param> /// <returns></returns> public static string TripleDesEncryptorCBC(string text, string key) { //密鑰 byte[] keyBytes = GetTripleDesKeyByte(key); //加密字符處理 byte[] plainText = Encoding.UTF8.GetBytes(text); //加密 using (var tripleDESCipher = new TripleDESCryptoServiceProvider()) { tripleDESCipher.Mode = CipherMode.CBC; tripleDESCipher.Padding = PaddingMode.PKCS7; tripleDESCipher.Key = keyBytes; tripleDESCipher.IV = TripleDesIV; using (ICryptoTransform transform = tripleDESCipher.CreateEncryptor()) { return Convert.ToBase64String(transform.TransformFinalBlock(plainText, 0, plainText.Length)); } } } /// <summary> /// 3DES的cbc模式解密 /// </summary> /// <param name="text"></param> /// <param name="key"></param> /// <returns></returns> public static string TripleDesDecryptorCBC(string text, string key) { //密鑰 byte[] keyBytes = GetTripleDesKeyByte(key); //加密字符處理 byte[] plainText = Convert.FromBase64String(text); //加密 using (var tripleDESCipher = new TripleDESCryptoServiceProvider()) { tripleDESCipher.Mode = CipherMode.CBC; tripleDESCipher.Padding = PaddingMode.PKCS7; tripleDESCipher.Key = keyBytes; tripleDESCipher.IV = TripleDesIV; using (ICryptoTransform transform = tripleDESCipher.CreateDecryptor()) { return Encoding.UTF8.GetString(transform.TransformFinalBlock(plainText, 0, plainText.Length)); } } } /// <summary> /// 獲取密鑰數組 /// </summary> /// <param name="key">密鑰</param> /// <returns></returns> public static byte[] GetTripleDesKeyByte(string key) { //密鑰處理 byte[] pwdBytes = Convert.FromBase64String(key); byte[] keyBytes = new byte[24]; int len = pwdBytes.Length; if (len > keyBytes.Length) len = keyBytes.Length; Array.Copy(pwdBytes, keyBytes, len); return keyBytes; }
效果圖: