JAVA加密解密之3DES(Triple DES)

3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法。由于计算机运算能力的增强,原版DES密码的密钥长度变得容易被暴力破解;3DES即是设计用来提供一种相对简单的方法,即通过增加DES的密钥长度来避免类似的攻击,而不是设计一种全新的块密码算法。

import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/** * DES加密解密 * * @author jianggujin * */
public class DESedeCoder {
   public static final String PADDING_PKCS5Padding = "PKCS5Padding";
   public static final String PADDING_NoPadding = "NoPadding";
   public static final String WORKING_MODE_CBC = "CBC";
   public static final String WORKING_MODE_ECB = "ECB";
   private static final String KEY_ALGORITHM = "DESede";

   /** * 加密 * * @param data * @param key * @param iv * @return * @throws Exception */
   public byte[] encrypt(byte[] data, byte[] key, byte[] iv) throws Exception
   {
      return encrypt(data, key, iv, WORKING_MODE_CBC, PADDING_PKCS5Padding);
   }

   /** * 加密 * * @param data * @param key * @return * @throws Exception */
   public byte[] encrypt(byte[] data, byte[] key) throws Exception
   {
      return encrypt(data, key, initIv(), WORKING_MODE_CBC,
            PADDING_PKCS5Padding);
   }

   /** * 加密 * * @param data * @param key * @param iv * @param workingMode * @param padding * @return * @throws Exception */
   public byte[] encrypt(byte[] data, byte[] key, byte[] iv,
         String workingMode, String padding) throws Exception
   {
      String fullAlg = KEY_ALGORITHM + "/" + workingMode + "/" + padding;

      Cipher cipher = Cipher.getInstance(fullAlg);
      SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);

      if (WORKING_MODE_CBC.equals(workingMode))
      {
         IvParameterSpec ivSpec = new IvParameterSpec(iv);
         cipher.init(1, secretKey, ivSpec);
      }
      else
      {
         cipher.init(1, secretKey);
      }
      return cipher.doFinal(data);
   }

   /** * 解密 * * @param data * @param key * @param iv * @return * @throws Exception */
   public byte[] decrypt(byte[] data, byte[] key, byte[] iv) throws Exception
   {
      return decrypt(data, key, iv, WORKING_MODE_CBC, PADDING_PKCS5Padding);
   }

   /** * 解密 * * @param data * @param key * @return * @throws Exception */
   public byte[] decrypt(byte[] data, byte[] key) throws Exception
   {
      return decrypt(data, key, initIv(), WORKING_MODE_CBC,
            PADDING_PKCS5Padding);
   }

   /** * 解密 * * @param data * @param key * @param iv * @param workingMode * @param padding * @return * @throws Exception */
   public byte[] decrypt(byte[] data, byte[] key, byte[] iv,
         String workingMode, String padding) throws Exception
   {
      String fullAlg = KEY_ALGORITHM + "/" + workingMode + "/" + padding;

      Cipher cipher = Cipher.getInstance(fullAlg);
      SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);

      if (WORKING_MODE_CBC.equals(workingMode))
      {
         IvParameterSpec ivSpec = new IvParameterSpec(iv);
         cipher.init(2, secretKey, ivSpec);
      }
      else
      {
         cipher.init(2, secretKey);
      }
      return cipher.doFinal(data);
   }

   /** * 初始化密钥 * * @param workingMode * @param padding * @return * @throws NoSuchAlgorithmException */
   public byte[] initKey(String workingMode, String padding)
         throws NoSuchAlgorithmException
   {
      return initKey(workingMode, padding, null);
   }

   /** * 初始化密钥 * * @param workingMode * @param padding * @param seed * @return * @throws NoSuchAlgorithmException */
   public byte[] initKey(String workingMode, String padding, byte[] seed)
         throws NoSuchAlgorithmException
   {
      SecureRandom secureRandom = null;

      if (seed != null)
      {
         secureRandom = new SecureRandom(seed);
      }
      else
      {
         secureRandom = new SecureRandom();
      }

      KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM + "/"
            + workingMode + "/" + padding);
      kg.init(secureRandom);

      return kg.generateKey().getEncoded();
   }

   /** * 初始化向量 * * @return * @throws GeneralSecurityException */
   public byte[] initIv() throws GeneralSecurityException
   {
      return initIv(PADDING_PKCS5Padding);
   }

   /** * 初始化向量 * * @param padding * @return * @throws GeneralSecurityException */
   public byte[] initIv(String padding) throws GeneralSecurityException
   {
      Cipher cipher = Cipher.getInstance(KEY_ALGORITHM + "/" + WORKING_MODE_CBC
            + "/" + padding);
      int blockSize = cipher.getBlockSize();
      byte[] iv = new byte[blockSize];
      for (int i = 0; i < blockSize; i++)
      {
         iv[i] = 0;
      }
      return iv;
   }
}

你可能感兴趣的:(java,加密,解密,3des,Triple)