Solved

Length of the data to decrypt is invalid

Posted on 2006-11-02
10
2,564 Views
Last Modified: 2008-01-09
I have thousands of encrypted data.

Which is encrypted by below code. Now I wish to decrypt it using the "public string Decrypt" method in the below code.

But it gives error when I try to decrypt my string saying Length of the data to decrypt is invalid

at line

csDecrypt.Read(decrypted, 0, decrypted.Length);

Can anyone help me

My code is given below

CryptionKey is:

private byte[] CryptionKey = {192,177,121,163,130,85,68,133,
                                     78,172,10,23,10,157,203,174,
                                     235,16,44,114,209,11,199,175,
                                     105,100,113,111,195,47,186,164};

Code is:

using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Com.Encryption
{
    /// <summary>
    /// class to encrypt and decrypt the data using Rijndael method
    /// </summary>
    public class DataCryptor
    {
        #region constants

        public const string STRING_TERMINATOR = "\0";

        #endregion

        #region local members

        private UnicodeEncoding textConverter;
        private RijndaelManaged myRijndael;
       

        //this will be send by the caller
        private byte[] cryptionKey;

        //hardcode this for all encryption
        private byte[] initializationVector = {227,190,12,8,133,67,10,7,
                                                                          112,152,51,45,89,197,211,191};

        #endregion local members

        #region constructors

        public DataCryptor(byte[] cryptionKey32Bit)
        {
            textConverter = new UnicodeEncoding();
            myRijndael = new RijndaelManaged();

            this.cryptionKey = cryptionKey32Bit;
        }

        #endregion constructors

        #region public methods

        public string Encrypt(string dataToEncrypt)
        {
            string result;
            byte[] encrypted;
            byte[] toEncrypt;

            ICryptoTransform encryptor = myRijndael.CreateEncryptor(this.cryptionKey, this.initializationVector);

            //Encrypt the data.
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

            toEncrypt = textConverter.GetBytes(dataToEncrypt);

            //Write all data to the crypto stream and flush it.
            csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
            csEncrypt.FlushFinalBlock();

            //Get encrypted array of bytes.
            encrypted = msEncrypt.ToArray();
            result = textConverter.GetString(encrypted);

            return result;
        }

        public string Decrypt(string dataToDecrypt)
        {
            string result;
            byte[] decrypted;
            byte[] toDecrypt;

            ICryptoTransform decryptor = myRijndael.CreateDecryptor(this.cryptionKey, this.initializationVector);

            toDecrypt = textConverter.GetBytes(dataToDecrypt);

            //Decrypt the data.
            MemoryStream msDecrypt = new MemoryStream(toDecrypt);
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

            decrypted = new byte[toDecrypt.Length];

            //Read the data out of the crypto stream.
            csDecrypt.Read(decrypted, 0, decrypted.Length);

            result = textConverter.GetString(decrypted).Replace(STRING_TERMINATOR, string.Empty);

            return result;
        }

        #endregion public methods
    }
}
0
Comment
Question by:dinesh_bali
  • 4
  • 3
10 Comments
 
LVL 13

Expert Comment

by:joechina
ID: 17859251
Can you try change you encoding from UnicodeEncoding to Base64 when return the result in encrypt?
Change
result = textConverter.GetString(encrypted);
To
result = Convert.ToBase64String(encrypted);

And in decrypt
Change
toDecrypt = textConverter.GetBytes(dataToDecrypt);
To
toDecrypt = Convert.FromBase64String(dataToDecrypt);

I am thinking there might be 0 value byte in your encrypted byte array, it could cause the string terminated.


0
 

Author Comment

by:dinesh_bali
ID: 17859411


Unfortunately, I cannot change the encrypt method as thousands of users has registerd our site

so i can only change in dcrypt function

0
 

Author Comment

by:dinesh_bali
ID: 17859547
In .Net 2003 it is working fine

I am using .Net 2005. So please help me for .Net 2005
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Expert Comment

by:joechina
ID: 17860221
I have tested your code from my end.
For data "This is a test", the encryption and decryption work file.

Are all encrypted data have the problem or just some of data have it?
0
 

Author Comment

by:dinesh_bali
ID: 17864964
If u have 5 character then it will encrypt and dycrypt fine but if it is more than 5 or the lenght of string is big then it is not decrypting. But it encrypts the any length of characters.

What are you using? Its is working fine on .Net 1.1 but it is not working fine on .Net 2.0

Where have you tested it.

Regards,
Dinesh
0
 
LVL 13

Accepted Solution

by:
joechina earned 500 total points
ID: 17866510
My test environment is VS 2005 .net 2.0

Can you try encrypt and decrypt '1234567'?

Here are the code I used to test, the same as yours I just changed them to static

class Program
    {
        static RijndaelManaged myRijndael = new RijndaelManaged();
        static byte[] cryptionKey = {192,177,121,163,130,85,68,133,
                               78,172,10,23,10,157,203,174,
                               235,16,44,114,209,11,199,175,
                               105,100,113,111,195,47,186,164};
        static byte[] initializationVector = {227,190,12,8,133,67,10,7,
                                                              112,152,51,45,89,197,211,191};

        static UnicodeEncoding textConverter = new UnicodeEncoding();

        static string STRING_TERMINATOR = "\0";


        static void Main(string[] args)
        {
            Console.WriteLine(Decrypt(Encrypt("j123456")));
            Console.Read();
        }
        static public string Encrypt(string dataToEncrypt)
        {
            string result;
            byte[] encrypted;
            byte[] toEncrypt;

            ICryptoTransform encryptor = myRijndael.CreateEncryptor(cryptionKey, initializationVector);
            //Encrypt the data.
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

            toEncrypt = textConverter.GetBytes(dataToEncrypt);

            //Write all data to the crypto stream and flush it.
            csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
            csEncrypt.FlushFinalBlock();

            //Get encrypted array of bytes.
            encrypted = msEncrypt.ToArray();
            result = textConverter.GetString(encrypted);

            return result;
        }

        static public string Decrypt(string dataToDecrypt)
        {
            string result;
            byte[] decrypted;
            byte[] toDecrypt;

            ICryptoTransform decryptor = myRijndael.CreateDecryptor(cryptionKey, initializationVector);

            toDecrypt = textConverter.GetBytes(dataToDecrypt);

            //Decrypt the data.
            MemoryStream msDecrypt = new MemoryStream(toDecrypt);
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

            decrypted = new byte[toDecrypt.Length];

            //Read the data out of the crypto stream.
            csDecrypt.Read(decrypted, 0, decrypted.Length);

            result = textConverter.GetString(decrypted).Replace(STRING_TERMINATOR, string.Empty);

            return result;
        }

    }
0
 
LVL 13

Expert Comment

by:joechina
ID: 17866525
I would suggest you write a small program, do a loop to decrypt all encrypted string, capture exception and in the exception output the dataToDecrypt. Then analyze those problem input.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now