|
| 1 | +package icu.epq.gateway.util; |
| 2 | + |
| 3 | +import javax.crypto.BadPaddingException; |
| 4 | +import javax.crypto.Cipher; |
| 5 | +import javax.crypto.IllegalBlockSizeException; |
| 6 | +import javax.crypto.NoSuchPaddingException; |
| 7 | +import javax.crypto.spec.GCMParameterSpec; |
| 8 | +import javax.crypto.spec.SecretKeySpec; |
| 9 | +import java.security.InvalidAlgorithmParameterException; |
| 10 | +import java.security.InvalidKeyException; |
| 11 | +import java.security.NoSuchAlgorithmException; |
| 12 | +import java.util.Base64; |
| 13 | + |
| 14 | +/** |
| 15 | + * 加密、解密工具 |
| 16 | + * |
| 17 | + * @author epqsky |
| 18 | + */ |
| 19 | +public class CryptUtil { |
| 20 | + |
| 21 | + /** |
| 22 | + * 密钥 |
| 23 | + */ |
| 24 | + private static final byte[] DEFAULT_SECRET_KEY = Base64.getEncoder().encode("8NJpH36GGK9a3IkyFd3l6Kb8CJkid9QJ".getBytes()); |
| 25 | + |
| 26 | + /** |
| 27 | + * 初始向量 VI |
| 28 | + */ |
| 29 | + private static final byte[] KEY_VI = Base64.getEncoder().encode("cs4GXOnrqLKK".getBytes()); |
| 30 | + |
| 31 | + /** |
| 32 | + * 字符串信息加密 |
| 33 | + * |
| 34 | + * @param info |
| 35 | + * @return |
| 36 | + * @throws NoSuchPaddingException |
| 37 | + * @throws NoSuchAlgorithmException |
| 38 | + * @throws InvalidAlgorithmParameterException |
| 39 | + * @throws InvalidKeyException |
| 40 | + * @throws BadPaddingException |
| 41 | + * @throws IllegalBlockSizeException |
| 42 | + */ |
| 43 | + public static String setAes256String(String info) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { |
| 44 | + SecretKeySpec keySpec = new SecretKeySpec(Base64.getDecoder().decode(DEFAULT_SECRET_KEY), "AES"); |
| 45 | + Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); |
| 46 | + GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(96, Base64.getDecoder().decode(KEY_VI)); |
| 47 | + cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec); |
| 48 | + |
| 49 | + return Base64.getEncoder().encodeToString(cipher.doFinal(info.getBytes())); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * 字符串信息解密 |
| 54 | + * |
| 55 | + * @param info |
| 56 | + * @return |
| 57 | + * @throws NoSuchPaddingException |
| 58 | + * @throws NoSuchAlgorithmException |
| 59 | + * @throws InvalidAlgorithmParameterException |
| 60 | + * @throws InvalidKeyException |
| 61 | + * @throws BadPaddingException |
| 62 | + * @throws IllegalBlockSizeException |
| 63 | + */ |
| 64 | + public static String getAes256String(String info) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { |
| 65 | + SecretKeySpec keySpec = new SecretKeySpec(Base64.getDecoder().decode(DEFAULT_SECRET_KEY), "AES"); |
| 66 | + Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); |
| 67 | + GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(96, Base64.getDecoder().decode(KEY_VI)); |
| 68 | + cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec); |
| 69 | + return new String(cipher.doFinal(Base64.getDecoder().decode(info))); |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments