Мне удалось собрать пример шифрования, как показано ниже, но во время расшифровки я получаю неверные данные (исключение).Как я должен расшифровать
Метод шифрования
public static string EncryptWithAes(string plainText, byte[] key, byte[] initiationVector)
{
byte[] cryptoBytes = Encoding.UTF8.GetBytes(plainText);
using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
{
aesAlgorithm.Key = key;
aesAlgorithm.IV = initiationVector;
aesAlgorithm.Mode = CipherMode.ECB;
using (ICryptoTransform encryptoTransform = aesAlgorithm.CreateEncryptor(aesAlgorithm.Key, aesAlgorithm.IV))
{
cryptoBytes = encryptoTransform.TransformFinalBlock(cryptoBytes, 0, cryptoBytes.Length);
}
}
return Convert.ToBase64String(cryptoBytes);
}
Метод расшифровки
public static string DecryptAesCryptoString(string cipherText, byte[] key, byte[] initiationVector)
{
byte[] decryptedByte;
using (RijndaelManaged aesAlgorithm = new RijndaelManaged())
{
aesAlgorithm.Key = key;
aesAlgorithm.IV = initiationVector;
aesAlgorithm.Mode = CipherMode.ECB;
using (ICryptoTransform decryptoTransform = aesAlgorithm.CreateDecryptor(aesAlgorithm.Key, aesAlgorithm.IV))
{
byte[] cipherBytes = Convert.FromBase64String(cipherText);
decryptedByte = decryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
}
}
return Encoding.UTF8.GetString(decryptedByte);
}
Я думаю, что проблема со всеми кодировками, которые идут внутри этих методов
Пример данных
plainText = stackoverflow
base64encoded Key = B8Y/6doxwqU870C6jzYWhsr3hKSLokAOkkLCDiy+TS4=
(его легко преобразовать в байты, не так ли)
base64encoded IV = NZIpD60eBmdsOFFhA2bfvw==
encryptedValue = 77+977+977+977+977+9Ce+/ve+/vQ3vv70F77+9UzHvv73vv70=
Я предоставляю то же зашифрованное значение, IV и ключ для расшифровки в Stackoverflow