우리 프로젝트의 일부로 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;
}
중 하나가 나에게 자바에서 동등한를 만드는 데 도움이 할 수있는 코드?
'Rfc2898DeriveBytes'이 너무 PBKDF2 – CodesInChaos
을 기반으로 - 나는 확실하지 않다. 자바 6에서 추가 된'PBKDF2WithHmacSHA1'을 살펴볼 것입니다. Re : * C# 암호 모드 * [기본값은 cbc입니다] (http://msdn.microsoft.com/en-us/library/system.security.cryptography .symmetricalgorithm.mode.aspx). 또한 지금까지 자바 코드를 게시 할 수 있습니까? – VamsiKrishna
@krish을 실현 자바의 모든 클래스가 PBKDF2 – Leigh