| | |
| | | package com.se.system.utils; |
| | | |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | import java.security.MessageDigest; |
| | | import java.util.*; |
| | | |
| | | @SuppressWarnings("ALL") |
| | |
| | | |
| | | public CommonUtils() { |
| | | } |
| | | |
| | | private static final String key = "Secret_Ke?y_02_95"; |
| | | |
| | | public static String validStringValue(Object object) { |
| | | return object == null ? "" : object.toString(); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | public static String encrypt(String json) throws Exception { |
| | | byte[] keyBytes = key.getBytes(); |
| | | MessageDigest sha = MessageDigest.getInstance("SHA-256"); |
| | | keyBytes = sha.digest(keyBytes); |
| | | keyBytes = java.util.Arrays.copyOf(keyBytes, 16); |
| | | SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES"); |
| | | |
| | | Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); |
| | | cipher.init(Cipher.ENCRYPT_MODE, secretKey); |
| | | byte[] encryptedBytes = cipher.doFinal(json.getBytes()); |
| | | return Base64.getEncoder().encodeToString(encryptedBytes); |
| | | } |
| | | |
| | | public static String decrypt(String encryptedJson) throws Exception { |
| | | byte[] keyBytes = key.getBytes(); |
| | | MessageDigest sha = MessageDigest.getInstance("SHA-256"); |
| | | keyBytes = sha.digest(keyBytes); |
| | | keyBytes = java.util.Arrays.copyOf(keyBytes, 16); |
| | | SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES"); |
| | | |
| | | Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); |
| | | cipher.init(Cipher.DECRYPT_MODE, secretKey); |
| | | byte[] encryptedBytes = Base64.getDecoder().decode(encryptedJson); |
| | | byte[] decryptedBytes = cipher.doFinal(encryptedBytes); |
| | | return new String(decryptedBytes); |
| | | } |
| | | |
| | | public static void test(String[] args) { |
| | | Integer a = null; |
| | | System.out.println(isNotEmpty(a)); |