nicedev92
nicedev92

Reputation: 1705

Rijndael decryption returning 16 character response only C#

I am using the Rijndael encryption and description method given below. When I encrypt a string of 16+ characters and then decrypt it, it returns a maximum of 16 characters even when the input used to encrypt is longer than 16. Sample input: "ABCDEFGHIJKLMNOP12345678" Encrypted using the method in the given code and then decrypted. Decrypted output: "ABCDEFGHIJKLMNOP" hashAlgorithm = "SHA1" passwordIterations = 2 keySize = 256

How can I fix the current code to encrypt/decrypt longer strings?

Here is the code

public class RijndaelSimple
{
    public static string Encrypt(string plainText,
                                 string passPhrase,
                                 string saltValue,
                                 string hashAlgorithm,
                                 int passwordIterations,
                                 string initVector,
                                 int keySize)
    {
        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();

        
        symmetricKey.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;
    }

    
    public static string Decrypt(string cipherText,
        string passPhrase,
        string saltValue,
        string hashAlgorithm,
        int passwordIterations,
        string initVector,
        int keySize)
    {
        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

        byte[] cipherTextBytes = Convert.FromBase64String(cipherText);

        PasswordDeriveBytes password = new PasswordDeriveBytes(
            passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);

        byte[] keyBytes = password.GetBytes(keySize / 8);

        RijndaelManaged symmetricKey = new RijndaelManaged();

        symmetricKey.Mode = CipherMode.CBC;

        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);

        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();

        string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);

        return plainText;
    }
}

Upvotes: 0

Views: 196

Answers (1)

nicedev92
nicedev92

Reputation: 1705

Used ReadToEnd() with StreamReader and it works fine now.

using var plainTextReader = new StreamReader(cryptoStream);
return plainTextReader.ReadToEnd();

Here is the complete code for the Decrypt method:

public static string Decrypt(string cipherText,
    string passPhrase,
    string saltValue,
    string hashAlgorithm,
    int passwordIterations,
    string initVector,
    int keySize)
{
    byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
    byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

    byte[] cipherTextBytes = Convert.FromBase64String(cipherText);

    PasswordDeriveBytes password = new PasswordDeriveBytes(
        passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);

    byte[] keyBytes = password.GetBytes(keySize / 8);

    RijndaelManaged symmetricKey = new RijndaelManaged();

    symmetricKey.Mode = CipherMode.CBC;

    ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);

    MemoryStream memoryStream = new MemoryStream(cipherTextBytes);

    CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                  decryptor,
                                                  CryptoStreamMode.Read);

    using var plainTextReader = new StreamReader(cryptoStream);
    return plainTextReader.ReadToEnd();
}

Upvotes: 1

Related Questions