sách gpt4 ăn đã đi

Mã hóa/giải mã AES giữa C# (mã hóa) và Java (giải mã)

In lại 作者:搜寻专家 更新时间:2023-11-01 01:28:53 33 4
mua khóa gpt4 giày nike

我有一个 C# 应用程序调用 Java 网络服务来验证用户密码。我想让 C# 应用程序加密密码,然后让 Java Web 服务解密密码。我已经完成了 Java 端的代码(解密代码),但我无法找出 C# 代码来加密代码。

这是我的 Java 代码...

public void validateUserPassword(String encryptedPassword) {
String algorithm = "AES";
SecretKeySpec keySpec = null;
byte[] key = "<==OMGWTFBBQ!==>".getBytes();

Cipher cipher = null;

cipher = Cipher.getInstance(algorithm);
keySpec = new SecretKeySpec(key, algorithm);

byte[] encryptionBytes = new sun.misc.BASE64Decoder().decodeBuffer(encryptedPassword);
cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
String recovered = new String(recoveredBytes);

log.info("Encrypted password: " + encryptedPassword);
log.info("Dencrypted password: " + recovered);
}

这是我发现可以使用 C# 加密的内容,但它不会生成与我的 Java 函数相同的加密字符串,因此我的 Java Web 服务无法解密它。

private void btnEncrypt_Click(object sender, EventArgs e)
{
string PlainText = "testing";
string Password = "<==OMGWTFBBQ!==>";
string Salt = "Kosher";
string HashAlgorithm = "SHA1";
int PasswordIterations = 2;
string InitialVector = "OFRna73m*aze01xY";
int KeySize = 256;
string encryptedPassword;

byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
byte[] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText);

PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);

byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
RijndaelManaged SymmetricKey = new RijndaelManaged();
SymmetricKey.Mode = CipherMode.CBC;
byte[] CipherTextBytes = null;

using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes))
{
using (MemoryStream MemStream = new MemoryStream())
{
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
{
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
CryptoStream.FlushFinalBlock();
CipherTextBytes = MemStream.ToArray();
MemStream.Close();
CryptoStream.Close();
}
}
}
SymmetricKey.Clear();
encryptedPassword = Convert.ToBase64String(CipherTextBytes);

MessageBox.Show("Encrypted password: " + encryptedPassword);
}

我不介意更改我的 Java Web 服务解密的方式以使其与我的 C# 应用程序一起工作。

câu trả lời hay nhất

在 C# 中,您使用 DeriveBytes 函数从密码中获取 key ,而在 Java 中,您直接使用密码作为 key 。

这样你显然在两边都有不同的 key 。不要这样做,两边使用相同的 key 推导函数。

关于c# - C#(加密)与Java(解密)之间的AES加密/解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7957752/

33 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress