繁体   English   中英

Rijndael或AES匹配Java加密-带有盐和密钥

[英]Rijndael or AES to match java encryption - with salt and key

我需要使用盐和密钥来加密字符串以匹配Java加密,以便第三方提供程序可以解密另一侧的值。

我已经尝试了几篇StackOverflow文章,因为我不是加密专家,并且无法使用作为第三方提供者的SALT和KEY获得相同的加密字符串。

我需要知道在C#中使用哪种加密类型和方式来匹配此处使用的Java的AES加密

https://gist.github.com/ca958d5921d47c4c0a0f

好的-即使它在某种程度上作弊,我也知道了。 因为找不到任何与第三方提供的普通AES加密相匹配的加密技术,所以我要求他们将其更改为

密码cipher = Cipher.getInstance(“ AES / CBC / PKCS5Padding”);

这样,我修改了我的C#代码,终于使集成工作了:

public static string Encrypt2(string plainText)
    {
        string PassPhrase = "somepassphrase";
        string SaltValue = "somesalt";
        int PasswordIterations = 0; //amend to match java encryption iteration
        string InitVector = "someiv";
        int KeySize = 0; //amend to match java encryption key size

        byte[] initVectorBytes = Encoding.ASCII.GetBytes(InitVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(SaltValue);

        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

        Rfc2898DeriveBytes password = new Rfc2898DeriveBytes(
                                                        PassPhrase,
                                                        saltValueBytes,
                                                        PasswordIterations);

        byte[] keyBytes = password.GetBytes(KeySize / 8);
        RijndaelManaged symmetricKey = new RijndaelManaged();
        symmetricKey.Mode = CipherMode.CBC;

        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
                                                         keyBytes,
                                                         initVectorBytes);
        MemoryStream memoryStream = new MemoryStream();

        CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                     encryptor,
                                                     CryptoStreamMode.Write);

        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
        cryptoStream.FlushFinalBlock();
        byte[] cipherTextBytes = memoryStream.ToArray();

        memoryStream.Close();
        cryptoStream.Close();

        string cipherText = Convert.ToBase64String(cipherTextBytes);

        return cipherText;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM