How to check whether the string is in encoded form or not
I have a method inside that i am encode the input string,and then i check the value with the my data base value(stored in the encoded form),
public void checkString(St开发者_开发问答ring strPass){
String s = MD5.crypt(strPass);
code to check the string s with the data base value..
}
I need a way that check the value of the 'strPass' before it passes to the method that encode the string.Data for the strPass may be in the form of 1. admin 2. L4989C
Please help me out..
You will have to:
- Hash the string you wanted to hash with MD5.
- Represent the Hash in hexadecimal String
- Do
str1.equals(str2)
to see if the 2 hashes are equal.
Here's the answer to the question that you asked.
public void checkString(String strPass) {
if (strPass.equals("admin") || strPass.equals("L4989C")) {
// do something else
} else {
String s = MD5.crypt(strPass);
// code to check the string s with the data base value.
}
However, I think that you may have a problem with your MD5.crypt()
method. If that method does the following:
- Use
String.getBytes(...)
to convert the string to abyte[]
. - Calculate the MD5 checksum of the bytes, giving you another
byte[]
. - Use
new String(byte[], ...)
to convert the encrypted bytes into a String.
The problem is that the last step is most likely lossy ... and incorrect. Most character encodings have bytes or byte sequences that don't map onto valid characters. If the String
constructor encounters one of these in the input byte array, then it will either discard it or map it to some character that denotes an unmappable character (e.g. '?'). The net result is that the checksum is not stored correctly. In order to deal with this, you must either store the byte[]
form of checksum in the database as a blob, or encode it in hexadecimal or base64 or whatever.
It would help in understanding your question if you provided the source code for your MD5.crypt method ... or told us which library it comes from.
精彩评论