parent
fcc1c4f7af
commit
916c55fc65
@ -0,0 +1,32 @@
|
||||
package com.ruoyi.jjh.declaration.util;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* @author dong
|
||||
* @since 2024/5/23 13:32
|
||||
*/
|
||||
|
||||
public class AESEncryptor {
|
||||
|
||||
private static final String ALGORITHM = "AES";
|
||||
private static final String SECRET_KEY = "2a20f065d22978998af65de11beeac5cad00cccf0a5d45abcff12eec0cd9311c"; // 密钥需要16个字符
|
||||
|
||||
public static String encrypt(String data) throws Exception {
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
|
||||
byte[] encryptedBytes = cipher.doFinal(data.getBytes());
|
||||
return Base64.getEncoder().encodeToString(encryptedBytes);
|
||||
}
|
||||
|
||||
public static String decrypt(String encryptedData) throws Exception {
|
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(), ALGORITHM);
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
|
||||
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
|
||||
return new String(decryptedBytes);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue