MySQL和Java通用加密解密方式小結(jié)
加密方式使用 AES 加密,再轉(zhuǎn)成 Base64。
SQL
-- 加密 update your_table set your_column=to_base64(aes_encrypt(your_column, "password")); -- 解密 select aes_decrypt(from_base64(your_column) ,"password") from your_table;
使用原生
public class AESUtils {
/**
* AES 加密操作
*
* @param data 待加密內(nèi)容
* @param aesKey 加密的Key
* @return 返回Base64轉(zhuǎn)碼后的加密數(shù)據(jù)
* @throws Exception
*/
public static String encrpt(String encrptData, String aesKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, getSecretKeySpec(aesKey));
byte[] encrypted = cipher.doFinal(encrptData.getBytes());
return Base64.encode(encrypted);
}
/**
* AES 解密操作
*
* @param decrptData
* @param aesKey
* @return 返回解密內(nèi)容
* @throws Exception
*/
public static String decrpt(String decrptData, String aesKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, getSecretKeySpec(aesKey));
byte[] doFinal = cipher.doFinal(Base64.decode(decrptData));
return new String(doFinal);
}
private static SecretKeySpec getSecretKeySpec(String key) {
byte[] keyBytes = Arrays.copyOf(key.getBytes(), 16);
return new SecretKeySpec(keyBytes, "AES");
}
public static void main(String[] args) throws Exception {
String aesKey = "1234567890";
String encrpt = encrpt("128931739@163.com", aesKey);
System.out.println(encrpt);
System.out.println(decrpt(encrpt, aesKey)); //解密 加密的數(shù)據(jù)
}
}使用 Hutool
public class AESUtils {
/**
* AES 加密操作
*
* @param encodedData 待加密內(nèi)容
* @param aesKey 加密的Key
* @return 返回Base64轉(zhuǎn)碼后的加密數(shù)據(jù)
* @throws Exception
*/
public static String encrpt(String encodedData, String aesKey) {
return Base64.encode(SecureUtil.aes(getSecretKey(aesKey)).encrypt(encodedData));
}
/**
* AES 解密操作
*
* @param decrptData 待解密內(nèi)容
* @param aesKey 解密的Key
* @return 返回解密內(nèi)容
* @throws Exception
*/
public static String decrpt(String decrptData, String aesKey) {
return SecureUtil.aes(getSecretKey(aesKey)).decryptStr(Base64.decode(decrptData));
}
private static byte[] getSecretKey(String key) {
byte[] keyBytes = Arrays.copyOf(key.getBytes(), 16);
return SecureUtil.generateKey("AES", keyBytes).getEncoded();
}
public static void main(String[] args) {
String aesKey = "1234567890";
String encrpt = encrpt("128931739@163.com", aesKey);
System.out.println("-->加密后字符串: " + encrpt);
System.out.println("-->解密后字符串: " + decrpt(encrpt, aesKey)); //解密 加密的數(shù)據(jù)
}
}到此這篇關(guān)于MySQL和Java通用加密解密方式的文章就介紹到這了,更多相關(guān)java mysql加密解密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中使用騰訊云發(fā)送短信驗(yàn)證碼的實(shí)現(xiàn)示例
本文主要介紹了Spring?中?使用騰訊云發(fā)送短信驗(yàn)證碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Spring?Boot?2.6.x整合Swagger啟動(dòng)失敗報(bào)錯(cuò)問題的完美解決辦法
這篇文章主要給大家介紹了關(guān)于Spring?Boot?2.6.x整合Swagger啟動(dòng)失敗報(bào)錯(cuò)問題的完美解決辦法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
Spring Boot不同版本Redis設(shè)置JedisConnectionFactory詳解
本文章向大家介紹Spring Boot不同版本Redis設(shè)置JedisConnectionFactory,主要內(nèi)容包括1.X 版本、2.X 版本、2.、基本概念、基礎(chǔ)應(yīng)用、原理機(jī)制和需要注意的事項(xiàng)等,并結(jié)合實(shí)例形式分析了其使用技巧,希望通過(guò)本文能幫助到大家理解應(yīng)用這部分內(nèi)容2023-09-09
Java實(shí)現(xiàn)的文件上傳下載工具類完整實(shí)例【上傳文件自動(dòng)命名】
這篇文章主要介紹了Java實(shí)現(xiàn)的文件上傳下載工具類,結(jié)合完整實(shí)例形式分析了java針對(duì)文件上傳下載操作的相關(guān)實(shí)現(xiàn)技巧,并且針對(duì)上傳文件提供了自動(dòng)命名功能以避免文件命名重復(fù),需要的朋友可以參考下2017-11-11
關(guān)于Jsoup將相對(duì)路徑轉(zhuǎn)為絕對(duì)路徑的方法
這篇文章主要介紹了關(guān)于Jsoup將相對(duì)路徑轉(zhuǎn)為絕對(duì)路徑的方法,jsoup 是一款Java 的HTML解析器,可直接解析某個(gè)URL地址、HTML文本內(nèi)容,需要的朋友可以參考下2023-04-04
SpringBoot 單元測(cè)試實(shí)戰(zhàn)(Mockito,MockBean)
這篇文章主要介紹了SpringBoot 單元測(cè)試實(shí)戰(zhàn)(Mockito,MockBean),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
spring boot 實(shí)現(xiàn)配置多個(gè)DispatcherServlet最簡(jiǎn)單方式
這篇文章主要介紹了spring boot 實(shí)現(xiàn)配置多個(gè)DispatcherServlet最簡(jiǎn)單方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
Java8 CompletableFuture 異步執(zhí)行操作
CompletableFuture是java8提供的基于異步操作的封裝,日常開發(fā)中經(jīng)常會(huì)用到,接下來(lái)通過(guò)本文給大家介紹Java8 CompletableFuture 異步執(zhí)行操作,感興趣的朋友一起看看吧2021-06-06

