[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 675
  • Last Modified:

Encryption Missing Last Byte

Below are my encrypt and decrypt methods. When I run this code on a file it appears to be working correctly except it leaves off the last byte of the file.

Example Text File:

Hello World!

returns

Hello World
(no exclamation on the end)
public static MemoryStream Encrypt(string key, Stream stream)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            ICryptoTransform desEncrypt;
            CryptoStream cryptoStream;
            MemoryStream memStream = new MemoryStream();

            //Setup encryption
            des.Key = ASCIIEncoding.ASCII.GetBytes(key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(key);
            desEncrypt = des.CreateEncryptor();
            cryptoStream = new CryptoStream(memStream, desEncrypt, CryptoStreamMode.Write);

            //Read stream into encrypted stream
            byte[] byteArrayInput = new byte[stream.Length - 1];
            stream.Read(byteArrayInput, 0, byteArrayInput.Length);
            stream.Flush();
            cryptoStream.Write(byteArrayInput, 0, byteArrayInput.Length);

            //Cleanup
            cryptoStream.Flush();
            cryptoStream.Close();
            memStream.Flush();
            stream.Close();

            //Return result
            return memStream;
        }


public static MemoryStream Decrypt(string key, Stream stream)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            ICryptoTransform desDecrypt;
            CryptoStream cryptoStream;
            MemoryStream memStream = new MemoryStream();

            //Setup encryption
            des.Key = ASCIIEncoding.ASCII.GetBytes(key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(key);
            desDecrypt = des.CreateDecryptor();
            cryptoStream = new CryptoStream(stream, desDecrypt, CryptoStreamMode.Read);
            
            //Read strem into decrypted stream
            StreamReader rdr = new StreamReader(cryptoStream);
            StreamWriter wrt = new StreamWriter(memStream);
            wrt.Write(rdr.ReadToEnd());
            cryptoStream.Flush();
            wrt.Flush();
 
            //byte[] byteArrayInput = new byte[stream.Length - 1];
            //stream.Read(byteArrayInput, 0, byteArrayInput.Length);
            //stream.Flush();
            //cryptoStream.Read(byteArrayInput, 0, byteArrayInput.Length);

            //Clenaup
            cryptoStream.Close();
            rdr.Close();
            wrt.Close();

            //Return result
            return memStream;
        }

Open in new window

0
jmwheeler
Asked:
jmwheeler
1 Solution
 
stsanzCommented:
Why do you read only stream.Length-1 bytes from the stream ?
I guess this explains that.
0
 
käµfm³d 👽Commented:
I agree.
0
 
jmwheelerAuthor Commented:
Ugh. (Embarrased)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now