[8.5.0]
[myflow升级md5和加密校验]
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package com.mogo.eagle.core.utilcode.util;
|
||||
|
||||
import android.util.Base64;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
/**
|
||||
* Android平台专用的AES-ECB Base64加解密工具类
|
||||
* 适配Android的Base64工具类,优化异常处理
|
||||
*/
|
||||
public class AesEcbBase64Util {
|
||||
|
||||
// 测试方法(Android中建议在Activity/Fragment/ViewModel中调用)
|
||||
public static void testEncryptDecrypt() {
|
||||
String plainText = "41a59bbbb04b4f02b03672f1c177d4cc";
|
||||
String keyBase64 = "3OMe6b0jgPRIgngYxRdeGA==";
|
||||
|
||||
String encryptResult = AesEcbBase64Util.encryptToBase64(plainText, keyBase64);
|
||||
String decryptResult = AesEcbBase64Util.decryptFromBase64(encryptResult, keyBase64);
|
||||
|
||||
System.out.println("加密结果:" + encryptResult);
|
||||
System.out.println("解密结果:" + decryptResult);
|
||||
}
|
||||
|
||||
private static final String ALGO = "AES";
|
||||
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
|
||||
private static final int KEY_LEN_BYTES = 16;
|
||||
|
||||
// 私有化构造方法,防止实例化
|
||||
private AesEcbBase64Util() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机的AES密钥(Base64编码)
|
||||
* @return 16字节密钥的Base64字符串
|
||||
*/
|
||||
public static String randomKeyBase64() {
|
||||
byte[] key = new byte[KEY_LEN_BYTES];
|
||||
new SecureRandom().nextBytes(key);
|
||||
// 替换为Android的Base64工具类,使用默认编码模式
|
||||
return Base64.encodeToString(key, Base64.NO_WRAP);
|
||||
}
|
||||
|
||||
/**
|
||||
* AES加密并输出Base64编码结果
|
||||
* @param plaintext 明文
|
||||
* @param keyBase64 Base64编码的16字节AES密钥
|
||||
* @return Base64编码的密文
|
||||
* @throws IllegalArgumentException 密钥长度错误时抛出
|
||||
* @throws IllegalStateException 加密失败时抛出
|
||||
*/
|
||||
public static String encryptToBase64(String plaintext, String keyBase64) {
|
||||
try {
|
||||
// 解码Base64密钥
|
||||
byte[] key = Base64.decode(keyBase64, Base64.NO_WRAP);
|
||||
if (key.length != KEY_LEN_BYTES) {
|
||||
throw new IllegalArgumentException("无效的AES密钥长度: " + key.length + ",必须为16字节");
|
||||
}
|
||||
|
||||
// 初始化加密器
|
||||
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, ALGO));
|
||||
|
||||
// 处理空明文
|
||||
byte[] plainBytes = plaintext == null ? new byte[0] : plaintext.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] encryptBytes = cipher.doFinal(plainBytes);
|
||||
|
||||
// 加密结果转Base64
|
||||
return Base64.encodeToString(encryptBytes, Base64.NO_WRAP);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new IllegalStateException("AES加密失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Base64密文解密出明文
|
||||
* @param ciphertextBase64 Base64编码的密文
|
||||
* @param keyBase64 Base64编码的16字节AES密钥
|
||||
* @return 解密后的明文
|
||||
* @throws IllegalArgumentException 密钥长度错误时抛出
|
||||
* @throws IllegalStateException 解密失败时抛出
|
||||
*/
|
||||
public static String decryptFromBase64(String ciphertextBase64, String keyBase64) {
|
||||
try {
|
||||
// 解码Base64密钥
|
||||
byte[] key = Base64.decode(keyBase64, Base64.NO_WRAP);
|
||||
if (key.length != KEY_LEN_BYTES) {
|
||||
throw new IllegalArgumentException("无效的AES密钥长度: " + key.length + ",必须为16字节");
|
||||
}
|
||||
|
||||
// 解码Base64密文
|
||||
byte[] cipherBytes = Base64.decode(ciphertextBase64, Base64.NO_WRAP);
|
||||
|
||||
// 初始化解密器
|
||||
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
|
||||
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, ALGO));
|
||||
|
||||
// 解密并转字符串
|
||||
byte[] plainBytes = cipher.doFinal(cipherBytes);
|
||||
return new String(plainBytes, StandardCharsets.UTF_8);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new IllegalStateException("AES解密失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user