一、DES算法
设置算法及参数
//算法名称
public static final String KEY_ALGORITHM = "DES";
//算法名称/加密模式/填充方式
public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
生成密钥
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
keyGenerator.init(56);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
还原密钥
private static Key toKey(byte[] key) {
DESKeySpec des = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(des);
return secretKey;
}
加密
public static byte[] encrypt(byte[] data ,byte[] key) {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal();
}
解密
Key k = toKey(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal();
注:加密和解密的区别在于init初始化时模式
二、三重DES---DESede
其过程同上只需把变声明算法改变一下,Java6支持的密钥长度为112或168
//算法名称
public static final String KEY_ALGORITHM = "DESede";
//算法名称/加密模式/填充方式
public static final String CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";
还原密钥
DESedeKeySpec des = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(des);
三、AES算法
同DES基本一致。
public static final String KEY_ALGORITHM = "AES";
public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
private static Key toKey(byte[] key){
SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
return secretKey;
}
四、IDEA 同AES算法一致。
public static final String KEY_ALGORITHM = "IDEA";
public static final String CIPHER_ALGORITHM = "IDEA/ECB/PKCS5Padding";
private static Key toKey(byte[] key){
SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
return secretKey;
}
五、基本口令加密---PBE
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
public class PBECode {
public static final String ALGORITHM = "PBEWITHMD5andDES";
//迭代次数
public static final int ITERATION_COUNT = 100;
/**
* 盐初始化<br>
* 盐长度必须为8字节
* @return byte[] 盐
*/
public static byte[] initSalt(){
//实例化安全随机数
SecureRandom random = new SecureRandom();
//生产盐
return random.generateSeed(8);
}
/**
* 转换密钥
* @param password 密码
* @return key 密钥
* @throws Exception
*/
private static Key toKey(String password)throws Exception{
//密钥材料转换
PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
//实例化
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(keySpec);
return secretKey;
}
/**
* 加密
* @param data 数据
* @param password 密码
* @param salt 盐
* @return byte[] 加密数据
* @throws Exception
*/
public static byte[] encrypt(byte[] data,String password,byte[] salt) throws Exception{
Key key = toKey(password);
//实例化PBE参数材料
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, ITERATION_COUNT);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key,parameterSpec);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data, String password,byte[] salt) throws Exception{
Key key = toKey(password);
//实例化PBE参数材料
PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, ITERATION_COUNT);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key,parameterSpec);
return cipher.doFinal(data);
}
}