gpt4 book ai didi

c# - C# 中的 AES 加密(帮助)和 Java 中的解密(完成)

转载 作者:行者123 更新时间:2023-11-30 15:10:04 24 4
gpt4 key购买 nike

我们有一个 .NET 应用程序需要将一些数据传递给 Java 应用程序。我们想给它一些简单的加密,M.C.霍金不会入侵,但它不能是纯文本。

我发现了一些很棒的 Java 代码,可以让我使用 AES 进行加密/解密。我希望找到的是 C# 的匹配项,它可以让我加密一个可以用我的 Java 例程解密的字符串。

这是我的java类:

class SimpleProtector
{
private final String ALGORITHM = "AES";
private final byte[] keyValue = new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };
public String encrypt(String valueToEnc) throws Exception
{
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
String encryptedValue = new BASE64Encoder().encode(encValue);
return encryptedValue;
}

public String decrypt(String encryptedValue) throws Exception
{
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}

private Key generateKey() throws Exception
{
Key key = new SecretKeySpec(keyValue, ALGORITHM);
return key;
}
}

感谢任何提示!

最佳答案

查看此问题的答案:Using AES encryption in C#

关于c# - C# 中的 AES 加密(帮助)和 Java 中的解密(完成),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3632392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com