【发布时间】:2012-04-10 11:22:54
【问题描述】:
我有一个使用 Rijndael 加密的 mp4,我正在 C# 中以下列方式解密。
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 等?
更新
我在 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
更新
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 提供了哪些值?
【问题讨论】:
标签: java android encryption