Java實(shí)現(xiàn)的AES256加密解密功能示例
本文實(shí)例講述了Java實(shí)現(xiàn)的AES256加密解密功能。分享給大家供大家參考,具體如下:
一.代碼
package com.handler;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AES256Encryption{
public static final String KEY_ALGORITHM="AES";
public static final String CIPHER_ALGORITHM="AES/ECB/PKCS7Padding";
public static byte[] initkey() throws Exception{
//實(shí)例化密鑰生成器
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
KeyGenerator kg=KeyGenerator.getInstance(KEY_ALGORITHM, "BC");
kg.init(256);
kg.init(128);
SecretKey secretKey=kg.generateKey();
return secretKey.getEncoded();
}
public static byte[] initRootKey() throws Exception{
return new byte[] { 0x08, 0x08, 0x04, 0x0b, 0x02, 0x0f, 0x0b, 0x0c,
0x01, 0x03, 0x09, 0x07, 0x0c, 0x03, 0x07, 0x0a, 0x04, 0x0f,
0x06, 0x0f, 0x0e, 0x09, 0x05, 0x01, 0x0a, 0x0a, 0x01, 0x09,
0x06, 0x07, 0x09, 0x0d };
}
public static Key toKey(byte[] key) throws Exception{
SecretKey secretKey=new SecretKeySpec(key,KEY_ALGORITHM);
return secretKey;
}
public static byte[] encrypt(byte[] data,byte[] key) throws Exception{
Key k=toKey(key);
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data,byte[] key) throws Exception{
Key k =toKey(key);
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(data);
}
public static void main(String[] args) throws UnsupportedEncodingException{
String str="蕓sweet";
//打印原文
System.out.println("原文:"+str);
//密鑰
byte[] key;
try {
//生成隨機(jī)密鑰
key = AES256Encryption.initkey();
//打印密鑰
System.out.print("密鑰:");
for(int i = 0;i
System.out.printf("%x", key[i]);
}
System.out.print("n");
//加密
byte[] data=AES256Encryption.encrypt(str.getBytes(), key);
//打印密文
System.out.print("加密后:");
for(int i = 0;i
System.out.printf("%x", data[i]);
}
System.out.print("n");
//解密密文
data=AES256Encryption.decrypt(data, key);
//打印原文
System.out.println("解密后:"+new String(data));
} catch (Exception e) {
e.printStackTrace();
}
二.注意
1.需要在工程中引入 bcprov-jdk15-133.jar
本站下載鏈接。
2.替換jrelibsecurity下的local_policy.jar 和 US_export_policy.jar
1)如果程序使用是系統(tǒng)jdk,則替換系統(tǒng)環(huán)境變量的jdk中jrelibsecurity下的jar包。
2)如果程序是在MyEclipse中運(yùn)行,則找到MyEclipse使用的jdk(方法:在MyEclipse里面進(jìn)入window->Preferences->java選項(xiàng)里面有一個(gè)Installed JREs的選項(xiàng),點(diǎn)擊右邊會(huì)出現(xiàn)一個(gè)列表,里面有你現(xiàn)在用到的JDK版本及路徑),替換該jdk中jrelibsecurity下的jar包。
可以解決:java.security.InvalidKeyException: Illegal key size or default parameters異常
三.如果密鑰需要存入數(shù)據(jù)庫,則需要對(duì)密鑰進(jìn)行base64編碼,即將密鑰(byte數(shù)組)通過base64編碼轉(zhuǎn)換成密鑰(String類型);從數(shù)據(jù)庫中讀取密鑰時(shí),則使用base64解碼,即將密鑰(String類型)轉(zhuǎn)換成密鑰(byte數(shù)組)。詳見《Java實(shí)現(xiàn)base64編碼》
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password
迅雷、快車、旋風(fēng)URL加密/解密工具:
http://tools.jb51.net/password/urlrethunder
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
JAVA解決在@autowired,@Resource注入為null的情況
這篇文章主要介紹了JAVA解決在@autowired,@Resource注入為null的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
使用Java動(dòng)態(tài)創(chuàng)建Flowable會(huì)簽?zāi)P偷氖纠a
動(dòng)態(tài)創(chuàng)建流程模型,尤其是會(huì)簽(Parallel Gateway)模型,是提升系統(tǒng)靈活性和響應(yīng)速度的關(guān)鍵技術(shù)之一,本文將通過Java編程語言,深入探討如何在運(yùn)行時(shí)動(dòng)態(tài)地創(chuàng)建包含會(huì)簽環(huán)節(jié)的Flowable流程模型,需要的朋友可以參考下2024-05-05
關(guān)于mybatis callSettersOnNulls 配置解析
這篇文章主要介紹了關(guān)于mybatis callSettersOnNulls 配置,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-06-06
Java攔截器Interceptor實(shí)現(xiàn)原理及代碼示例
本文詳細(xì)講解了Java攔截器Interceptor實(shí)現(xiàn)原理及代碼示例,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
SpringBoot統(tǒng)一數(shù)據(jù)返回格式的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot統(tǒng)一數(shù)據(jù)返回格式,它提高了代碼的可維護(hù)性和一致性,并改善了客戶端與服務(wù)端之間的通信,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05

