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. Python新手学习基础之数据类型——变量

    关于Python的变量是这样描述的: 变量是存储在内存里的一个值,通过变量名,我们可以访问到该变量的值. 上面这几行代码中,price,count和sum都是变量,Python是动态类型语言,变量是不 ...

  2. [Python 3.x 官方文档翻译]Whetting Your Appetite 欢迎您的使用

    If you do much work on computers, eventually you find that there’s some task you’d like to automate. ...

  3. iOS学习之根据文本内容动态计算文本框高度的步骤

    在视图加载的过程中,是先计算出frame,再根据frame加载视图的,所以在设计计算高度的方法的时候,设计成加号方法; //首先给外界提供计算cell高度的方法 + (CGFloat)heightFo ...

  4. 设置iOS项目BuildVersion自动增加-备用

    一.概念阐述:Build与Version的区别 在iOS中有两种“版本号”,也就是所谓的version号与build号,如下图所示: 我们用最简洁的语言来区分这两个版本号的区别以及用途如下: Vers ...

  5. Eclipse远程调试weblogic

    http://www.cnblogs.com/dyllove98/archive/2013/08/06/3241140.html http://blog.csdn.net/afgasdg/articl ...

  6. jQuery的animate方法在IE8下出现小问题

    今天修改网站的bug,把网页显示的几张图片给做成左右滑动的动画效果: 由于本身有一个demo可供参考,然后在此基础上进行修改,所以很快就搞定了,然后在chrome,firefox,IE9下分别进行测试 ...

  7. Struct2(五)处理表单

    简介: 1.表单的提交 表单和对应的Java模型的类 在此次的例子中,我们将会模仿一个用户提交表单的动作,具体提交表单做什么,不关心,我们需要知道 first last Name,Email addr ...

  8. [转]Jquery中AJAX错误信息调试参考

    下面是Jquery中AJAX参数详细列表: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET") 请求 ...

  9. sdut2623--The number of steps(概率dp第一弹,求期望)

    The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 Mary stands in a st ...

  10. C#委托的详细使用

    代码如下: public delegate void GreetingDelegate(string name);//定义委托,它定义了可以代表方法的类型 class Program { public ...