AES 加解密
public class AESUtil {
private static final String KEY_ALGORITHM = "AES";
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默認(rèn)的加密算法
/**
* AES 加密操作
*
* @param content 待加密內(nèi)容
* @param key 加密密鑰
* @return 返回Base64轉(zhuǎn)碼后的加密數(shù)據(jù)
*/
public static String encrypt(String content, String key) {
try {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 創(chuàng)建密碼器
byte[] byteContent = content.getBytes(StandardCharsets.UTF_8);
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));// 初始化為加密模式的密碼器
byte[] result = cipher.doFinal(byteContent);// 加密
return new String(Base64.encode(result, Base64.NO_WRAP));//通過Base64轉(zhuǎn)碼返回
} catch (Exception ex) {
Logger.getLogger(AESUtil.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
/**
* AES 解密操作
*
* @param content
* @param key
* @return
*/
public static String decrypt(String content, String key) {
try {
//實(shí)例化
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
//使用密鑰初始化,設(shè)置為解密模式
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
//執(zhí)行操作
byte[] result = cipher.doFinal(Base64.decode(content, Base64.NO_WRAP));
return new String(result, StandardCharsets.UTF_8);
} catch (Exception ex) {
Logger.getLogger(AESUtil.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
private static SecretKeySpec getSecretKey(final String key) {
//返回生成指定算法密鑰生成器的 KeyGenerator 對(duì)象
return new SecretKeySpec(key.getBytes(), DEFAULT_CIPHER_ALGORITHM);
}
public static void main(String[] args) {
String content = "hello,您好";
String key = "4sde34&df8543*er";
System.out.println("content:" + content);
String s1 = AESUtil.encrypt(content, key);
System.out.println("s1:" + s1);
System.out.println("s2:"+AESUtil.decrypt(s1, key));
}
}
RSA 加解密
public class RSAUtil {
public static Map<Integer, String> keyMap = new HashMap<Integer, String>(); //用于封裝隨機(jī)產(chǎn)生的公鑰與私鑰
/**
* 隨機(jī)生成密鑰對(duì)
*
* @throws NoSuchAlgorithmException
*/
public static void genKeyPair() throws NoSuchAlgorithmException {
// KeyPairGenerator類用于生成公鑰和私鑰對(duì),基于RSA算法生成對(duì)象
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// 初始化密鑰對(duì)生成器,密鑰大小為96-1024位
keyPairGen.initialize(1024, new SecureRandom());
// 生成一個(gè)密鑰對(duì),保存在keyPair中
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私鑰
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 得到公鑰
String publicKeyString = Base64.encodeToString(publicKey.getEncoded(), Base64.DEFAULT);
// 得到私鑰字符串
String privateKeyString = Base64.encodeToString(privateKey.getEncoded(), Base64.DEFAULT);
// 將公鑰和私鑰保存到Map
keyMap.put(0, publicKeyString); //0表示公鑰
keyMap.put(1, privateKeyString); //1表示私鑰
}
/**
* RSA公鑰加密
*
* @param str 加密字符串
* @param publicKey 公鑰
* @return 密文
* @throws Exception 加密過程中的異常信息
*/
public static String encrypt(String str, String publicKey) throws Exception {
//base64編碼的公鑰
byte[] decoded = Base64.decode(publicKey, Base64.DEFAULT);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return Base64.encodeToString(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)), Base64.DEFAULT);
}
/**
* RSA私鑰解密
*
* @param str 加密字符串
* @param privateKey 私鑰
* @return 銘文
* @throws Exception 解密過程中的異常信息
*/
public static String decrypt(String str, String privateKey) throws Exception {
//64位解碼加密后的字符串
byte[] inputByte = Base64.decode(str.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
//base64編碼的私鑰
byte[] decoded = Base64.decode(privateKey, Base64.DEFAULT);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return new String(cipher.doFinal(inputByte));
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。