将加密Algo从C#转换为Java

时间:2016-08-22 06:09:09

标签: java rijndael cryptostream

public static string Encrypt(string KeyToEncrypt)
    {
        byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(KeyToEncrypt);
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_Pwd, _Salt);
        //Calling another private method for Encryption
        byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
        return Convert.ToBase64String(encryptedData);
    }

    private static byte[] Encrypt(byte[] candelaData, byte[] Key, byte[] IV)
    {
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = null;

            Rijndael alg = Rijndael.Create();
            alg.Key = Key;
            alg.IV = IV;
            cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(candelaData, 0, candelaData.Length);
            cs.FlushFinalBlock();
            return ms.ToArray();
     }

我想在java中转换以下算法,我搜索过这些库并且无法获得任何内容。请帮忙。 ?

1 个答案:

答案 0 :(得分:0)

Darab,你在Java中最好的选择是Bouncy Castle。当我从代码中读取时,你有用于加密的密码的API以及AES Rijndael。

对于Rejndael部分,您可以参考:http://www.itcsolutions.eu/2011/08/24/how-to-encrypt-decrypt-files-in-java-with-aes-in-cbc-mode-using-bouncy-castle-api-and-netbeans-or-eclipse/。这让您对代码的AES部分有了一个很好的了解,这里的问题Rfc2898DeriveBytes in java也让您对盐析和Rfc2898有了很好的了解。