c# - C#(加密)与Java(解密)之间的AES加密/解密

标签 c# java aes encryption

我有一个 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# 中,您使用 DeriveBytes 函数从密码中获取 key ,而在 Java 中,您直接使用密码作为 key 。

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

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

相关文章:

java - ViewPager2 刷卡时闪烁/重新加载

java - 您如何处理数据处理的时区?

java - Commons Codec 中 decodeBuffer 方法的合适替代品

c# - GetHashCode 已实现,但 Dictionary 无法找到 key ?

c# - 推出 Beta 版时的 Google Play 游戏服务 : ERROR_NOT_AUTHORIZED,

c# - 将文件设置为只读直至 checkout

java - 使用 AES 和 Cipher 将 java 代码转换为 php

c# - 使用 MVVM 在 wpf 中使用对话框的好习惯还是坏习惯?

java - 在 hibernate 中的一个公共(public)位置进行逻辑删除

android - 在 Kotlin 中使用 ByteArrays 和 SecretKeySpec