欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于AES加密算法在linux下解密失敗的解決辦法

 更新時間:2017年02月22日 16:46:53   作者:太陽當(dāng)空照~  
這篇文章主要介紹了關(guān)于AES加密算法在linux下解密失敗的解決辦法,需要的朋友可以參考下

前段時間項目要部署到linux上時遇到了這個問題,百度一下找到了解決方案,在這分享一下:

public class RSAEncrypt {
// 密鑰
private static Key key;
// KEY種子
private static String KEY_STR = "keyString";
// 常量
public static final String UTF_8 = "UTF-8";
public static final String AES = "AES";
// 靜態(tài)初始化
static {
try {
// KEY 生成器
KeyGenerator generator = KeyGenerator.getInstance(AES);
// 初始化算法,設(shè)置成“SHA1PRNG”是為了防止在linux環(huán)境下隨機(jī)生成算法
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(KEY_STR.getBytes(UTF_8));
//128,192,256
generator.init(128,secureRandom);
// 生成密鑰
key = generator.generateKey();
generator = null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 對源字符串加密,返回 BASE64編碼后的加密字符串
*
* @param source
* 源字符串,明文
* @return 密文字符串
*/
public static String encode(String source) {
try {
// 根據(jù)編碼格式獲取字節(jié)數(shù)組
byte[] sourceBytes = source.getBytes(UTF_8);
// 加密模式
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.ENCRYPT_MODE, key);
// 加密后的字節(jié)數(shù)組
byte[] encryptSourceBytes = cipher.doFinal(sourceBytes);
// Base64編碼器
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(encryptSourceBytes);
} catch (Exception e) {
// throw 也算是一種 return 路徑
throw new RuntimeException(e);
}
}
/**
* 對本工具類 encode() 方法加密后的字符串進(jìn)行解碼/解密
*
* @param encrypted
* 被加密過的字符串,即密文
* @return 明文字符串
*/
public static String decode(String encrypted) {
// Base64解碼器
BASE64Decoder base64Decoder = new BASE64Decoder();
try {
// 先進(jìn)行base64解碼
byte[] cryptedBytes = base64Decoder.decodeBuffer(encrypted);
// 解密模式
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.DECRYPT_MODE, key);
// 解碼后的字節(jié)數(shù)組
byte[] decryptStrBytes = cipher.doFinal(cryptedBytes);
// 采用給定編碼格式將字節(jié)數(shù)組變成字符串
return new String(decryptStrBytes, UTF_8);
} catch (Exception e) {
// 這種形式確實(shí)適合處理工具類
throw new RuntimeException(e);
}
}

以上所述是小編給大家介紹的關(guān)于AES加密算法在linux下解密失敗的解決辦法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論