Click here to Skip to main content
15,039,714 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys..
am trying to encrypt and decrypt a string value.
String is getting encrypted successfully, but while decrypting it gives me cryptographic exception " Length of the data to decrypt is invalid"
Can you please help me with this??
Here's my code behind.
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.Security.Cryptography;
using System.IO;
namespace EncryptDecrypt
{
    public partial class Form1 : Form
    {
        private byte[] key = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
        private byte[] iv = { 65,110,68,26,69,178,200,219};
        byte[] inputinbytes;
        UTF8Encoding obj;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEncrypt_Click(object sender, EventArgs e)
        {
           Byte[] abc= Encrypt(textBox1.Text);
           FileStream fs = new FileStream(@"C:\temp\abc.txt", FileMode.Create, FileAccess.Write);
           fs.Write(abc, 0, abc.Length);
           fs.Close();
        }


        public Byte[] Encrypt(String plaintext)
        {
            byte[] result=new byte[1024];
            try
            {
                obj = new UTF8Encoding();
                inputinbytes = obj.GetBytes(textBox1.Text);
                TripleDESCryptoServiceProvider objtriple = new TripleDESCryptoServiceProvider();
                ICryptoTransform cryptotransform = objtriple.CreateEncryptor(this.key, this.iv);
                MemoryStream encryptedstream = new MemoryStream();
                CryptoStream cryptstream = new CryptoStream(encryptedstream, cryptotransform, CryptoStreamMode.Write);
                cryptstream.Write(inputinbytes, 0, inputinbytes.Length);
                cryptstream.FlushFinalBlock();
                encryptedstream.Position = 0;
                result = new byte[encryptedstream.Length-1];
                encryptedstream.Read(result, 0, (int)encryptedstream.Length-1);
                cryptstream.Close();
                return result;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
                return result;
            }
            
        }


        public string Decrypt(byte[] input)
        {
            
            try
            {
                UTF8Encoding utf8encoder = new UTF8Encoding();
                TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
                ICryptoTransform cryptotransform = tdesProvider.CreateDecryptor(this.key, this.iv);
                MemoryStream decryptedstream = new MemoryStream();
                CryptoStream cryptstream = new CryptoStream(decryptedstream, cryptotransform, CryptoStreamMode.Write);
                cryptstream.Write(input, 0, input.Length);
                cryptstream.FlushFinalBlock();
                //cryptstream.Position = 0;
                byte[] result = new byte[decryptedstream.Length-1];
                decryptedstream.Read(result, 0,(int) decryptedstream.Length-1);
                cryptstream.Close();
                UTF8Encoding ob = new UTF8Encoding();
                return ob.GetString(result);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
                return "false";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(@"C:\temp\abc.txt", FileMode.Open, FileAccess.Read);
            byte[] abc=new byte[fs.Length];
            fs.Read(abc, 0, (int)fs.Length);
            string name= Decrypt(abc);
            MessageBox.Show("" + name);
        }
    }
}
Posted
Updated 28-May-11 0:17am
v2

1 solution

Could it have anything to do with the minus one you are using?
result = new byte[encryptedstream.Length-1];
encryptedstream.Read(result, 0, (int)encryptedstream.Length-1);
Isn't it possible that ALL bytes in an encrypted stream are relevant?
   
Comments
Kim Togo 28-May-11 7:31am
   
I think you are right Griff. My 5 for that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900