jdk8之后梗醇,sun.misc.BASE64Decoder和sun.misc.BASE64Encoder被移除
從JDK 1.8開始寺滚,就提供了java.util.Base64.Decoder和java.util.Base64.Encoder的JDK公共API,可代替sun.misc.BASE64Decoder和sun.misc.BASE64Encoder的JDK內部API
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
//import java.util.Base64;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class DesUtil {
/*
* 加密 1.構造密鑰生成器 2.根據(jù)ecnodeRules規(guī)則初始化密鑰生成器 3.產生密鑰 4.創(chuàng)建和初始化密碼器 5.內容加密 6.返回字符串
*/
public static String AESEncode(String encodeRules, String content) throws Exception {
GetCipher getCipher = new GetCipher(encodeRules).invoke();
SecretKey key = getCipher.getKey();
Cipher cipher = getCipher.getCipher();
// 7.初始化密碼器熔萧,第一個參數(shù)為加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二個參數(shù)為使用的KEY
cipher.init(Cipher.ENCRYPT_MODE, key);
// 8.獲取加密內容的字節(jié)數(shù)組(這里要設置為utf-8)不然內容中如果有中文和英文混合中文就會解密為亂碼
byte[] byte_encode = content.getBytes("utf-8");
// 9.根據(jù)密碼器的初始化方式--加密:將數(shù)據(jù)加密
byte[] byte_AES = cipher.doFinal(byte_encode);
// 10.將加密后的數(shù)據(jù)轉換為字符串
// 這里用Base64Encoder中會找不到包
// 解決辦法:
// 在項目的Build path中先移除JRE System Library,再添加庫JRE System Library荷科,重新編譯后就一切正常了。
//String AES_encode=new String(new BASE64Encoder().encode(byte_AES));
String AES_encode = new String(Base64.encodeBase64(byte_AES));
// 11.將字符串返回
return AES_encode;
}
/*
* 解密 解密過程: 1.同加密1-4步 2.將加密后的字符串反紡成byte[]數(shù)組 3.將加密內容解密
*/
public static String AESDncode(String encodeRules, String content) throws Exception {
GetCipher getCipher = new GetCipher(encodeRules).invoke();
SecretKey key = getCipher.getKey();
Cipher cipher = getCipher.getCipher();
// 7.初始化密碼器纱注,第一個參數(shù)為加密(Encrypt_mode)或者解密(Decrypt_mode)操作畏浆,第二個參數(shù)為使用的KEY
cipher.init(Cipher.DECRYPT_MODE, key);
// 8.將加密并編碼后的內容解碼成字節(jié)數(shù)組
// byte [] byte_content= new BASE64Decoder().decodeBuffer(content);
// byte[] byte_content = Base64.getDecoder().decode(content);
byte[] byte_content = Base64.decodeBase64(content);
/*
* 解密
*/
byte[] byte_decode = cipher.doFinal(byte_content);
String AES_decode = new String(byte_decode, "utf-8");
return AES_decode;
}
private static class GetCipher {
private String encodeRules;
private SecretKey key;
private Cipher cipher;
public GetCipher(String encodeRules) {
this.encodeRules = encodeRules;
}
public SecretKey getKey() {
return key;
}
public Cipher getCipher() {
return cipher;
}
public GetCipher invoke() throws NoSuchAlgorithmException, NoSuchPaddingException {
// 1.構造密鑰生成器,指定為AES算法,不區(qū)分大小寫
KeyGenerator keygen = KeyGenerator.getInstance("AES");
// 2.根據(jù)ecnodeRules規(guī)則初始化密鑰生成器
// 生成一個128位的隨機源,根據(jù)傳入的字節(jié)數(shù)組
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(encodeRules.getBytes());
keygen.init(128, secureRandom);
// keygen.init(128, new SecureRandom(encodeRules.getBytes()));
// 3.產生原始對稱密鑰
SecretKey original_key = keygen.generateKey();
// 4.獲得原始對稱密鑰的字節(jié)數(shù)組
byte[] raw = original_key.getEncoded();
// 5.根據(jù)字節(jié)數(shù)組生成AES密鑰
key = new SecretKeySpec(raw, "AES");
// 6.根據(jù)指定算法AES自成密碼器
cipher = Cipher.getInstance("AES");
return this;
}
}
}