RFC2898DeriveBytes在Java中的实现 [英] RFC2898DeriveBytes implementation in Java

查看:193
本文介绍了RFC2898DeriveBytes在Java中的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to decrypt a string encrypted in C# as a part of our project. This decryption is done using AES algorithm and packing mode as PKCS7. For generating the initialization vector they have used the following:

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

salt是默认字节。

The salt is the default bytes.

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

This IV is used in encrypting the string using AES.

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

I have read through some documents and found that AES can be implemented in Java. But not sure on how to pass the IV and packing mode.

另外,我已经看到CBC,ECB模式提到了Cipher块模式。我不确定C#对应的模式是什么。

Also, I have seen that there are modes CBC, ECB for mentioning the Cipher block mode. I am not sure what mode is used in C# counterpart.

以下是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中创建等价物吗?

Can any one help me to create the equivalent in Java?

推荐答案

我以某种方式想出办法。它工作正常

i have somehow figured a way. it was working properly

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

I have requested the .net counterpart to pass the key and IV as strings. I encoded them to byte[] and used the below code

 String sKey ="fromdotnetpart";

 String sIv="fromdotnetiv";

    byte[] bKey = key.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.getByte());

希望这对您有所帮助。请注意,对于更高强度的AES加密,即AES-256,192等,您需要下载 Java密码术扩展(JCE)无限强度管辖政策文件

Hope this helps you. Please note that for higher strength AES encryption i.e AES-256,192 etc You need to download the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files

这篇关于RFC2898DeriveBytes在Java中的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆