SHA与PBKDF2加密的实现与应用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jingzi123456789/article/details/84939980

哈希算法并不是一种加密算法。不能对用户信息的保护。因为哈希算法是单向的,可以将任何大小的数据转化为定长的“指纹”,而且无法被反向计算。

但Hash算法常用于对口令的保存上。另外,即使数据源只改动了一丁点,哈希的结果也会完全不同。这样的特性使得它非常适合用于保存密码,因为我们需要加密后的密码无法被解密,同时也能保证正确校验每个用户的密码。

例如,用户登录网站需要通过用户名和密码来进行验证。如果网站后台直接保存用户的口令明文,一旦数据库发生泄漏,后果不堪设想。

然而,由于有时候用户设置的口令的强度不够,只是一些常见的字符串,如password、123456等。有人专门搜集这些常见的口令,计算对应的Hash值,制成字典。这样通过Hash值可以快速的查到原始口令。这一类型以空间换时间的攻击方法包括字典攻击和彩虹攻击(只保存一条Hash链的首尾值,相对字典攻击可以节省存储空间)等。

为了防范这一类攻击,一般采用加盐值(salt)的方法。这样保存的不是口令明文的Hash值,而是口令再加上一段随机字符串(即“盐”)之后的Hash值。盐是一个添加到用户的密码哈希过程中的一段随机序列。这个机制能够防止通过预先计算结果的彩虹表破解。

每个用户都有自己的盐,这样的结果就是即使用户的密码相同,通过加盐后哈希值也将不同。

为了校验密码是否正确,我们需要储存盐值。通常和密码哈希值一起存放在账户数据库中,或者直接存为哈希字符串的一部分。

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 {

    /**
     * MD5和SHA1已被破解,SHA-1已被证明不具备“强抗碰撞性”
     * 一般推荐至少使用SHA2-256或更安全的算法。
     * PBKDF2WithHmacSHA256
     * PBKDF2WithHmacSHA384
     * PBKDF2WithHmacSHA512
     */
    public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA256";

    /**
     * 盐的长度
     */
    public static final int SALT_BYTE_SIZE = 32 / 2;

    /**
     * 生成密文的长度
     */
    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);
        // 把加密后的密文和原密文进行比较,相同则验证成功,否则失败
        return encryptedAttemptedPassword.equals(encryptedPassword);
    }

    /**
     * 生成密文
     *
     * @param password
     *            明文密码
     * @param salt
     *            盐值
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     */
    public static String getEncryptedPassword(String password, String salt) throws NoSuchAlgorithmException,
            InvalidKeySpecException {

        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;
    }

    public static void main(String[] args) {
        String password = "test";
        String salt;
        String ciphertext;
        try {
            salt = PasswordEncryption.generateSalt();
            ciphertext = PasswordEncryption.getEncryptedPassword(password, salt);
            boolean result = PasswordEncryption.authenticate(password, ciphertext, salt);

            System.out.println(password + "  " + password.length());
            System.out.println(salt + "  " + salt.length());
            System.out.println(ciphertext + "  " + ciphertext.length());
            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");
        }
    }

}

--------------------------代码实现来载地址--------------------------------------
作者:阿牛哞了一声 
原文:https://blog.csdn.net/u014375869/article/details/46773995 
---------------------------------------------------------------------------------------- 

猜你喜欢

转载自blog.csdn.net/jingzi123456789/article/details/84939980