JDK對DESede算法的支持
十余年的白堿灘網站建設經驗,針對設計、前端、開發、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。成都營銷網站建設的優勢是能夠根據用戶設備顯示端的尺寸不同,自動調整白堿灘建站的顯示方式,使網站能夠適用不同顯示終端,在瀏覽器中調整網站的寬度,無論在任何一種瀏覽器上瀏覽網站,都能展現優雅布局與設計,從而大程度地提升瀏覽體驗。創新互聯從事“白堿灘網站設計”,“白堿灘網站推廣”以來,每個客戶項目都認真落實執行。
密鑰長度:128位
工作模式:ECB/CBC/PCBC/CTR/CTS/CFB/CFB8 to CFB128/OFB/OBF8 to OFB128
填充方式:Nopadding/PKCS5Padding/ISO10126Padding/
AES加密解密的java實現:
package com.kongxincai.encanddec;import java.security.Key;import java.security.NoSuchAlgorithmException;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;public class AESCoder { ? private static final String KEY_ALGORITHM = "AES"; ? private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默認的加密算法
public static byte[] initSecretKey() { ? ? ? //返回生成指定算法密鑰生成器的 KeyGenerator 對象
KeyGenerator kg = null; ? ? ? try {
kg = KeyGenerator.getInstance(KEY_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace(); ? ? ? ? ? return new byte[0];
} ? ? ? //初始化此密鑰生成器,使其具有確定的密鑰大小 ? ? ? //AES 要求密鑰長度為 128
kg.init(128); ? ? ? //生成一個密鑰
SecretKey ?secretKey = kg.generateKey(); ? ? ? return secretKey.getEncoded();
} ? private static Key toKey(byte[] key){ ? ? ? //生成密鑰
return new SecretKeySpec(key, KEY_ALGORITHM);
} ? public static byte[] encrypt(byte[] data,Key key) throws Exception{ ? ? ? return encrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} ? public static byte[] encrypt(byte[] data,byte[] key) throws Exception{ ? ? ? return encrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} ? public static byte[] encrypt(byte[] data,byte[] key,String cipherAlgorithm) throws Exception{ ? ? ? //還原密鑰
Key k = toKey(key); ? ? ? return encrypt(data, k, cipherAlgorithm);
} ? public static byte[] encrypt(byte[] data,Key key,String cipherAlgorithm) throws Exception{ ? ? ? //實例化
Cipher cipher = Cipher.getInstance(cipherAlgorithm); ? ? ? //使用密鑰初始化,設置為加密模式 ? ? ? cipher.init(Cipher.ENCRYPT_MODE, key); ? ? ? //執行操作
return cipher.doFinal(data);
} ? public static byte[] decrypt(byte[] data,byte[] key) throws Exception{ ? ? ? return decrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} ? public static byte[] decrypt(byte[] data,Key key) throws Exception{ ? ? ? return decrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} ? public static byte[] decrypt(byte[] data,byte[] key,String cipherAlgorithm) throws Exception{ ? ? ? //還原密鑰
Key k = toKey(key); ? ? ? return decrypt(data, k, cipherAlgorithm);
} ? public static byte[] decrypt(byte[] data,Key key,String cipherAlgorithm) throws Exception{ ? ? ? //實例化
Cipher cipher = Cipher.getInstance(cipherAlgorithm); ? ? ? //使用密鑰初始化,設置為解密模式 ? ? ? cipher.init(Cipher.DECRYPT_MODE, key); ? ? ? //執行操作
return cipher.doFinal(data);
} ? private static String ?showByteArray(byte[] data){ ? ? ? if(null == data){ ? ? ? ? ? return null;
}
StringBuilder sb = new StringBuilder("{"); ? ? ? for(byte b:data){
sb.append(b).append(",");
}
sb.deleteCharAt(sb.length()-1);
sb.append("}"); ? ? ? return sb.toString();
} ? public static void main(String[] args) throws Exception { ? ? ? byte[] key = initSecretKey();
System.out.println("key:"+showByteArray(key));
Key k = toKey(key); //生成秘鑰
String data ="AES數據";
System.out.println("加密前數據: string:"+data);
System.out.println("加密前數據: byte[]:"+showByteArray(data.getBytes()));
System.out.println(); ? ? ? byte[] encryptData = encrypt(data.getBytes(), k);//數據加密
System.out.println("加密后數據: byte[]:"+showByteArray(encryptData));// ? ? ? System.out.println("加密后數據: hexStr:"+Hex.encodeHexStr(encryptData)); ? ? ? System.out.println(); ? ? ? byte[] decryptData = decrypt(encryptData, k);//數據解密
System.out.println("解密后數據: byte[]:"+showByteArray(decryptData));
System.out.println("解密后數據: string:"+new String(decryptData));
}
}
1. AES加密字符串
public static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");// 創建AES的Key生產者
kgen.init(128, new SecureRandom(password.getBytes()));// 利用用戶密碼作為隨機數初始化出
// 128位的key生產者
//加密沒關系,SecureRandom是生成安全隨機數序列,password.getBytes()是種子,只要種子相同,序列就一樣,所以解密只要有password就行
SecretKey secretKey = kgen.generateKey();// 根據用戶密碼,生成一個密鑰
byte[] enCodeFormat = secretKey.getEncoded();// 返回基本編碼格式的密鑰,如果此密鑰不支持編碼,則返回
// null。
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 轉換為AES專用密鑰
Cipher cipher = Cipher.getInstance("AES");// 創建密碼器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化為加密模式的密碼器
byte[] result = cipher.doFinal(byteContent);// 加密
return result;
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
2. AES解密
public static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");// 創建AES的Key生產者
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();// 根據用戶密碼,生成一個密鑰
byte[] enCodeFormat = secretKey.getEncoded();// 返回基本編碼格式的密鑰
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 轉換為AES專用密鑰
Cipher cipher = Cipher.getInstance("AES");// 創建密碼器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化為解密模式的密碼器
byte[] result = cipher.doFinal(content);
return result; // 明文
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
你解密的key必須是加密的key啊
你看看,你解密的時候又KeyGenerator.getInstance("AES").generateKey();這是重新搞了一個key啊,當然解不出來了
我估計你這代碼人家原先是寫在一起的吧,加密完了再直接解密給你看,人家只generateKey一次,自然很順利,你分成了兩個例子,居然分別generateKey,自然失敗
這個算法java SDK自帶的額 參考代碼如下:
/**解密
*?@param?content??待解密內容
*?@param?password?解密密鑰
*?@return
*/
public?static?byte[]?decrypt(byte[]?content,?String?password)?{
try?{
KeyGenerator?kgen?=?KeyGenerator.getInstance("AES");
kgen.init(128,?new?SecureRandom(password.getBytes()));
SecretKey?secretKey?=?kgen.generateKey();
byte[]?enCodeFormat?=?secretKey.getEncoded();
SecretKeySpec?key?=?new?SecretKeySpec(enCodeFormat,?"AES");
Cipher?cipher?=?Cipher.getInstance("AES");//?創建密碼器
cipher.init(Cipher.DECRYPT_MODE,?key);//?初始化
byte[]?result?=?cipher.doFinal(content);
return?result;?//?加密
}?catch?(NoSuchAlgorithmException?e)?{
e.printStackTrace();
}?catch?(NoSuchPaddingException?e)?{
e.printStackTrace();
}?catch?(InvalidKeyException?e)?{
e.printStackTrace();
}?catch?(IllegalBlockSizeException?e)?{
e.printStackTrace();
}?catch?(BadPaddingException?e)?{
e.printStackTrace();
}
return?null;
}
/**
*?加密
*
*?@param?content?需要加密的內容
*?@param?password??加密密碼
*?@return
*/
public?static?byte[]?encrypt(String?content,?String?password)?{
try?{
KeyGenerator?kgen?=?KeyGenerator.getInstance("AES");
kgen.init(128,?new?SecureRandom(password.getBytes()));
SecretKey?secretKey?=?kgen.generateKey();
byte[]?enCodeFormat?=?secretKey.getEncoded();
SecretKeySpec?key?=?new?SecretKeySpec(enCodeFormat,?"AES");
Cipher?cipher?=?Cipher.getInstance("AES");//?創建密碼器
byte[]?byteContent?=?content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE,?key);//?初始化
byte[]?result?=?cipher.doFinal(byteContent);
return?result;?//?加密
}?catch?(NoSuchAlgorithmException?e)?{
e.printStackTrace();
}?catch?(NoSuchPaddingException?e)?{
e.printStackTrace();
}?catch?(InvalidKeyException?e)?{
e.printStackTrace();
}?catch?(UnsupportedEncodingException?e)?{
e.printStackTrace();
}?catch?(IllegalBlockSizeException?e)?{
e.printStackTrace();
}?catch?(BadPaddingException?e)?{
e.printStackTrace();
}
return?null;
}
圖像界面的話就不說了
網站標題:java代碼aes解密 javaaes加密解密算法
分享URL:http://vcdvsql.cn/article40/ddcciho.html
成都網站建設公司_創新互聯,為您提供品牌網站制作、搜索引擎優化、網站導航、網站設計公司、全網營銷推廣、虛擬主機
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯