2

At the moment, I'm working on a project that involves encryption. Therefore, I have a class, which worked fine in .NET 4.0, but now that I changed it so it would work in .NET 2.0, it fails to encrypt almost any string... I don't know why, but it fails every time and throws a CryptographicException. Decrypting works fine as far as I can tell, and everything else too. Anyway, here's the code:

public class Encryption
{
    static readonly string PasswordHash = "P@@Sw0rd";
    static readonly string SaltKey = "S@LT&KEY";
    static readonly string VIKey = "@1B2c3D4e5F6g7H8";
    public static string Encrypt(string plainText, string passwordHash)
    {
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
        byte[] keyBytes = new Rfc2898DeriveBytes(passwordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
        RijndaelManaged symmetricKey = new RijndaelManaged();
        symmetricKey.Mode = CipherMode.CBC;
        symmetricKey.Padding = PaddingMode.None;
        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
        byte[] cipherTextBytes;
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
            {
                cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                cryptoStream.FlushFinalBlock();
                cipherTextBytes = memoryStream.ToArray();
                cryptoStream.Close();
            }
            memoryStream.Close();
        }
        return Convert.ToBase64String(cipherTextBytes);
    }
    public static string Decrypt(string encryptedText, string passwordHash)
    {
        byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
        byte[] keyBytes = new Rfc2898DeriveBytes(passwordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
        RijndaelManaged symmetricKey = new RijndaelManaged(); 
        symmetricKey.Mode = CipherMode.CBC;
        symmetricKey.Padding = PaddingMode.None;
        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
        MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
        CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
        byte[] plainTextBytes = new byte[cipherTextBytes.Length];
        int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
        memoryStream.Close();
        cryptoStream.Close();
        return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
    }
}

The error message is (translated from German):

CryptographicException: Length of the data to encrypt is invalid. at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) at System.Security.Cryptography.CryptoStream.FlushFinalBlock() at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)

6
  • You said "almost any string". Please show the strings that work. Also, where is the exception thrown? Commented Feb 14, 2015 at 20:31
  • You need to give more details than just "throws a CryptographicException". What is the exact error and message? From what I can tell, you should not be setting the Padding to None because arbitrary strings aren't going to completely fill blocks. Just remove that line from Encrypt. Commented Feb 14, 2015 at 20:37
  • Encryption.Encrypt("Client connected!", "elit3Nase") does not work Encryption.Encrypt("Client connectedasdssssssssssssssssssssssssssss!", "elit3Nase") does work... I was just playing around and spammed some chars, and it suddently worked Commented Feb 14, 2015 at 20:43
  • Just try it, there's a CryptographicException on cryptoStream.FlushFinalBlock(); call. Commented Feb 14, 2015 at 20:44
  • Yeah that's it, and I don't know how to solve it... Commented Feb 14, 2015 at 20:45

1 Answer 1

1

Encryption.Encrypt("Client connected!", "elit3Nase") does not work

"Client connected!" is not long enough to fully fill a block (16 bytes) and you are not using padding:

symmetricKey.Padding = PaddingMode.None;

Change to symmetricKey.Padding = PaddingMode.PKCS7; to ensure padding is applied.

Sign up to request clarification or add additional context in comments.

2 Comments

Would it alternatively be possible to change the password? And would a 16+ bytes long password solve the problem?
Sorry I mean the input text "Client connected!" is not a multiple of the block size (128 bits = 16 bytes) - yes you can add characters as needed to make sure it always is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.