我有一個使用Rijndael加密的mp4,並且我正在以下面的方式在C#中解密。用於C#構造的Java中的加密/解密等效代碼
System.Security.Cryptography.Rijndael crypt = System.Security.Cryptography.Rijndael.Create();
crypt.Key = convertedSecureString;
byte[] initializationVectorLength = new byte[sizeof(int)];
CryptoStream cryptostream = new CryptoStream(inputStream, crypt.CreateDecryptor(), CryptoStreamMode.Read);
byte[] buffer = new byte[1024];
int len;
while ((len = cryptostream.Read(buffer, 0, buffer.Length)) > 0)
{
outputStream.Write(buffer, 0, len);
buffer = new byte[1024];
}
outputStream.Flush();
cryptostream.Close();
inputStream.Close();
現在我需要將此代碼轉換爲Java/Android等效。我不知道從哪裏開始坦率地說。我很困惑這麼多的選擇 - 有人說使用Bouncy Castle,有人說Apache Commons,一些本地Java庫。我該怎麼做呢。我該如何處理CryptoStream等?
UPDATE
我正在使用C#代碼分配的關鍵
byte[] convertedSecureString = new byte[this.key.Length];
IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(this.key);
for (int i = 0, j = 0; i < this.key.Length * UnicodeByteLength; i = i + UnicodeByteLength, j++)
{
convertedSecureString[j] = System.Runtime.InteropServices.Marshal.ReadByte(ptr, i);
}
try
{
crypt.Key = convertedSecureString;
}
,其中關鍵是安全的下面。我有Java中等效的不安全密鑰。我如何這段代碼轉換成Java
UPDATE
Rfc2898DeriveBytes newKey = new Rfc2898DeriveBytes(crypt.Key.ToString(), crypt.IV);
Array.Copy(newKey.GetBytes((int)crypt.KeySize/8), crypt.Key, (int)crypt.KeySize/8);
Array.Copy(newKey.GetBytes((int)crypt.BlockSize/8), crypt.IV, (int)crypt.BlockSize/8);
我使用這個在C#國防部使用RFC 2898的關鍵和導出字節 - 我不能找到Java中的等價物 - 我在這裏發現Java equivalent of C#'s Rfc2898DerivedBytes在第二個評論 - 但我給了迭代器和dklen什麼值?
謝謝!但是我仍然不確定如何繼續 - 你在評論中展示給我的東西超出了我在C#代碼中的作用。我不知道如何將加密的數據文件轉換爲流,解密這些位並將其重新寫回文件 – Slartibartfast 2012-04-11 05:27:11
@Vrashabh:我已經用Java中的等效代碼更新了我的答案。 – 2012-04-11 07:26:43
感謝您的幫助!我會嘗試一下,做更多的研究,並回答任何問題或標記爲正確的答案:) – Slartibartfast 2012-04-11 08:09:12