0

作为我们项目的一部分,我必须解密用 C# 加密的字符串。此解密使用 AES 算法和打包模式作为 PKCS7 完成。为了生成初始化向量,他们使用了以下内容:

Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes("somestring", salt);

salt 是默认字节。

该 IV 用于使用 AES 加密字符串。

我通读了一些文档,发现AES可以用Java实现。但不确定如何通过 IV 和打包模式。

此外,我还看到有 CBC、ECB 模式用于提及密码块模式。我不确定 C# 对应项中使用的是什么模式。

下面是 C# 中的代码

/// Method to encrypt the plain text based on the key and Iv
/// </summary>
/// <param name="plainText"></param>
/// <param name="key"></param>
/// <returns>encrypted Text</returns>
private string Encrypt(string plainText, byte[] key)
{
    if (plainText == null || plainText.Length <= 0)
        throw new ArgumentNullException("plainText");
    if (key == null || key.Length <= 0)
        throw new ArgumentNullException("Key");
    // Declare the stream used to encrypt to an in memory
    // array of bytes.
    MemoryStream msEncrypt = null;

    // Declare the RijndaelManaged object
    // used to encrypt the data.
    AesCryptoServiceProvider aesAlg = null;
  //  using (new Tracer("Encryption","",""))
  //  {
        try
        {
            // Create a RijndaelManaged object
            // with the specified key and IV.
            aesAlg = new AesCryptoServiceProvider();
            aesAlg.Key = key;
            aesAlg.IV = GetInitializationVector();
            aesAlg.Padding = PaddingMode.PKCS7;
            // Create an encryptor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for encryption.
            msEncrypt = new MemoryStream();
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {

                    //Write all data to the stream.
                    swEncrypt.Write(plainText);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            // Clear the RijndaelManaged object.
            if (aesAlg != null)
                aesAlg.Clear();
        }

        // Return the encrypted bytes from the memory stream.
        // Console.WriteLine();

        return Convert.ToBase64String(msEncrypt.ToArray());
  //  }
}

private byte[] GetInitializationVector()
{
    byte[] iv;
    //create the initial salt 

    byte[] salt = Encoding.Default.GetBytes("abcdefghijkl");

    //create the key generator

    Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes("ricksaw", salt);

    iv =  keyGenerator.GetBytes(16);

   return iv;
}

任何人都可以帮助我在 Java 中创建等价物吗?

4

1 回答 1

1

我想出了一个办法。它工作正常

我已要求 .net 对应方将密钥和 IV 作为字符串传递。我将它们编码为 byte[] 并使用以下代码

 String sKey ="fromdotnetpart";
    
 String sIv="fromdotnetiv";
    
    byte[] bKey = skey.getBytes();
    byte[] iv = sIv.getBytes();
    SecretKeySpec skey = new SecretKeySpec(bKey, "AES");   
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    AlgorithmParameterSpec param = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key,param);
    String decrypted = cipher.doFinal(encryptedString.getBytes());

希望这对您有所帮助。请注意,对于更高强度的 AES 加密,即 AES-256,192 等,您需要下载Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files

于 2012-04-19T12:13:59.530 回答