I’m trying to decrypt strings with aes using key and iv but when i use this code
public static string DecryptBS(string cipherTextGet, string keyGet, string ivGet)
{
byte[] ciphertext = Encoding.UTF8.GetBytes(cipherTextGet);
byte[] key = Encoding.UTF8.GetBytes(keyGet);
byte[] iv = Encoding.UTF8.GetBytes(ivGet);
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
byte[] decryptedBytes;
using (var msDecrypt = new System.IO.MemoryStream(ciphertext))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var msPlain = new System.IO.MemoryStream())
{
csDecrypt.CopyTo(msPlain);
decryptedBytes = msPlain.ToArray();
}
}
}
return Encoding.UTF8.GetString(decryptedBytes);
}
it gives me “The input data is not a complete block.” error i tried so many ways to fix but nothing changed
I want this code to give me a string output but it just throws error in
plaintext = srDecrypt.ReadToEnd();
line.
I use CBC and I’m using keys and iv’s that has right lengths.
Unhandled exception. System.Security.Cryptography.CryptographicException: The input data is not a complete block.
at System.Security.Cryptography.UniversalCryptoDecryptor.UncheckedTransformFinalBlock(ReadOnlySpan`1 inputBuffer, Span`1 outputBuffer)
at System.Security.Cryptography.UniversalCryptoDecryptor.UncheckedTransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.UniversalCryptoTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.ReadAsyncCore(Memory`1 buffer, CancellationToken cancellationToken, Boolean useAsync)
at System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Security.Cryptography.CryptoStream.CopyTo(Stream destination, Int32 bufferSize)
at SKYFALL_V0002.EncryptionSystem.DecryptBS(String cipherTextGet, String keyGet, String ivGet) in /Users/serhan/Desktop/SKYFALL/SKYFALL DEMOS/SKYFALL_V0002/EncryptionSystem.cs:line 53
It gives me this
New contributor
Sero King is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.