簡體   English   中英

哈希密碼並與MD5進行比較

[英]Hashing password and comparing with MD5

我有以下要求。

1. save a user password converted to hash(digested)
2. when comparing with data base, add random bytes with the password given from user 
3. now send the random bytes added password  to DAO class
4. separate the random byte from password 
5. compare with the stored hashed(digested) password

我嘗試了類似的東西,但它給出了數組超出綁定的異常。

package poc;

import com.sun.xml.internal.ws.message.ByteArrayAttachment;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Arrays;


public class HashedPassword {
    public static final String CRYPTOGRAPHY_ALGORITHM = "MD5";
    public static final String CHAR_SET = "UTF8";
    public static void main(String[] arg){
        System.out.println(createPassword("r14@17*$"));
    }
    public static byte[] createPassword(String password){
        byte[] salt = new byte[12];
        byte[] digestedPassword =null;
        byte[] digestedPasswordPwd =null;
        try {
                SecureRandom random = new SecureRandom();
                random.nextBytes(salt);
                MessageDigest mdPassword = MessageDigest.getInstance(CRYPTOGRAPHY_ALGORITHM);
                MessageDigest mdPasswordPawd = MessageDigest.getInstance(CRYPTOGRAPHY_ALGORITHM);

                mdPassword.update(salt);
                mdPassword.update(password.getBytes(CHAR_SET));

                mdPasswordPawd.update(password.getBytes(CHAR_SET));
                digestedPassword = mdPassword.digest();
                digestedPasswordPwd = mdPasswordPawd.digest();
                byte[] resultBytes= new byte[1000];

                System.arraycopy(digestedPassword, 11, resultBytes,0,digestedPassword.length);

                if(Arrays.equals(resultBytes, digestedPasswordPwd)){
                    System.out.println("match");
                }else{
                    System.out.println("no-match");
                }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("digestedPassword : "+digestedPassword);
        System.out.println("digestedPasswordPwd : "+digestedPasswordPwd);
        return digestedPassword;
    }

}

堆棧跟蹤 :

java.lang.ArrayIndexOutOfBoundsException
digestedPassword : [B@9980d5
digestedPasswordPwd : [B@1d95492
[B@9980d5
    at java.lang.System.arraycopy(Native Method)
    at poc.HashedPassword.createPassword(HashedPassword.java:43)
    at poc.HashedPassword.main(HashedPassword.java:23)

所以請幫助我如何去做

親切的問候

這條線有問題:

System.arraycopy(digestedPassword, 11, resultBytes,0,digestedPassword.length); 

它嘗試從位置11開始從digestedPassword復制digestedPassword.length字節。因此它嘗試復制不存在的11個字節。

試試這個:

System.arraycopy(digestedPassword, 11, resultBytes,0,digestedPassword.length-11); 

System.arraycopyAPI文檔中復制:

否則,如果滿足以下任何條件,則拋出IndexOutOfBoundsException並且不修改目標:

srcPos參數為負數。
destPos參數是否定的。
長度參數是否定的。
srcPos + length大於src.length,即源數組的長度。
destPos + length大於dest.length,即目標數組的長度。

首先,我認為從您的代碼中,您遺漏了與從密碼中刪除/分離隨機字節相關的位。因此它可能永遠不會相等。

關於你建議的ArrayIndexOutOfBoundsException,請使用

System.arraycopy(digestedPassword, 0, resultBytes,0,digestedPassword.length);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM