C
C#2y ago
hippiewho

CryptoStream decrypt encrypt

I have a class to encrypt and decrypt strings but for some reason i get an exception: System.Security.Cryptography.CryptographicException: 'Padding is invalid and cannot be removed.'
public class BasicEncryption
{
public string Encrypt(string rawValue)
{
using Aes aes = Aes.Create();
using ICryptoTransform encryptor = aes.CreateEncryptor();
using MemoryStream memoryStream = new MemoryStream();
using CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
using StreamWriter swEncrypt = new StreamWriter(cryptoStream);
swEncrypt.Write(rawValue);
swEncrypt.Flush();
var memBytes = memoryStream.ToArray();
return Convert.ToBase64String(memBytes);
}

public string Decrypt(string encryptedValue)
{
using Aes aes = Aes.Create();
byte[] encryptedData = Convert.FromBase64String(encryptedValue);
using ICryptoTransform decryptor = aes.CreateDecryptor();
using MemoryStream memoryStream = new MemoryStream(encryptedData);
using CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
using StreamReader srDecrypt = new StreamReader(cryptoStream);
string plaintext = srDecrypt.ReadToEnd();
return plaintext;
}
}
public class BasicEncryption
{
public string Encrypt(string rawValue)
{
using Aes aes = Aes.Create();
using ICryptoTransform encryptor = aes.CreateEncryptor();
using MemoryStream memoryStream = new MemoryStream();
using CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
using StreamWriter swEncrypt = new StreamWriter(cryptoStream);
swEncrypt.Write(rawValue);
swEncrypt.Flush();
var memBytes = memoryStream.ToArray();
return Convert.ToBase64String(memBytes);
}

public string Decrypt(string encryptedValue)
{
using Aes aes = Aes.Create();
byte[] encryptedData = Convert.FromBase64String(encryptedValue);
using ICryptoTransform decryptor = aes.CreateDecryptor();
using MemoryStream memoryStream = new MemoryStream(encryptedData);
using CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
using StreamReader srDecrypt = new StreamReader(cryptoStream);
string plaintext = srDecrypt.ReadToEnd();
return plaintext;
}
}
7 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
hippiewho
hippiewho2y ago
not related
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Scratch
Scratch2y ago
Stack Overflow
Padding is invalid and cannot be removed?
I have looked online for what this exception means in relation to my program but can't seem to find a solution or the reason why it's happening to my specific program. I have been using the example
hippiewho
hippiewho2y ago
I read this as well... And it does seem to be a padding issue but none of the padding options work... I changed it up a bit and i was able to get the decrypt method to decrypt most of the string.... The last few bits are missing. I ended up adding a few characters to the rawValue string and it decrypted the entire string correctly (with the padding characters I added). Still so lost
Scratch
Scratch2y ago
did you set alg.Padding = PaddingMode.PKCS7; on both the encryption and decryption Aes object?
hippiewho
hippiewho2y ago
Yea I did...