none
TripleDES - ICryptoTransform.TransformFinalBlock - Error: Invalid length for a Base-64 char array RRS feed

  • Question

  • I have a client/server application (winform) which draws shapes which are sent from client to server and vice versa. Data is also encrypted and decrypted with 3DES. When I send data from client to server I get 'Invalid length for a Base-64 char array' error, but it works properly when I send data from server to client although the same function is called in both cases. Bellow is the code (without draw function):

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Security.Cryptography;
    
    
    
    namespace VajaOmrezvaEna
    {
        public partial class Form1 : Form
        {
    
            bool SERVER = false;
            Socket socketForClient = null;
            int s = 5;
            TcpClient odjemalec = null;
            Image mainimage = Image.FromFile("bg.png");
            string key = "fp8IMJv4hdKrn8uid95i72NQ73v2w797";
    
    
            public Form1()
            {
                InitializeComponent();
            }
    
    
    
            private void serverButton_Click(object sender, EventArgs e)
            {
                SERVER = true;
                klijent.Hide();
                TcpListener tcpListener = new TcpListener(1234);
                tcpListener.Start();
                textStatus.AppendText("Server running..." + Environment.NewLine);
    
                try
                {
    
                    socketForClient = tcpListener.AcceptSocket();
    
                    if (socketForClient.Connected)
                    {
                        textStatus.AppendText("Client connected" + socketForClient.RemoteEndPoint.ToString() + Environment.NewLine);
    
                        NetworkStream networkStream = new NetworkStream(socketForClient);
                        StreamReader streamReader = new StreamReader(networkStream);
    
                        string line = streamReader.ReadLine();
                        textStatus.AppendText(line + Environment.NewLine);
    
                        string decrypted = Decryption(line, key);
                        textStatus.AppendText(decrypted + Environment.NewLine + Environment.NewLine);
                        narisi_lik(decrypted);
    
                    }
    
                }
                catch (Exception l)
                {
                    textStatus.AppendText(l.ToString());
                }
            }
    
    
            private void clientButton_Click(object sender, EventArgs e)
            {
                streznik.Hide();
                try
                {
                    odjemalec = new TcpClient();
                    textStatus.AppendText("Connecting... " + Environment.NewLine);
    
                    odjemalec.Connect("127.0.0.1", 1234);
    
                    textStatus.AppendText("Connected!" + Environment.NewLine);
                }
                catch (Exception l)
                {
                    Console.WriteLine("Error: " + l.Message);
                }
    
            }
            private void sendButton_Click(object sender, EventArgs e)
            {
    
                if (SERVER == true)
                {
                    //server
                    NetworkStream networkStream = new NetworkStream(socketForClient);
                    StreamReader streamReader = new StreamReader(networkStream);
                    StreamWriter streamWriter = new StreamWriter(networkStream);
                    string opis_lika = comboBox1.Text + "/" + comboBox2.Text + "/" + textVelikost.Text;
    
                    byte[] buffer = Encryption(opis_lika, key);
                    string encrypted = Convert.ToBase64String(buffer);
                    textStatus.AppendText(encrypted + Environment.NewLine);
                    
                    
                    streamWriter.WriteLine(encrypted);
                    streamWriter.Flush();
    
                    
                    string line = streamReader.ReadLine(); 
                    textStatus.AppendText(line + Environment.NewLine);
    
                    string decrypted = Decryption(line, key);
                    textStatus.AppendText(decrypted + Environment.NewLine + Environment.NewLine);
    
                    draw(decrypted);
    
                }
                else
                {
                    //client
                    NetworkStream networkStream = odjemalec.GetStream();
                    StreamReader streamReader = new StreamReader(networkStream);
                    StreamWriter streamWriter = new StreamWriter(networkStream);
                    string opis_lika = comboBox1.Text + "/" + comboBox2.Text + "/" + textVelikost.Text;
    
                    byte[] buffer = Encryption(opis_lika, key);
                    string encrypted = Convert.ToBase64String(buffer);
    
                    streamWriter.WriteLine(opis_lika);
                    streamWriter.Flush();
                    string line = streamReader.ReadLine();
                    textStatus.AppendText(line + Environment.NewLine);
    
                    string decrypted = Decryption(line, key);
                    textStatus.AppendText(decrypted + Environment.NewLine + Environment.NewLine);
    
                    draw(decrypted);
    
                }
            }
    
    
            public static byte[] Encryption(string PlainText, string key)
            {
                TripleDES des = CreateDES(key);
                ICryptoTransform ct = des.CreateEncryptor();
                byte[] input = Encoding.Unicode.GetBytes(PlainText);
                return ct.TransformFinalBlock(input, 0, input.Length);
            }
    
    
            public static string Decryption(string CypherText, string key)
            {
                byte[] b = Convert.FromBase64String(CypherText);
    
                TripleDES des = CreateDES(key);
                ICryptoTransform ct = des.CreateDecryptor();
                byte[] output = ct.TransformFinalBlock(b, 0, b.Length);
                return Encoding.Unicode.GetString(output);
            }
    
    
            static TripleDES CreateDES(string key)
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                TripleDES des = new TripleDESCryptoServiceProvider();
                des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key));
                des.IV = new byte[des.BlockSize / 8];
                return des;
            }
    
           
        }
    }

    What am I missing?



    • Edited by Pierre_E Thursday, January 24, 2013 1:13 PM
    • Moved by Jason Dot Wang Friday, January 25, 2013 4:45 AM This thread is about Windows Communication Foundation,Serialization,and Networking
    Thursday, January 24, 2013 12:37 PM

All replies

  • Hi Pierre_E,

      Welcome to MSDN Forum Support.

      We're doing research on this issue. It might take some time before we get back to you.

      Sincerely,

      Jason Wang




    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Friday, January 25, 2013 3:15 AM
  • Thank you Jason.

    Error occurs at this line:

    byte[] output = ct.TransformFinalBlock(b, 0, b.Length);

    Friday, January 25, 2013 10:04 AM