PHP代码:

/**
* PBKDF2 加密函数
* 参考标准
* @link https://www.ietf.org/rfc/rfc2898.txt
*
* php官方函数将在php5.5发布
* @see http://php.net/manual/en/function.hash-pbkdf2.php
* example: pbkdf2("sha256", 'password', 'salt', 1, 20);
* result:120fb6cffcf8b32c43e7 (与php5.5内置的pbkdf2函数输出一至)
*
* @param string $algo The hash algorithm to use. Recommended: SHA256
* @param string $password The password to use for the derivation.
* @param string $salt The salt to use for the derivation.
* @param int $iterations The number of internal iterations to perform for the derivation.
* @param int $length The length of the derived key to output. If 0, the length of the supplied algorithm is used.
* @param boolean $raw_output When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits.
* @return string
*/
public static function pbkdf2($algo, $password, $salt, $iterations, $length= 0, $raw_output = false)
{
$algo = strtolower($algo);
if(!in_array($algo, hash_algos(), true))
throw new Exception('PBKDF2 ERROR: Invalid hash algorithm.');
if($iterations <= 0)
throw new Exception('PBKDF2 ERROR: Invalid parameters.');
// hash方式默认长度,对一个空值进行hash计算,得出
// length in octets of pseudorandom function output, a positive integer
$hlen = strlen(hash($algo, null, true));
if ($length <=0) $length = $hlen;
if ($length > (pow(2,32)-1)*$hlen)
throw new Exception('PBKDF2 ERROR: derived key too long.'); // 如果生成的加密值长度低于取值长度,就将加密再执行N遍以补足数据长度
// N=取值长度除以加密方式对应的值长度加一法取整
$block_count = ceil($length / $hlen);
$output = "";
for($i = 1; $i <= $block_count; $i++) {
// $i encoded as 4 bytes, big endian.
$last = $salt . pack("N", $i);
// first iteration
$last = $xorsum = hash_hmac($algo, $last, $password, true);
// perform the other $count - 1 iterations
for ($j = 1; $j < $iterations; $j++) {
$xorsum ^= ($last = hash_hmac($algo, $last, $password, true));
}
$output .= $xorsum;
}
if($raw_output)
return substr($output, 0, $length);
else
return substr(bin2hex($output), 0, $length);
}

JAVA 代码:

  /*  
    hashPassword("PBKDF2WithHmacSHA1","123456","abcdefg",1024,128);
  */
  static public byte[] hashPassword(String algorithm, String password, String salt, int iterations, int len) throws Exception{
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt.getBytes(), iterations, len);
SecretKeyFactory kFactory=SecretKeyFactory.getInstance(algorithm);
SecretKey secretKey = kFactory.generateSecret(pbeKeySpec);
byte[] buff = secretKey.getEncoded();
return buff;
}

PHP & JAVA 实现 PBKDF2 加密算法的更多相关文章

  1. Java的各种加密算法

    Java的各种加密算法 JAVA中为我们提供了丰富的加密技术,可以基本的分为单向加密和非对称加密 1.单向加密算法 单向加密算法主要用来验证数据传输的过程中,是否被篡改过. BASE64 严格地说,属 ...

  2. Java执行js加密算法

    Java执行js加密算法 今日需求:在后端执行一段加密算法,算法是js写的 明白需求以后疯狂百度.最后发现JDK提供了各种脚本的支持(怪笔者学艺不精,第一次见识到这个库,留下不学无术的泪水),正题开始 ...

  3. JAVA使用DES加密算法加密解密

    程序中使用了.properties文件作为参数配置文档,好处是灵活配置各项参数 一旦对数据库的一些参数进行了配置,势必涉及数据库的IP,端口,用户名和密码 properties文件全是unicode编 ...

  4. Java中间MD5加密算法完整版

    携带Java软件开发过程.,因此Java中提供了自带的MessageDigest实现对文本的加密算法,以下是一个对文本进行加密的MD5加密工具类代码演示样例: package net.yuerwan. ...

  5. Java使用RSA加密算法对内容进行加密

    什么是RSA加密算法 RSA是一种典型的非对称性加密算法,具体介绍可参考阮一峰的日志 RSA算法原理 下面是使用RSA算法对传输内容进行加密的一个简要Java案例,主要用到了三个类,大体实现如下: 对 ...

  6. JAVA实现MD5加密算法(使用MessageDigest)

    http://blog.csdn.net/ymc0329/article/details/6738711 *********************************************** ...

  7. JAVA的非对称加密算法RSA——加密和解密

    原文转载至:https://www.cnblogs.com/OnlyCT/p/6586856.html 第一部分:RSA算法原理与加密解密 一.RSA加密过程简述 A和B进行加密通信时,B首先要生成一 ...

  8. Java学习---MD5加密算法

    前言 在我们日常的程序开发中,或多或少会遇到一些加密/解密的场景,比如在一些接口调用的过程中,我们(Client)不仅仅需要传递给接口服务(Server)必要的业务参数,还得提供Signature(数 ...

  9. Java工具类-加密算法

    import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.securit ...

随机推荐

  1. C语言格式化输入输出函数

    一:格式输出函数printf() 1.调用形式一般为:printf("格式化控制字符串",输出表列): 2.格式化控制字符串用于指定输出格式,它有三种形式: 1.格式说明符:规定了 ...

  2. 安卓 eclipse项目创建

    一. 创建项目工程 1.  点击 file -> new -> Android Application project -> 2.  创建工程项目名字   自己命名 (注: 不要出现 ...

  3. Pick two points at random from the interior of a unit square, what is the expected distance between them?

    My solution is as folllowing. This integration is hard to solve. I googled it, and found the result ...

  4. Flume配置

    http://my.oschina.net/leejun2005/blog/288136#OSC_h1_1 http://blog.cloudera.com/blog/2012/09/analyzin ...

  5. 如何在KEIL中编写模块化的C程序

    在KEIL中的模块化程序写法在使用KEIL的时候,我们习惯上在一个.c的文件中把自己要写的东西按照自己思路的顺序进行顺序书写.这样是很普遍的写法,当程序比较短的时候比如几十行或者一百多行,是没有什么问 ...

  6. 黑马程序员_Java基本数据类型对象包装类

    基本数据类型对象包装类 byte Byte short Short int Integer long Long boolean Boolean float Float double Double ch ...

  7. dictionary (key-value) (map容器)

    #dictionary可以保存不同类型的值 menu = {'fish0':14.50} list = [] menu['fish1'] = 10.1 # Adding new key-value p ...

  8. 使用Python,字标注及最大熵法进行中文分词

    使用Python,字标注及最大熵法进行中文分词 在前面的博文中使用python实现了基于词典及匹配的中文分词,这里介绍另外一种方法, 这种方法基于字标注法,并且基于最大熵法,使用机器学习方法进行训练, ...

  9. hdu 4869 Turn the pokers(组合数+费马小定理)

    Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. S ...

  10. java.lang.NoClassDefFoundError 异常

    在项目实施过程中,当访问某一个功能时,出现异常为  java.lang.NoClassDefFoundError  com/xxx/yyy/Zzzz > ,检查发现这个类实际已经存在于应用服务器 ...