Java C# 共用 AES-128 加密并解决258位密钥报错问题
Java AES-128 加解密:
/**
- Aes 加密
- @param content 加密内容
- @param key 秘钥
- @return
- @throws Exception
*/
public static String Encrypt(String content, String key) throws Exception {
byte[] raw = key.getBytes(“utf-8”);
SecretKeySpec skeySpec = new SecretKeySpec(raw, “AES”);
//“算法/模式/补码方式”
Cipher cipher = Cipher.getInstance(“AES/ECB/PKCS5Padding”);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(content.getBytes(“utf-8”));
//此处使用BASE64做转码功能,同时能起到2次加密的作用。
return new Base64().encodeToString(encrypted);
}
/**
- AES解密
- @param content 解密内容
- @param key 秘钥
- @return
- @throws Exception
*/
public static String Decrypt(String content, String key) throws Exception {
try {
byte[] raw = key.getBytes(“utf-8”);
Secre