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. Pyzo -- 好用的 Python 轻量级 IDE

    近期 yvivid 使用 Python 进行科学计算类应用(如matlab部分应用场景) 比较好的 发行版本为 Anaconda: A free distribution for the SciPy ...

  2. C++ 的多重继承

    不能够从对象访问基类的公开方法,真悲剧!只能在类里面提供公共函数! void Mentor::GetInfo(){ cout<<endl<<name<<endl&l ...

  3. Codeforces 526E Transmitting Levels

    http://codeforces.com/contest/526/problem/E 题意:给一个环,每个点有权值,每次给一个数B,求把这个环切割成若干部分,每个部分不超过B,至少要切成几块? #i ...

  4. POJ 1755 Triathlon

    http://poj.org/problem?id=1755 题意:铁人三项,每个人有自己在每一段的速度,求有没有一种3条路线长度都不为0的设计使得某个人能严格获胜? 我们枚举每个人获胜,得到不等式组 ...

  5. Codeforces 459E Pashmak and Graph

    http://www.codeforces.com/problemset/problem/459/E 题意: 给出n个点,m条边的有向图,每个边有边权,求一条最长的边权上升的路径的长度. 思路:用f存 ...

  6. DDUI For Delphi Seattle Directui界面组件

    http://www.delphigear.cn/0/11258/go.aspx http://bbs.csdn.net/topics/390285613

  7. WPF中使用文件浏览对话框的几种方式

    原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用 ...

  8. AS3给显示对象加边框

    给显示对象加边框,可以有以下三种方法1.根据相交路径的缠绕规则的奇偶规则法(使用奇偶缠绕规则时,任何相交路径都交替使用开放填充与闭合填充.如果使用同一填充绘制的两个正方形相交,则不会填充相交的区域.通 ...

  9. 好多邮箱的SMTP设置

    http://731771490.diandian.com/post/2011-04-20/19576550

  10. myeclipse实现Servlet实例(1) 通过继承servlet接口实现

    1.在myeclipse新建web project,配置Tomcat(在myeclipse的Window--preferences) 2.然后在src新建servlet文件( 此处放在com.tsin ...