Java加解密艺术之DESede对称加密算法

package com.jadyer.util.codec;

import java.security.Key;
import java.security.NoSuchAlgorithmException;

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

import org.apache.commons.codec.binary.Base64;

/**
 * DESede对称加密算法
 * @see DESede也就是所谓的Triple-DES,俗称3DES
 * @see 关于代码注释,请参见我的前一篇文章http://blog.csdn.net/jadyer/article/details/7615951
 */
public class DESedeCodec {
	public static final String KEY_ALGORITHM = "DESede";
	public static final String CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";
	
	/**
	 * 生成密钥
	 * @see Java6只支持56位密钥
	 * @see BouncyCastle支持64位密钥,官网是http://www.bouncycastle.org/
	 */
	public static String initkey() throws NoSuchAlgorithmException {
		KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
		kg.init(168);
		SecretKey secretKey = kg.generateKey();
		return Base64.encodeBase64String(secretKey.getEncoded());
	}

	/**
	 * 转换密钥
	 */
	private static Key toKey(byte[] key) throws Exception {
		DESedeKeySpec dks = new DESedeKeySpec(key);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
		SecretKey secretKey = keyFactory.generateSecret(dks);
		return secretKey;
	}
	
	/**
	 * 加密数据
	 * @param data 待加密数据
	 * @param key  密钥
	 * @return 加密后的数据
	 */
	public static String encrypt(String data, String key) throws Exception {
		Key k = toKey(Base64.decodeBase64(key));
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
		cipher.init(Cipher.ENCRYPT_MODE, k);
		return Base64.encodeBase64String(cipher.doFinal(data.getBytes()));
	}
	
	/**
	 * 解密数据
	 * @param data 待解密数据
	 * @param key  密钥
	 * @return 解密后的数据
	 */
	public static String decrypt(String data, String key) throws Exception {
		Key k = toKey(Base64.decodeBase64(key));
		Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
		cipher.init(Cipher.DECRYPT_MODE, k);
		return new String(cipher.doFinal(Base64.decodeBase64(data)));
	}
	
	public static void main(String[] args) throws Exception {
		String source = "岂向苍天";
		System.out.println("原文: " + source);
		
		String key = initkey();
		System.out.println("密钥: " + key);
		
		String encryptData = encrypt(source, key);
		System.out.println("加密: " + encryptData);
		
		String decryptData = decrypt(encryptData, key);
		System.out.println("解密: " + decryptData);
	}
}

你可能感兴趣的:(java,Algorithm,加密,exception,String,解密)