以下是使用C#进行ASE加密后,Java可以访问的代码示例:
C# 代码(使用ASE加密):
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public class AESExample
{
public static string Encrypt(string plainText, string key, string iv)
{
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Encoding.UTF8.GetBytes(key);
aesAlg.IV = Encoding.UTF8.GetBytes(iv);
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
byte[] encryptedBytes = msEncrypt.ToArray();
return Convert.ToBase64String(encryptedBytes);
}
}
}
}
}
Java 代码(解密使用):
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.util.Base64;
public class AESExample {
public static String Decrypt(String cipherText, String key, String iv) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encryptedBytes = Base64.getDecoder().decode(cipherText);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes);
}
}
通过将加密后的数据从C#传递到Java,您可以使用上述Java代码进行解密。请确保在使用Java代码之前,将该代码添加到您的Java项目中,并在使用时提供正确的密钥和IV值。