java常用加密算法之pbkdf2

PBKDF2简介

常见的加密算法,如MD5,此类算法为单向的,无法通过逆向破解,但由于技术的不断进步,可以通过字典和暴力破解。后来人们通过加盐来增加密码的安全性,但彩虹表的出现让这种方式也变得不安全。以至于出现了现在的PBKDF2算法。
PBKDF2算法通过多次hash来对密码进行加密。原理是通过password和salt进行hash,然后将结果作为salt在与password进行hash,多次重复此过程,生成最终的密文。此过程可能达到上千次,逆向破解的难度太大,破解一个密码的时间可能需要几百年,所以PBKDF2算法是安全的。
在web系统中最基础的一个功能就是用户密码的验证,采用pbkdf2算法对用户的密码进行加密是比较常见的作法,本篇博客将简单的给出加密的Java实现。

package com.*.*.*.utiles;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.xml.bind.DatatypeConverter;

public class PBKDF2 {
	public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA1";  
	  
	//盐的长度  
    public static final int SALT_SIZE = 16;  
  
     //生成密文的长度 
    public static final int HASH_SIZE = 16;  
  
     // 迭代次数 
    public static final int PBKDF2_ITERATIONS = 1000;  
  
    /** 
     * 对输入的密码进行验证 
     *  password 密码明文
     *  dataPassWord 密码加密
     */  
    public static boolean verify(String password, String dataPassWord)  
            throws NoSuchAlgorithmException, InvalidKeySpecException {  
        // 用相同的盐值对用户输入的密码进行加密  
        String result = getPBKDF2(password, dataPassWord.substring(0,16));  
        // 把加密后的密文和原密文进行比较,相同则验证成功,否则失败  
        return result.equals(dataPassWord.substring(16,dataPassWord.length()));  
    }  
  
    /**
     * 根据password和salt生成密文
     * 
     */
    public static String getPBKDF2(String password, String salt) throws NoSuchAlgorithmException,  
            InvalidKeySpecException {  
    	//将16进制字符串形式的salt转换成byte数组
    	byte[] bytes = DatatypeConverter.parseHexBinary(salt);
        KeySpec spec = new PBEKeySpec(password.toCharArray(), bytes, PBKDF2_ITERATIONS, HASH_SIZE * 4);  
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM);  
        byte[] hash = secretKeyFactory.generateSecret(spec).getEncoded();
		//将byte数组转换为16进制的字符串
        return DatatypeConverter.printHexBinary(hash);  
    }  
    
  
    /** 
     * 生成随机盐值
     *  
     */  
    public static String getSalt() throws NoSuchAlgorithmException {  
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");  
        byte[] bytes = new byte[SALT_SIZE / 2];  
        random.nextBytes(bytes);  
        //将byte数组转换为16进制的字符串
        String salt = DatatypeConverter.printHexBinary(bytes);
        return salt;  
    } 
    
    public static void main(String[] args) {
		try {
			String salt= getSalt();
			System.out.println("salt:"+salt);//获取salt
			String miwen = getPBKDF2("111", salt); //明文密码加密
			System.out.println("miwen:"+miwen);
			System.out.println(salt+miwen);
			System.out.println(verify("111", salt+miwen));//解密
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
	}
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PBKDF2是一种密码学中的密钥派生函数,它可以将一个较弱的密码转换为一个强密钥,从而增加密码的安全性。在Java中,可以使用javax.crypto.SecretKeyFactory类来实现PBKDF2算法的加密和解密。 下面是一个简单的Java代码示例,演示如何使用PBKDF2算法进行加密和解密: ```java import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import java.security.spec.KeySpec; import java.util.Arrays; public class PBKDF2Example { public static void main(String[] args) throws Exception { String password = "myPassword123"; byte[] salt = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; int iterations = 10000; int keyLength = 256; // 加密 byte[] encrypted = encrypt(password, salt, iterations, keyLength); System.out.println("Encrypted: " + Arrays.toString(encrypted)); // 解密 boolean isMatch = decrypt(password, salt, iterations, keyLength, encrypted); System.out.println("Is match: " + isMatch); } public static byte[] encrypt(String password, byte[] salt, int iterations, int keyLength) throws Exception { KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, keyLength); SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256"); return factory.generateSecret(spec).getEncoded(); } public static boolean decrypt(String password, byte[] salt, int iterations, int keyLength, byte[] encrypted) throws Exception { byte[] decrypted = encrypt(password, salt, iterations, keyLength); return Arrays.equals(encrypted, decrypted); } } ``` 在上面的代码中,encrypt()方法使用PBKDF2算法对密码进行加密,decrypt()方法使用相同的算法对密码进行解密,并比较解密后的结果是否与加密前的密码相同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值