开发者

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:

  1. Hash the string you wanted to hash with MD5.
  2. Represent the Hash in hexadecimal String
  3. 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:

  1. Use String.getBytes(...) to convert the string to a byte[].
  2. Calculate the MD5 checksum of the bytes, giving you another byte[].
  3. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜