0

I am encrypting some text and converting it to base64 and upon decryption I appear to have some odd characters as it blocks the rest of the output...

For example, I want to encrypt 10,2013-12-16 10:22:24,1387189344 which converts to MaEA8gd7Xyg8tNBrtVBlb75U/a0J9x5x7UryI81gy3R+ZvL01p05uzDtzdBWL5Pg (base64) and then when I decrypt I try to print the output as "[" + decrypted + "]" but the second bracket doesn't come out.....

Here's my code. Thanks in advance.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataSync;
using System.IO;
using System.Security.Cryptography;

namespace EncryptionTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // encrypt
        private void button1_Click(object sender, EventArgs e)
        {
            txtOutput.Text = Encrypt(txtData.Text, txtCIV.Text, txtSecret.Text);

        }

        // decrypt
        private void button2_Click(object sender, EventArgs e)
        {
            txtOutput.Text = "[" + Decrypt(txtData.Text, txtCIV.Text, txtSecret.Text) + "\r\n" + "]";
        }



        public string GenerateIV()
        {
            RijndaelManaged RM = new RijndaelManaged();
            RM.KeySize = 128;
            RM.BlockSize = 128;
            RM.Mode = CipherMode.CBC;
            RM.Padding = PaddingMode.Zeros;

            RM.GenerateIV();

            return Convert.ToBase64String(RM.IV);
        }

        public string Decrypt(string Encrypted, string IV, string SecretKey)
        {
            byte[] DecryptThis = Convert.FromBase64String(Encrypted);
            byte[] DecryptIV = Convert.FromBase64String(IV);
            byte[] Key = Encoding.UTF8.GetBytes(SecretKey);

            SHA256 SHA256Obj = SHA256.Create();
            byte[] PrivateKeyHash = SHA256Obj.ComputeHash(Key, 0, Key.Length);

            RijndaelManaged RM = new RijndaelManaged();
            RM.Mode = CipherMode.CBC;
            RM.Padding = PaddingMode.Zeros;

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, RM.CreateDecryptor(PrivateKeyHash, DecryptIV), CryptoStreamMode.Write);

            cs.Write(DecryptThis, 0, DecryptThis.Length);
            cs.FlushFinalBlock();
            cs.Close();          

            // Strip null (padding) characters
            int Position = -1;
            foreach (byte Char in ms.ToArray())
            {
                ++Position;
                if (Char == 0)
                {
                break;
                }
            }

            string Temp = Encoding.UTF8.GetString(ms.ToArray());

            return Temp.Substring(0, Position+1).Trim();              
        }

        public string Encrypt(string ToBeEncrypted, string IV, string SecretKey)
        {
            byte[] EncryptThis = Encoding.UTF8.GetBytes(ToBeEncrypted);
            byte[] EncryptIV = Convert.FromBase64String(IV);
            byte[] Key = Encoding.UTF8.GetBytes(SecretKey);

            SHA256 SHA256Obj = SHA256.Create();
            byte[] PrivateKeyHash = SHA256Obj.ComputeHash(Key, 0, Key.Length);

            RijndaelManaged RM = new RijndaelManaged();
            RM.Mode = CipherMode.CBC;
            RM.Padding = PaddingMode.Zeros;

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, RM.CreateEncryptor(PrivateKeyHash, EncryptIV), CryptoStreamMode.Write);

            cs.Write(EncryptThis, 0, EncryptThis.Length);
            cs.FlushFinalBlock();
            cs.Close();

            return Convert.ToBase64String(ms.ToArray());
        }
    }
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
Anonymous
  • 575
  • 1
  • 4
  • 11
  • Looks like you are inserting a newline before the closing bracket? `txtOutput.Text = "[" + Decrypt(txtData.Text, txtCIV.Text, txtSecret.Text) + "\r\n" + "]";` – Håkan Fahlstedt Jan 21 '14 at 09:57
  • 1
    Put the code in the question itself, having reduced it as far as possible. Then read http://msmvps.com/blogs/jon_skeet/archive/2014/01/20/diagnosing-issues-with-reversible-data-transformations.aspx – Jon Skeet Jan 21 '14 at 09:57
  • **FYI** to those who find this question, in reference to the code above, sha256 should not be used as a KDF, and padding with zeros is problematic. If you don't know this, you should use a library that handles encryption details for you. – jbtule Jan 21 '14 at 16:02

2 Answers2

0

You have a line break in your code here:

txtOutput.Text = "[" + Decrypt(txtData.Text, txtCIV.Text, txtSecret.Text) + "\r\n" + "]";
                                                                              ^^^

You have to place the line break after the bracket:

txtOutput.Text = "[" + Decrypt(txtData.Text, txtCIV.Text, txtSecret.Text) + "]" + "\r\n";

Also I am not sure if you even need the line break. Are you using a multiline textbox?

RononDex
  • 4,143
  • 22
  • 39
  • @Anonymous were you able to fix your problem with an answer provided here? If yes pls accept the best answer and close the question – RononDex Jan 23 '14 at 09:45
0

You are not removing all the "\0" in your Decrypt() method. at line 82, replace

return Temp.Substring(0, Position + 1).Trim();

by

return Temp.Substring(0, Position).Trim();
RawBean
  • 442
  • 5
  • 20