比较两个字符串时如何将一个值替换为另一个值?
我的用户在数据库中保存了一个密码字段,例如“0!ZWQ2”。 我必须将我的用户输入密码从“aA1234”解读为“0!ZWQ2”,并将其与数据库中的数据进行比较。
我比较的两个字符串是:
“abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890” 和 “9setybcpqwiuvxr108daj5'-`~!@#$%^&*()_+|}][{:.?/<>,;ZWQ2@#34KDA”
这样 a = 9 且 0 = A
如何我用 C# 做这个,有什么想法吗?
我只需要从用户输入中获取密码字段,并将其打乱为有趣的文本,以便将其与数据库中的数据进行比较。
任何帮助将不胜感激。
问候 艾蒂安
更新:(2009 年 2 月 10 日) 感谢大家的回复。 请注意,我确实意识到有更好的方法来处理这个问题。 但请注意,我正在创建一个 ASP.NET 应用程序,该应用程序将位于 SharePoint 中,连接到 Cobol 平面文件数据,而不是正确的数据库。 使用 Transoft 将我的 ASP.NET(ODBC 连接器)连接到 Cobol 平面文件。 所以我必须坚持使用这段代码,并且这不会在我的私人网站上使用。 我也无法控制何时在 Cobol 中创建密码。
My user have a password field such as “0!ZWQ2” saved in the database.
I must unscramble my User input password from “aA1234” to “0!ZWQ2” and compare it to data in a database.
The 2 strings that I compare is:
“abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ”
With
“9setybcpqwiuvxr108daj5'-`~!@#$%^&*()_+|}][{:.?/<>,;ZWQ2@#34KDA”
This way a = 9 and 0 = A
How would I do this in C#, any ideas?
I just need to take the password field from the user input and scramble it to funny text to compare it to the data in the Database.
Any help will be appreciated.
Regards
Etienne
Updates: (10 Feb 2009)
Thanks everyone for the replies. Please note that i do realize that there are much better ways of handeling this. But please note that I am creating an ASP.NET application thats goign to be sitting inside SharePoint connecting to Cobol flat file data and not a proper database. Using Transoft to connect my ASP.NET (ODBC connector) to the Cobol flat files. So i have to stick with this code, and this will not be used on my Private site. I also have no control on when the Password is created in Cobol.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下是要转换的快速、快速且较少的代码示例。
C# 代码:
VB 代码:
Following is Quick, Fast, and lesser code example to convert.
Code in C#:
Code in VB:
有什么特殊原因不使用标准哈希+盐来存储密码,而不是凯撒密码?
应该解决它的一种方法(未经测试的代码):
Any special reason not to use a standard hash + salt for storing the passwords, instead of a Caesars cipher?
One way that should solve it (untested code):
您不应以可逆的方式将密码保存在数据库中。 数据库管理员或入侵您系统的黑客不应该能够获取用户存储的密码。
You should not save the passwords in your database in a way that can be reversed. The database administrator or a hacker intruding your system should not be able to get the user's stored passwords.
首先,正如其他用户所说,如果您正在设计这个系统和密码存储方法,那么这种方法几乎毫无价值,应该使用许多更安全的方法。
然而,您要求这样做进行比较的事实让我认为您无法控制数据库中已有的内容,因此正在处理遗留的内容。
下面的函数会将您的用户密码作为字符串,并以字符串形式返回数据库密码。 需要注意的一件事是,您提供的两个字符串的长度不相等,因此字符 9 和 0 会崩溃。我已将其他字母 B 和 C 添加到 dbChars 字符串中。 另外,我“假设”输入的任何在映射字符中找不到的字符都只是按输入时附加。 您可能要求忽略这些字符。
Firstly, as other users have stated, if you are designing this system and password storage method then this method is almost worthless and there are many more secure methods that should be used instead.
However, the fact that you're asking for this to do comparison makes me think you do not have control over what is already in the database and therefore are working with something legacy.
The function below will take your users password as a string and return the database password as a string. One thing to note, the two strings you provided are not of equal length and therefore would crash for characters 9 and 0. I have added to additional letters B and C to the dbChars string. Also, I have "assumed" that any characters entered that cannot be found in the mapping characters are just appended as entered. You may require that these characters are ignored.
我会编写一个包含 Encode 或 Translate 方法的实用程序类,该方法接受 inputString 并(检查任何错误/恶意输入)使用您的密码方法对其进行变形。
I'd write a Utility-class that contains an Encode or Translate method which takes the inputString and (checks for any bad/malicious input) transfigures it using your cipher-method.