魔术师耿 2017-02-22 10:03 采纳率: 0%
浏览 1915
已结题

nodejs 通过盐值密码校验 的java实现方式,只有这么多豆了

图片说明
通过java来通过盐值和密码来计算校验密码,是不对的,求教错在哪里,哪位做过请指点下
nodejs的代码如下:

  //无密码的属于第三方登陆不能拿昵称登陆
                authenticate: function (plainText) {
                    return this.password&&this.salt&&this.encryptPassword(plainText, this.salt) === this.password;
                },
                encryptPassword: function (password, salt) {
                    if (!password || !salt) {
                        return '';
                    }
                    salt = new Buffer(salt, 'base64');
                    return crypto.pbkdf2Sync(password, salt, 10000, 64).toString('base64');
                },

我试验的java的代码如下

 package com.xxshow.util;

import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

public class PasswordEncryption {

    public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA1";
//  public static final String PBKDF2_ALGORITHM = "SHA1";
    /**
     * 盐的长度
     */
    public static final int SALT_BYTE_SIZE = 32 / 2;
//  public static final int SALT_BYTE_SIZE = 16 / 2;
//public static final int SALT_BYTE_SIZE = 16;
    /**
     * 生成密文的长度
     */
    public static final int HASH_BIT_SIZE = 128 * 4;
    /**
     * 迭代次数
     */
    public static final int PBKDF2_ITERATIONS = 1000;

    /**
     * 对输入的密码进行验证
     * 
     * @param attemptedPassword
     *            待验证的密码
     * @param encryptedPassword
     *            密文
     * @param salt
     *            盐值
     * @return 是否验证成功
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     */
    public static boolean authenticate(String attemptedPassword, String encryptedPassword, String salt)
            throws NoSuchAlgorithmException, InvalidKeySpecException {
        // 用相同的盐值对用户输入的密码进行加密
        String encryptedAttemptedPassword = getEncryptedPassword(attemptedPassword, salt);

        System.out.println("加密后:"+encryptedAttemptedPassword);
        System.out.println("base64位加密后:");
        System.out.println(Base64.encodeBase64String(fromHex(encryptedAttemptedPassword)));
        encryptedAttemptedPassword = Base64.encodeBase64String(fromHex(encryptedAttemptedPassword));

        // 把加密后的密文和原密文进行比较,相同则验证成功,否则失败
        return encryptedAttemptedPassword.equals(encryptedPassword);
    }

    /**
     * 生成密文
     * 
     * @param password
     *            明文密码
     * @param salt
     *            盐值
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     */
    public static String getEncryptedPassword(String password, String salt) throws NoSuchAlgorithmException,
            InvalidKeySpecException {
        System.out.println(new String(toHex(fromHex(salt))));
        KeySpec spec = new PBEKeySpec(password.toCharArray(), fromHex(salt), PBKDF2_ITERATIONS, HASH_BIT_SIZE);
        SecretKeyFactory f = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);

        return toHex(f.generateSecret(spec).getEncoded());
    }

    /**
     * 通过提供加密的强随机数生成器 生成盐
     * 
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String generateSalt() throws NoSuchAlgorithmException {
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        byte[] salt = new byte[SALT_BYTE_SIZE];
        random.nextBytes(salt);

        return toHex(salt);
    }

    /**
     * 十六进制字符串转二进制字符串
     * 
     * @param   hex         the hex string
     * @return              the hex string decoded into a byte array      
     */
    private static byte[] fromHex(String hex) {
        byte[] binary = new byte[hex.length() / 2];
        for (int i = 0; i < binary.length; i++) {
            binary[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
        }
        return binary;
    }

    /**
     * 二进制字符串转十六进制字符串
     * 
     * @param   array       the byte array to convert
     * @return              a length*2 character string encoding the byte array      
     */
    private static String toHex(byte[] array) {
        BigInteger bi = new BigInteger(1, array);
        String hex = bi.toString(16);
        int paddingLength = (array.length * 2) - hex.length();
        if (paddingLength > 0)
            return String.format("%0" + paddingLength + "d", 0) + hex;
        else
            return hex;
    }

    /*
                makeSalt: function () {
                    return crypto.randomBytes(16).toString('base64');
                },
                //无密码的属于第三方登陆不能拿昵称登陆
                authenticate: function (plainText) {
                    return this.password&&this.salt&&this.encryptPassword(plainText, this.salt) === this.password;
                },
                encryptPassword: function (password, salt) {
                    if (!password || !salt) {
                        return '';
                    }
                    salt = new Buffer(salt, 'base64');
                    return crypto.pbkdf2Sync(password, salt, 10000, 64).toString('base64');

    * */
    public static void main(String[] args) {
        String password = "a123";
        String salt = toHex(Base64.decodeBase64("XL9yrupj+X6Z70oFNu5Iog=="));
        System.out.println(salt);
        String ciphertext = "YuhDM3R8tOIsOejI5/1beoic7Ykh633i30Grb43f5mjO8lYI6XGrWF2fcwfOH/da0f3m6JNM3JZXe+9Wtn2+Fw==";
        System.out.println(ciphertext);
            try {
            boolean result = PasswordEncryption.authenticate(password, ciphertext, salt);
            if (result) {
                System.out.println("succeed");
            } else {
                System.out.println("failed");
            }
        } catch (NoSuchAlgorithmException e) {
            System.out.println("NoSuchAlgorithmException");
        } catch (InvalidKeySpecException e) {
            System.out.println("InvalidKeySpecException");
        }
    }
}
  • 写回答

2条回答 默认 最新

  • devmiao 2017-02-22 15:37
    关注

    没有豆可以去充值的。

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名