Search code examples
c#cryptographymemorystreamdes.net-4.5.2

DESCryptoServiceProvider decryption not working. Getting output with \0 character and just first letter


I am creating a license from the following code with encryption as per the following logic:

private void SaveLicenseInfoWithEncryption(LicenseInfo obj)
                {

                    string strLicenseString = "Kamran";
                    //string strLicenseString = JsonConvert.SerializeObject(obj, Formatting.None);
                    byte[] buffer = new byte[strLicenseString.Length * sizeof(char)];
                    Buffer.BlockCopy(strLicenseString.ToCharArray(), 0, buffer, 0, buffer.Length);


                    DESCryptoServiceProvider desCryptSrvckey = new DESCryptoServiceProvider
                    {
                        Key = new UTF8Encoding().GetBytes(ConfigurationManager.AppSettings["EncryptionKey"])
                    };
                    desCryptSrvckey.IV = desCryptSrvckey.Key;

                    using (MemoryStream stmCipherText = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(stmCipherText, desCryptSrvckey.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            cs.Write(buffer, 0, buffer.Length);
                            cs.FlushFinalBlock();
                            string licPath = Path.Combine(txtFolderPath.Text, @"Test.lic");
                            File.WriteAllBytes(licPath, stmCipherText.ToArray());
                            MessageBox.Show(@"License File Generated Successfully");
                        }
                    }
                }

The license is generating fine (I have temporarily put sample text in). The problem is that I am trying to decrypt my license and I am getting some invalid behavior.

My code is:

private void button1_Click(object sender, EventArgs e)
        {
            var fileDialog = new OpenFileDialog
            {
                Filter = @"License Files|*.lic",
                Title = @"Select a License File"
            };
        if (fileDialog.ShowDialog() == DialogResult.OK)
        {

            byte[] buffer = File.ReadAllBytes(fileDialog.FileName);

            DESCryptoServiceProvider desCryptSrvckey = new DESCryptoServiceProvider
            {
                Key = new UTF8Encoding().GetBytes(ConfigurationManager.AppSettings["EncryptionKey"])
            };
            desCryptSrvckey.IV = desCryptSrvckey.Key;

            using (MemoryStream encryptedStream = new MemoryStream(buffer))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(encryptedStream, desCryptSrvckey.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        using (StreamWriter sw = new StreamWriter(ms))
                        {
                            using (StreamReader sr = new StreamReader(cs))
                            {
                                sw.Write(sr.ReadToEnd());
                                sw.Flush();
                            }


                            ms.Position = 0;
                            txtContent.Text = new StreamReader(ms).ReadToEnd();
                        }
                    }
                }
            }

        }
    }

In the watch window I am getting text K\0a\0m\0r\0a\0n\0 but the final output in the textbox is just K.


Solution

  • Lose the BlockCopy and convert the string as UTF-8:

    buffer = Encoding.UTF8.GetBytes(strLicenseString); 
    

    Or use your current code & tell the reader the text is UTF-16 (Which it is as thats the internal string encoding & is what ends up in your buffer when you do a raw block copy)

    new StreamReader(cs, Encoding.Unicode)
    

    You can also simplify to:

    ...
    
    desCryptSrvckey.IV = desCryptSrvckey.Key;
    
    using (MemoryStream encryptedStream = new MemoryStream(buffer))
    using (CryptoStream cs = new CryptoStream(encryptedStream, desCryptSrvckey.CreateDecryptor(), CryptoStreamMode.Read))
    using (StreamReader sr = new StreamReader(cs))
    {
        txtContent.Text = sr.ReadToEnd();
    }