Click here to Skip to main content
15,113,692 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Everytime i debuc i always get this message "length of the data to decrypt is invalid"

here is the PasswordEncryption class

C#
 public class PasswordEncryption
    {

         string passPhrass = "";
         public PasswordEncryption()
        {
            passPhrass = "P333awss";
        }
        private byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();

            Rijndael algorithm = Rijndael.Create();

            algorithm.Key = Key;
            algorithm.IV = IV;

            CryptoStream cs = new CryptoStream(ms, algorithm.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(clearData, 0, clearData.Length);
            cs.Close();

            byte[] encrytedData = ms.ToArray();

            return encrytedData;
        }
        public string Encrypt(string password)
        {
            byte[] clearBytes = Encoding.Unicode.GetBytes(password);
            byte[] saltBytes = 
                {
                    0x49, 
                    0x76,
                    0x61, 
                    0x6e, 
                    0x20, 
                    0x4d, 
                    0x65, 
                    0x64, 
                    0x76, 
                    0x65, 
                    0x64, 
                    0x65, 
                    0x76
                };

            PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(passPhrass, saltBytes);

            byte[] encryptedData = Encrypt(clearBytes, passwordDeriveBytes.GetBytes(32), passwordDeriveBytes.GetBytes(16));

            return Convert.ToBase64String(encryptedData);
        }
        private byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
        {
            MemoryStream memoryStream = new MemoryStream();

            Rijndael algorithm = Rijndael.Create();

            algorithm.Key = Key;
            algorithm.IV = IV;

            CryptoStream cryptoStream = new CryptoStream(memoryStream, algorithm.CreateDecryptor(), CryptoStreamMode.Write);

            cryptoStream.Write(cipherData, 0, cipherData.Length);
            cryptoStream.Close();

            byte[] decryptedData = memoryStream.ToArray();

            return decryptedData;
        }
        public string Decrypt(string password)
        {
            byte[] passwordBytes = Convert.FromBase64String(password);

            byte[] saltBytes = 
                            {
                                0x49, 
                                0x76,
                                0x61, 
                                0x6e, 
                                0x20, 
                                0x4d, 
                                0x65, 
                                0x64, 
                                0x76, 
                                0x65, 
                                0x64, 
                                0x65, 
                                0x76
                            };

            PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(passPhrass, saltBytes);

            byte[] decryptedData = Decrypt(passwordBytes, passwordDeriveBytes.GetBytes(32), passwordDeriveBytes.GetBytes(16));
           
            return Encoding.Unicode.GetString(decryptedData);
            
        }
}
Posted
Comments
Maarten Kools 19-Aug-13 7:45am
   
Please do not repost[^].

Decrypt(Encrypt("test")) works fine. Your problem is with the usage of these methods, not the methods itself.
Keith Barrow 19-Aug-13 10:28am
   
Can you post the code you call these methods from?
Member 9549287 19-Aug-13 13:14pm
   
you may want to check whitespaces at the end of the encrypted password
idenizeni 19-Aug-13 17:31pm
   
Are you sure you are using the same passPhrase value to decrypt that was used to encrypt the data?

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