TripleDES说明:
TripleDES(3Des)和Des都是对称加密算法,TripleDes是Des加密算法的增强版本,这里主要说的是TripleDes加密算法的应用。
工作中由于涉及到不同系统的对接,整理了Java、C#和php通用的算法代码,直接看代码:
Java版本:
package com.jaamy.common.util; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; public class DefaultEncryptor { private static final String ALGORITHM = "DESede"; //默认为 DESede/ECB/PKCS5Padding private static final String CIPHER_TRANSFORMAT = "DESede/ECB/PKCS5Padding"; private static final String ENCODING = "UTF-8"; public static String encryptToBase64(String plainText, String key) throws Exception { SecretKey deskey = new SecretKeySpec(key.getBytes(), ALGORITHM); Cipher c1 = Cipher.getInstance(CIPHER_TRANSFORMAT); c1.init(Cipher.ENCRYPT_MODE, deskey); byte[] result = c1.doFinal(plainText.getBytes(ENCODING)); return Base64.encodeBase64String(result); } public static String decryptFromBase64(String base64, String key) throws Exception { SecretKey deskey = new SecretKeySpec(key.getBytes(), ALGORITHM); Cipher c1 = Cipher.getInstance(CIPHER_TRANSFORMAT); c1.init(Cipher.DECRYPT_MODE, deskey); byte[] result = c1.doFinal(Base64.decodeBase64(base64)); return new String(result, ENCODING); } }
C#代码:
using System; using System.IO; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Web; namespace DesEncryptor { class DefaultEncryptor { #region DES 加密 public static string encryptToBase64(string plainText, string key) { try { // Create a MemoryStream. MemoryStream mStream = new MemoryStream(); TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider(); tripleDESCryptoServiceProvider.Key = Encoding.ASCII.GetBytes(key); tripleDESCryptoServiceProvider.Padding = PaddingMode.PKCS7;//补位 tripleDESCryptoServiceProvider.Mode = CipherMode.ECB;//CipherMode.CBC CryptoStream cStream = new CryptoStream(mStream, tripleDESCryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Write); // Convert the passed string to a byte array. byte[] toEncrypt = Encoding.UTF8.GetBytes(plainText); // Write the byte array to the crypto stream and flush it. cStream.Write(toEncrypt, 0, toEncrypt.Length); cStream.FlushFinalBlock(); // Get an array of bytes from the // MemoryStream that holds the // encrypted data. byte[] ret = mStream.ToArray(); // Close the streams. cStream.Close(); mStream.Close(); // Return the encrypted buffer. return Convert.ToBase64String(ret); } catch (CryptographicException e) { Console.WriteLine("A Cryptographic error occurred: {0}", e.Message); return null; } } #endregion #region DES解密 public static string decryptFromBase64(string base64, string key) { try { byte[] inputByteArray = Convert.FromBase64String(base64); // Create a new MemoryStream using the passed // array of encrypted data. MemoryStream msDecrypt = new MemoryStream(inputByteArray); // Create a CryptoStream using the MemoryStream TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider(); tripleDESCryptoServiceProvider.Key = ASCIIEncoding.ASCII.GetBytes(key); tripleDESCryptoServiceProvider.Padding = PaddingMode.PKCS7;//补位 tripleDESCryptoServiceProvider.Mode = CipherMode.ECB;//CipherMode.CBC CryptoStream csDecrypt = new CryptoStream(msDecrypt, tripleDESCryptoServiceProvider.CreateDecryptor(), CryptoStreamMode.Read); // Create buffer to hold the decrypted data. byte[] fromEncrypt = new byte[inputByteArray.Length]; // Read the decrypted data out of the crypto stream // and place it into the temporary buffer. csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); //Convert the buffer into a string and return it. return Encoding.UTF8.GetString(fromEncrypt).TrimEnd('