私はC#で暗号化された文字列をプロジェクトの一部として復号化する必要があります。この解読は、PKCS7としてAESアルゴリズムとパッキングモードを使用して行われます。初期化ベクトルを生成するために、以下を使用した。JavaのRFC2898DeriveBytes実装
Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes("somestring", salt);
塩はデフォルトのバイトである。
このIVは、AESを使用して文字列を暗号化する際に使用されます。
私はいくつかのドキュメントを読み、AESをJavaで実装できることを発見しました。しかし、IVとパッキングモードに合格する方法は不明です。
また、Cipherブロックモードについて言及すると、CBC、ECBモードがあることがわかりました。私はどのモードがC#のカウンターパートで使われているのかよくわかりません。以下は
コードはC#の
/// Method to encrypt the plain text based on the key and Iv
/// </summary>
/// <param name="plainText"></param>
/// <param name="key"></param>
/// <returns>encrypted Text</returns>
private string Encrypt(string plainText, byte[] key)
{
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("Key");
// Declare the stream used to encrypt to an in memory
// array of bytes.
MemoryStream msEncrypt = null;
// Declare the RijndaelManaged object
// used to encrypt the data.
AesCryptoServiceProvider aesAlg = null;
// using (new Tracer("Encryption","",""))
// {
try
{
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new AesCryptoServiceProvider();
aesAlg.Key = key;
aesAlg.IV = GetInitializationVector();
aesAlg.Padding = PaddingMode.PKCS7;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
msEncrypt = new MemoryStream();
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
// Clear the RijndaelManaged object.
if (aesAlg != null)
aesAlg.Clear();
}
// Return the encrypted bytes from the memory stream.
// Console.WriteLine();
return Convert.ToBase64String(msEncrypt.ToArray());
// }
}
private byte[] GetInitializationVector()
{
byte[] iv;
//create the initial salt
byte[] salt = Encoding.Default.GetBytes("abcdefghijkl");
//create the key generator
Rfc2898DeriveBytes keyGenerator = new Rfc2898DeriveBytes("ricksaw", salt);
iv = keyGenerator.GetBytes(16);
return iv;
}
にいずれかがJavaで同等のものを作成するために私を助けることができるのですか?
が 'Rfc2898DeriveBytes'がPBKDF2 – CodesInChaos
に基づいてダウンロードする必要があることに注意してくださいので、PBKDF2 – VamsiKrishna
@krishを実現するJavaのいずれかのクラスがあります - 私はわかりません。 Re:* C#暗号モード* [デフォルトはcbc](http://msdn.microsoft.com/en-us/library/system.security.cryptography) .symmetricalgorithm.mode.aspx)。また、これまでのところあなたのJavaコードを投稿できますか? – Leigh