2016-08-22 121 views
0
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中轉換下面的算法,我已經搜索了這些庫並且找不到任何東西。請幫助。 ?將加密Algo從C#轉換爲Java

回答

0

Darab,你在Java中最好的選擇是Bouncy Castle。當我從代碼中讀取API時,您可以使用API​​中的crypt以及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這個問題給你一個salting和Rfc2898的好主意。