java AES加密
生活随笔
收集整理的這篇文章主要介紹了
java AES加密
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用JDK8引入的 java.util.Base64 負責base64轉碼
Base64Tools 工具類,AES加密要用到它,負責 byte 數組的base64編碼,base64 編碼的目的是消除亂碼
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加密的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 树莓派3b+在tf卡配置wifi连接+s
- 下一篇: 树莓派3开wifi热点