【问题标题】:AES Crypto from Java to C#从 Java 到 C# 的 AES 加密
【发布时间】:2020-05-11 18:06:20
【问题描述】:

我在 Java 中有这段代码,我成功地加密了字符串。

public AESCrypt(String password) throws Exception
{
    // hash password with SHA-256 and crop the output to 128-bit for key
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    digest.update(password.getBytes("UTF-8"));

    byte[] keyBytes = new byte[32];
    System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);

    cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    key = new SecretKeySpec(keyBytes, "AES");
    spec = getIV();
}

public AlgorithmParameterSpec getIV()
{
    byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
    IvParameterSpec ivParameterSpec;
    ivParameterSpec = new IvParameterSpec(iv);

    return ivParameterSpec;
}

public String encrypt(String plainText) throws Exception
{
    cipher.init(Cipher.ENCRYPT_MODE, key, spec);
    byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
    String encryptedText = new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8");

    return encryptedText;
}

示例:我有字符串 093949,使用上面的代码我们得到“b28VNQIxfmlCfLcjQRS7Dw=="

在 C# 中,我试图获得相同的结果,但我做错了,我被卡住了。 我在 java 中需要同样的结果

C#

 public RijndaelManaged GetRijndaelManaged(String secretKey)
    {
        var keyBytes = new byte[16];
        var secretKeyBytes = Encoding.UTF8.GetBytes(secretKey);



        Array.Copy(secretKeyBytes, keyBytes, Math.Min(keyBytes.Length, secretKeyBytes.Length));
        return new RijndaelManaged
        {
            Mode = CipherMode.CBC,
            Padding = PaddingMode.PKCS7,
            KeySize = 128,
            BlockSize = 128,
            Key = keyBytes,
            IV = keyBytes
        };
    }

    public byte[] Encrypt(byte[] plainBytes, RijndaelManaged rijndaelManaged)
    {
        return rijndaelManaged.CreateEncryptor()
            .TransformFinalBlock(plainBytes, 0, plainBytes.Length);
    }

    public byte[] Decrypt(byte[] encryptedData, RijndaelManaged rijndaelManaged)
    {
        return rijndaelManaged.CreateDecryptor()
            .TransformFinalBlock(encryptedData, 0, encryptedData.Length);
    }


    // Encrypts plaintext using AES 128bit key and a Chain Block Cipher and returns a base64 encoded string

    public String Encrypt(String plainText, String key)
    {
        var plainBytes = Encoding.UTF8.GetBytes(plainText);
        return Convert.ToBase64String(Encrypt(plainBytes, GetRijndaelManaged(key)));
    }


    public String Decrypt(String encryptedText, String key)
    {
        var encryptedBytes = Convert.FromBase64String(encryptedText);
        return Encoding.UTF8.GetString(Decrypt(encryptedBytes, GetRijndaelManaged(key)));
    } 

我们这样调用c#方法:

string senhaencrypted = Encrypt(string_tobeencrypt, password);

这个回报给我:"Nv1V4RIJ1TczqnKGdN8jMA==" not "b28VNQIxfmlCfLcjQRS7Dw=="

如果有人知道它出了什么问题,请。 谢谢!

【问题讨论】:

  • 标题说 AES-256,代码注释说 AES 128bit。
  • 我尝试像在 java 代码中那样更改为 256,但我不能,我真的卡在这里我尝试逐行转换,因为我不知道 c# 很像 java。密码是“1OnePlaceServicesDecryptionKey”
  • 在以下情况下,您怎么能期望在 C# 中得到相同的答案:1)您使用不同的密钥大小,以及 2)您在 Java 中而不是在 C# 中散列密码?
  • hash em 256bits 是什么意思?我在这里尝试使用相同的密钥大小..但还有区别。

标签: java c# encryption aes


【解决方案1】:

这是从 Java 到 C# 的一个端口(就我可以解释 Java 代码而言,作为 C# 开发人员)。

但是,使用纯文本"093949" 和密码"1OnePlaceServicesDecryptionKey",代码不会生成"b28VNQIxfmlCfLcjQRS7Dw=="。所以还是有问题。但我认为下面的代码应该更接近原始代码。

public static string Encrypt(string plainText, string password)
{
    byte[] keyBytes;
    using (SHA256 hash = SHA256.Create())
    {
        keyBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(password));
    }

    using (Aes aes = Aes.Create())
    {
        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;
        aes.Key = keyBytes;
        aes.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        using (ICryptoTransform cryptoTransform = aes.CreateEncryptor())
        {
            byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
            byte[] encryptedBytes = cryptoTransform.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
            return Convert.ToBase64String(encryptedBytes);
        }
    }
}

【讨论】:

  • 谢谢你的回答我的朋友!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-06
  • 2017-04-15
  • 1970-01-01
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多