diff --git a/src/main/java/com/imyeyu/utils/Decryptor.java b/src/main/java/com/imyeyu/utils/Decryptor.java new file mode 100644 index 0000000..6e0e692 --- /dev/null +++ b/src/main/java/com/imyeyu/utils/Decryptor.java @@ -0,0 +1,50 @@ +package com.imyeyu.utils; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +/** + * @author 夜雨 + * @since 2025-10-29 16:11 + */ +public class Decryptor { + + /** + * 解密字符串 + * + * @param data 待解密字节数据 + * @param key 密钥 + * @return 解密结果 + */ + public byte[] aes(String data, byte[] key) { + return aes(data.getBytes(), key); + } + + /** + * 解密 + * + * @param data 待解密字节数据 + * @param key 密钥 + * @return 解密结果 + */ + public byte[] aes(byte[] data, byte[] key) { + try { + SecretKey secretKey = new SecretKeySpec(key, "AES"); + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + cipher.init(Cipher.DECRYPT_MODE, secretKey); + return cipher.doFinal(data); + } catch (IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e) { + throw new RuntimeException("invalid data"); + } catch (InvalidKeyException e) { + throw new RuntimeException("invalid key"); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("not support AES encrypt"); + } + } +} diff --git a/src/main/java/com/imyeyu/utils/Encryptor.java b/src/main/java/com/imyeyu/utils/Encryptor.java new file mode 100644 index 0000000..eb84134 --- /dev/null +++ b/src/main/java/com/imyeyu/utils/Encryptor.java @@ -0,0 +1,65 @@ +package com.imyeyu.utils; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.KeyGenerator; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +/** + * @author 夜雨 + * @since 2025-10-29 16:02 + */ +public class Encryptor { + + public byte[] aesKey() { + return aesKey(256); + } + + public byte[] aesKey(int size) { + try { + KeyGenerator keyGen = KeyGenerator.getInstance("AES"); + keyGen.init(size); + return keyGen.generateKey().getEncoded(); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("not support AES encrypt"); + } + } + + /** + * 加密字符串 + * + * @param data 待加密字符串 + * @param key 密钥 + * @return 加密结果 + */ + public byte[] aes(String data, byte[] key) { + return aes(data.getBytes(), key); + } + + /** + * 加密 + * + * @param data 待加密字节数据 + * @param key 密钥 + * @return 加密结果 + */ + public byte[] aes(byte[] data, byte[] key) { + try { + SecretKey secretKey = new SecretKeySpec(key, "AES"); + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, secretKey); + return cipher.doFinal(data); + } catch (IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e) { + throw new RuntimeException("invalid data"); + } catch (InvalidKeyException e) { + throw new RuntimeException("invalid key"); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("not support AES encrypt"); + } + } +}