在asp.net&中解密Guid时遇到问题C# [英] having problem while Decrypt Guid in asp.net & c#

查看:90
本文介绍了在asp.net&中解密Guid时遇到问题C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我正在尝试加密和解密日期..
我正在为用户ID创建Guid,然后我对其进行加密并将其发送给链接中的电子邮件(如果用户单击它),然后我获得了ID并对其进行解密.....但是Guid Decryption存在问题,并通过提供不同的价位对其进行了测试对于"12345678"而不是"123456789",我认为它有些数据错误.


这是解密和加密的代码.


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();
            }
        }

解决方案

所有值从何而来?怎么了 ?您知道ASP.NET页没有状态,对吗?您设置的任何值仅适用于该页面实例.


3条建议-希望其中一个对您有用!


建议1.

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

我认为您可能需要从CryptoStream中读取内容,而不是对其进行写入(没有真正的主意,这为什么会有所作为,但这就是我所看到的所有示例都这样做的方式以及我的方式" ve总是做到这一点).可能还有其他方法可以执行此操作,但是请尝试通过上面的链接修改示例.


建议2.

PaddingMode和/或CipherMode是否存在问题?建议您查看密文的长度(转换为Base64之前)-您有多少个字节?这是基于以下事实的预感:您拥有的内容可以使用8个字节(1个块?),而不是9个字节.当失败时,您知道它给您的是纯文本吗?垃圾?还是字符太少/太多?

例如,请参见 ^ ]


建议3.

您是否尝试过在一个例程中进行加密/解密(不通过您的网页往返)以查看是否有效?网页是否返回与您提供的相同的Base64字符串?


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();
            }
        }

解决方案

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.


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?


这篇关于在asp.net&中解密Guid时遇到问题C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆