JAVA加密解密之对称加密

写的一个JAVA对称加密的工具类,支持DES、DESede、AES、Blowfish、RC2、RC4的加密解密。

DES全称为Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法,1976年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),随后在国际上广泛流传开来。

DESede是由DES对称加密算法改进后的一种对称加密算法。使用 168 位的密钥对资料进行三次加密的一种机制;它通常(但非始终)提供极其强大的安全性。如果三个 56 位的子元素都相同,则三重 DES 向后兼容 DES。

AES,高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。

Blowfish算法是一个64位分组及可变密钥长度的对称密钥分组密码算法,可用来加密64比特长度的字符串。32位处理器诞生后,Blowfish算法因其在加密速度上超越了DES而引起人们的关注。Blowfish算法具有加密速度快、紧凑、密钥长度可变、可免费使用等特点,已被广泛使用于众多加密软件。

RC2是由著名密码学家Ron Rivest设计的一种传统对称分组加密算法,它可作为DES算法的建议替代算法。它的输入和输出都是64比特。密钥的长度是从1字节到128字节可变,但目前的实现是8字节(1998年)。

RC4加密算法是大名鼎鼎的RSA三人组中的头号人物Ronald Rivest在1987年设计的密钥长度可变的流加密算法簇。之所以称其为簇,是由于其核心部分的S-box长度可为任意,但一般为256字节。该算法的速度可以达到DES加密的10倍左右,且具有很高级别的非线性。RC4起初是用于保护商业机密的。但是在1994年9月,它的算法被发布在互联网上,也就不再有什么商业机密了。RC4也被叫做ARC4(Alleged RC4——所谓的RC4),因为RSA从来就没有正式发布过这个算法。

import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;

/** * 对称加密 * * @author jianggujin * */
public class SymmetricalCoder {
   public static final String ALGORITHM_DES = "DES";
   public static final String ALGORITHM_DESede = "DESede";
   public static final String ALGORITHM_AES = "AES";
   public static final String ALGORITHM_Blowfish = "Blowfish";
   public static final String ALGORITHM_RC2 = "RC2";
   public static final String ALGORITHM_RC4 = "RC4";

   private Key toKey(String algorithm, byte[] key) throws InvalidKeyException,
         InvalidKeySpecException, NoSuchAlgorithmException
   {
      if (ALGORITHM_DES.equals(algorithm))
      {
         DESKeySpec dks = new DESKeySpec(key);
         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
         SecretKey secretKey = keyFactory.generateSecret(dks);
         return secretKey;
      }

      return new SecretKeySpec(key, algorithm);
   }

   /** * 解密 * * @param algorithm * @param data * @param key * @return * @throws InvalidKeyException * @throws InvalidKeySpecException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws IllegalBlockSizeException * @throws BadPaddingException */
   public byte[] decrypt(String algorithm, byte[] data, byte[] key)
         throws InvalidKeyException, InvalidKeySpecException,
         NoSuchAlgorithmException, NoSuchPaddingException,
         IllegalBlockSizeException, BadPaddingException
   {
      Key k = toKey(algorithm, key);

      Cipher cipher = Cipher.getInstance(algorithm.toString());
      cipher.init(2, k);

      return cipher.doFinal(data);
   }

   /** * 加密 * * @param algorithm * @param data * @param key * @return * @throws InvalidKeyException * @throws InvalidKeySpecException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws IllegalBlockSizeException * @throws GeneralSecurityException */
   public byte[] encrypt(String algorithm, byte[] data, byte[] key)
         throws InvalidKeyException, InvalidKeySpecException,
         NoSuchAlgorithmException, NoSuchPaddingException,
         IllegalBlockSizeException, GeneralSecurityException
   {
      Key k = toKey(algorithm, key);
      Cipher cipher = Cipher.getInstance(algorithm.toString());
      cipher.init(1, k);

      return cipher.doFinal(data);
   }

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

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

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

      KeyGenerator kg = KeyGenerator.getInstance(algorithm.toString());
      kg.init(secureRandom);

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

   public static void main(String[] args) throws Exception
   {
      String algorithm = ALGORITHM_RC4;
      byte[] data = "jianggujin".getBytes();
      SymmetricalCoder coder = new SymmetricalCoder();
      byte[] key = coder.initKey(algorithm);
      byte[] result = coder.encrypt(algorithm, data, key);
      System.out.println(new String(coder.decrypt(algorithm, result, key)));
   }
}

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