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 security自定義認(rèn)證登錄的全過(guò)程記錄
這篇文章主要給大家介紹了關(guān)于spring security自定義認(rèn)證登錄的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12springboot讀取application.yaml文件數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了springboot讀取application.yaml文件數(shù)據(jù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07java.io.UncheckedIOException: Cannot delete C
本文主要介紹了java.io.UncheckedIOException: Cannot delete C:\Users\guo\AppData\Local\Temp\tomcat.8081問(wèn)題,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05Java如何跳出當(dāng)前的多重嵌套循環(huán)的問(wèn)題
Java中的循環(huán)結(jié)構(gòu)包括for循環(huán)、while循環(huán)、do-while循環(huán)和增強(qiáng)型for循環(huán),每種循環(huán)都有其適用場(chǎng)景,在循環(huán)中,break、continue和return分別用于跳出循環(huán)、跳過(guò)當(dāng)前循環(huán)和結(jié)束當(dāng)前方法,對(duì)于多重嵌套循環(huán)2025-01-01別在Java代碼里亂打日志了,這才是正確的打日志姿勢(shì)
這篇文章主要介紹了別在Java代碼里亂打日志了,這才是正確的打日志姿勢(shì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Java使用ByteArrayOutputStream 和 ByteArrayInputStream 避免重復(fù)讀取配置文
這篇文章主要介紹了Java使用ByteArrayOutputStream 和 ByteArrayInputStream 避免重復(fù)讀取配置文件的方法,需要的朋友可以參考下2015-12-12