DESCoderUtil加密解密工具类

1、代码

package com.ra.util;

import java.security.Key;
import java.security.SecureRandom;
 


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.lang3.Validate;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
 
/**
 * DES安全编码组件
 * 
 * 
 * 支持 DES、DESede(TripleDES,就是3DES)、AES、Blowfish、RC2、RC4(ARCFOUR)
 * DES          		key size must be equal to 56
 * DESede(TripleDES) 	key size must be equal to 112 or 168
 * AES          		key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
 * Blowfish     		key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
 * RC2          		key size must be between 40 and 1024 bits
 * RC4(ARCFOUR) 		key size must be between 40 and 1024 bits
 * 具体内容 需要关注 JDK Document http://.../docs/technotes/guides/security/SunProviders.html
 * 
* * @author * @version 1.0 * @since 1.0 */ public abstract class DESCoderUtil { /** * ALGORITHM 算法
* 可替换为以下任意一种算法,同时key值的size相应改变。 * *
	 * DES          		key size must be equal to 56
	 * DESede(TripleDES) 	key size must be equal to 112 or 168
	 * AES          		key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
	 * Blowfish     		key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
	 * RC2          		key size must be between 40 and 1024 bits
	 * RC4(ARCFOUR) 		key size must be between 40 and 1024 bits
	 * 
* * 在Key toKey(byte[] key)方法中使用下述代码 * SecretKey secretKey = new SecretKeySpec(key, ALGORITHM); 替换 * * DESKeySpec dks = new DESKeySpec(key); * SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); * SecretKey secretKey = keyFactory.generateSecret(dks); * */ public static final String ALGORITHM = "DES"; private static SecureRandom random = new SecureRandom(); /** * BASE64解密 * * @param key * @return * @throws Exception */ public static byte[] decryptBASE64(String key) throws Exception { return (new BASE64Decoder()).decodeBuffer(key); } /** * BASE64加密 * * @param key * @return * @throws Exception */ public static String encryptBASE64(byte[] key) throws Exception { return (new BASE64Encoder()).encodeBuffer(key); } /** * 生成随机的Byte[]作为salt * @param numBytes * @return */ public static byte[] generateSalt(int numBytes) { Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes); byte[] bytes = new byte[numBytes]; random.nextBytes(bytes); return bytes; } /** * 转换密钥
* * @param key * @return * @throws Exception */ private static Key toKey(byte[] key) throws Exception { DESKeySpec dks = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM); SecretKey secretKey = keyFactory.generateSecret(dks); // 当使用其他对称加密算法时,如AES、Blowfish等算法时,用下述代码替换上述三行代码 // SecretKey secretKey = new SecretKeySpec(key, ALGORITHM); return secretKey; } /** * 解密 * * @param data * @param key * @return * @throws Exception */ public static byte[] decrypt(byte[] data, String key) throws Exception { Key k = toKey(decryptBASE64(key)); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, k); return cipher.doFinal(data); } /** * 加密 * * @param data * @param key * @return * @throws Exception */ public static byte[] encrypt(byte[] data, String key) throws Exception { Key k = toKey(decryptBASE64(key)); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, k); return cipher.doFinal(data); } /** * 生成密钥 * * @return * @throws Exception */ public static String initKey() throws Exception { return initKey(null); } /** * 生成密钥 * * @param seed * @return * @throws Exception */ public static String initKey(String seed) throws Exception { SecureRandom secureRandom = null; if (seed != null) { secureRandom = new SecureRandom(decryptBASE64(seed)); } else { secureRandom = new SecureRandom(); } KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM); kg.init(secureRandom); SecretKey secretKey = kg.generateKey(); return encryptBASE64(secretKey.getEncoded()); } }

2、测试

package com.ra.util;


import org.junit.Test;

public class DESCoderTest {

	/**
	 * 加密
	 * @throws Exception
	 */
	@Test
	public void test() throws Exception {
		byte[] generateSalt = DESCoderUtil.generateSalt(8);
		String salt = new String(generateSalt);
		System.out.println("盐:\t" + salt);
		
		String base64 = DESCoderUtil.encryptBASE64(salt.getBytes());
		System.out.println(base64);
		
		String inputStr = "DES";
		String key = DESCoderUtil.initKey(salt);
		System.err.println("原文:\t" + inputStr);
 
		System.err.println("密钥:\t" + key);
		byte[] inputData = inputStr.getBytes();
		
		inputData = DESCoderUtil.encrypt(inputData, key);
		System.out.println(inputData);
		System.err.println("加密后:\t" + DESCoderUtil.encryptBASE64(inputData));
 
		/*assertEquals(inputStr, outputStr);*/
	}

	/**
	 * 解密
	 * @throws Exception
	 */
	@Test
	public void test2() throws Exception {
		byte[] base64 = DESCoderUtil.decryptBASE64("W0JAMmZiNGI2OWQ=");
		System.out.println(new String(base64));
		
		String key = DESCoderUtil.initKey(new String(base64));
 
		System.err.println("密钥:\t" + key);
		
		String coder = "MXs1cE0CUHY=";//加密后代码
		
		byte[] bs = DESCoderUtil.decryptBASE64(coder);
		System.out.println(new String(bs));
		byte[] outputData = DESCoderUtil.decrypt(bs, key);
		String outputStr = new String(outputData);
 
		System.err.println("解密后:\t" + outputStr);
 
		/*assertEquals(inputStr, outputStr);*/
	}
}

 

你可能感兴趣的:(工具类)