日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java AES加密

發布時間:2025/3/21 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java AES加密 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用JDK8引入的 java.util.Base64 負責base64轉碼
Base64Tools 工具類,AES加密要用到它,負責 byte 數組的base64編碼,base64 編碼的目的是消除亂碼

import java.io.UnsupportedEncodingException; import java.util.Base64; public class Base64Tools {private final static Base64.Decoder decoder = Base64.getDecoder();private final static Base64.Encoder encoder = Base64.getEncoder();public static byte[] encode(byte[] content) throws UnsupportedEncodingException {return encoder.encode(content);}public static byte[] decode(byte[] content) throws UnsupportedEncodingException {return decoder.decode(content);} }

AESTools 工具類,負責對 byte 數組加密和解密

import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Base64;public class AESTools {private static final Base64.Decoder decoder = Base64.getDecoder();private static final Base64.Encoder encoder = Base64.getEncoder();public static byte[] AESEncode(String password, byte[] content) {try {KeyGenerator keygen = KeyGenerator.getInstance("AES");SecureRandom random = SecureRandom.getInstance("SHA1PRNG");random.setSeed(password.getBytes());keygen.init(128, random);SecretKey originalKey = keygen.generateKey();SecretKey key = new SecretKeySpec(originalKey.getEncoded(), "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE, key);return Base64Tools.encode(cipher.doFinal(content)); // 加密后用base64編碼} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (InvalidKeyException e) {e.printStackTrace();} catch (NoSuchPaddingException e) {e.printStackTrace();} catch (BadPaddingException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IllegalBlockSizeException e) {e.printStackTrace();}return null;}public static byte[] AESDecode(String password, byte[] content) {try {KeyGenerator keygen = KeyGenerator.getInstance("AES");SecureRandom random = SecureRandom.getInstance("SHA1PRNG");random.setSeed(password.getBytes());keygen.init(128, random);SecretKey originalKey = keygen.generateKey();SecretKey key = new SecretKeySpec(originalKey.getEncoded(), "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE, key);return cipher.doFinal(Base64Tools.decode(content));} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (InvalidKeyException e) {e.printStackTrace();} catch (NoSuchPaddingException e) {e.printStackTrace();} catch (BadPaddingException e) {e.printStackTrace();} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (IllegalBlockSizeException e) {e.printStackTrace();}return null;}}

測試用例我從網上找了一段文字,將它加密后再解密,和原文進行對比

public class Main {public static void main(String[] args) throws Exception {String password = "passwordwhateveryouwant";String content = "We hold these truths to be self-evident, that all men are created equal\n我們認為下述真理是不言而喻的:人人生而平等";System.out.println(content);String encryptedString = new String(AESTools.AESEncode(password, content.getBytes()));System.out.println(encryptedString);String decryptedString = new String(AESTools.AESDecode(password, encryptedString.getBytes()));System.out.println(decryptedString);if (content.equals(decryptedString)) System.out.printf("原文和加密后再解密的結果相等");else System.out.printf("原文和加密后再解密的結果不相等");} }

運行結果

總結

以上是生活随笔為你收集整理的java AES加密的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。