Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Encrypting and decrypting a string in C#

Posted on 2009-04-08
3
Medium Priority
?
2,804 Views
Last Modified: 2012-06-27
I am trying to encrypt data stored and sent back from a windows mobile device using the encrypt function below, i couldnt access PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt); from using the System.Security.Cryptography so i included the OpenNETCF.Security.Cryptography; instead.

However i am trying to decrypt the string back to plain text in a .net web application and i keep getting the error Padding is invalid and cannot be removed.

If i use the decrypt function on the windows mobile app(compat framework 2.) it does decrypt it back though, i guessing that it has to do with using the OpenNETCF library instead ?

Does any one know how to overcome this issue or have an alternative way to achive what im trying to do ?
public static string Encrypt(string clearText, string Password)
        {
 
            RijndaelManaged RijndaelCipher = new RijndaelManaged();
            RijndaelCipher.Padding =  PaddingMode.PKCS7 ;
            byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(clearText);
            byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
            
            PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
            //Creates a symmetric encryptor object.
            ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
            MemoryStream memoryStream = new MemoryStream();
            //Defines a stream that links data streams to cryptographic transformations
            CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(PlainText, 0, PlainText.Length);
            //Writes the final state and clears the buffer
            cryptoStream.FlushFinalBlock();
            byte[] CipherBytes = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();
            string EncryptedData = Convert.ToBase64String(CipherBytes);
            return EncryptedData;
 
 
        }
 
        public static string Decrypt(string TextToBeDecrypted,string Password)
        {
 
            RijndaelManaged RijndaelCipher = new RijndaelManaged();
            RijndaelCipher.Padding = PaddingMode.PKCS7 ;
            byte[] EncryptedData = Convert.FromBase64String(TextToBeDecrypted);
            byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
            //Making of the key for decryption
            PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
            //Creates a symmetric Rijndael decryptor object.
            ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
            MemoryStream memoryStream = new MemoryStream(EncryptedData);
            //Defines the cryptographics stream for decryption.THe stream contains decrpted data
            CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
            byte[] PlainText = new byte[EncryptedData.Length];
            int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
            memoryStream.Close();
            cryptoStream.Close();
            //Converting to string
            string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
            return DecryptedData;
        }

Open in new window

0
Comment
Question by:MCLR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Expert Comment

by:topdog770
ID: 24108445
This looks like it should help:  http://dalelane.co.uk/blog/?p=234
 
 
0
 
LVL 1

Author Comment

by:MCLR
ID: 24108623
thanks, but i have similar code already, in that the only difference between the function on the mobile device and the .net app  is that I use:
OpenNETCF.Security.Cryptography.PasswordDeriveBytes instead of
System.Security.Cryptography.PasswordDeriveBytes.

but yet i am getting the error error Padding is invalid and cannot be removed.
0
 
LVL 1

Accepted Solution

by:
MCLR earned 0 total points
ID: 24139549
I couldnt get that method to work after so in teh end i used the following
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Encoding.UTF8.GetBytes(text);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Provide an easy one stop to quickly get the relevant information on common asked question on Ransomware in Expert Exchange.
Businesses who process credit card payments have to adhere to PCI Compliance standards. Here’s why that’s important.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

715 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