我正在尝试将现有的 C# 加密方法转换为 Java,但遇到了如下障碍
例如,当我用 c# 加密一个基本字符串“12345”时,我得到这个输出 8ZQZEURctqP1PMmQxVtCcA==
当我用 java 加密相同的字符串时,我得到了这个 jkEZp2cfeGXVE/IxIW6X3g==
private static string Encrypt(string plainText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations,
string initVector, int keySize)
{
try
{
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = password.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged { Mode = CipherMode.CBC };
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
string cipherText = Convert.ToBase64String(cipherTextBytes);
return cipherText;
}
catch (Exception execp)
{
MessageBox.Show(string.Format("Exception in Encrypt function\r\nError: {0}", execp.Message));
return "";
}
}
下面是我转换为 java 的内容,但仍然没有得到相同的加密输入和输出 - 我只是将“ProtectPassword”重命名为“Encrypt”,将“UnprotectPassword”重命名为“Decrypt”
喵喔喔
烙印99
相关分类