Click here to Skip to main content
15,887,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi i am trying to encrypt and decrypt the date ..
i am creating Guid for user id then i Encrypt it and send it to email in a link if user click it then i get the id and Decrypt it ....but having problem with Guid Decryption and tested it by giving different vales it works for "12345678" but not for "123456789" i think its some data error.


here is the code for Decryption and Encryption.


public class EDdata
    {
        internal static string Decrypt(string cipherText)
        {
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_Pwd, _Salt);
            byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
            return System.Text.Encoding.Unicode.GetString(decryptedData);
        }

        private static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = null;
            try
            {
                Rijndael alg = Rijndael.Create();
                alg.Key = Key;
                alg.IV = IV;
                cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(cipherData, 0, cipherData.Length);
                cs.FlushFinalBlock();
                return ms.ToArray();
            }
            catch
            {
                return null;
            }
            finally
            {
                cs.Close();
            }
        }

        public static string Encrypt(string clearText)
        {
            byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_Pwd, _Salt);
            byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
            return Convert.ToBase64String(encryptedData);
        }


        private static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
        {
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = null;
            try
            {
                Rijndael alg = Rijndael.Create();
                alg.Key = Key;
                alg.IV = IV;
                cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(clearData, 0, clearData.Length);
                cs.FlushFinalBlock();
                return ms.ToArray();
            }
            catch
            {
                return null;
            }
            finally
            {
                cs.Close();
            }
        }
Posted
Updated 21-May-11 10:53am
v3

where do all your values come from ? What is happening ? You know ASP.NET pages have no state, right ? any values you set, are only there for that one page instance.
 
Share this answer
 
Comments
vicky87 21-May-11 16:56pm    
you can get values using
Request.QueryString["id"];
3 suggestions - hope one of them works for you!


Suggestion 1.

See http://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptostream.aspx[^]

I think you might need to be reading from your CryptoStream, not writing to it (no real idea why this would make a difference, but that's the way all the examples I've seen do it and the way I've always done it). There may be other ways of doing this, but try adapting the sample from the link above.


Suggestion 2.

Is there a possible issue with PaddingMode and/or CipherMode? Suggest you look at the length of your cipher text (before converting to Base64) - how many bytes do you have? This is a hunch based on the fact that what you have works with 8 bytes (1 block?) but not 9. When it fails, do you know what plain text it is giving back to you? Junk? Or just too few / too many characters?

See for example http://connect.microsoft.com/VisualStudio/feedback/details/97915/padding-paddingmode-property-ignored-in-rijndaelmanaged-behavior-is-always-paddingmode-zeros-ciphermode-cbc[^]


Suggestion 3.

Have you tried encrypting / decrypting in one routine (without round tripping via your web page) to see if that works? Does the web page deliver back the same Base64 string as you supplied?
 
Share this answer
 
Comments
vicky87 22-May-11 15:54pm    
Suggestion 3.
yes it works on same page ...but when i send it to other this not works...
NuttingCDEF 22-May-11 17:01pm    
When you say 'not works' - do you mean you send one base64 string and get a different base64 string back? Or you get the same base64 string back as you sent, but can't recover the correct plain text when you decrypt it?

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