java中的php openssl_pbkdf2等效项

java中的php openssl_pbkdf2等效项,java,php,encryption,Java,Php,Encryption,我有一些php代码可以将消息加密到webservice。现在,我想创建等效的java代码,但却陷入了困境。请帮忙 --PHP代码看起来像 $length = 60; $salt = "MySalt"; $interation = 3000; $bytes = openssl_pbkdf2("mypasspharse", $salt, $length, $interation, "sha1"); $value1 = substr($bytes, 0, 10); $value2 = substr

我有一些php代码可以将消息加密到webservice。现在,我想创建等效的java代码,但却陷入了困境。请帮忙

--PHP代码看起来像

$length = 60;
$salt =  "MySalt";
$interation = 3000;
$bytes = openssl_pbkdf2("mypasspharse", $salt, $length, $interation, "sha1");

$value1 = substr($bytes, 0, 10);
$value2 = substr($bytes, 10, 20);
$value3 = substr($bytes, 30, 30);

return array('value1' => $value1, 'value2' => $value2, 'value3' => $value3);
--

我正试图使用SecretKeyFactory在java中做同样的事情。我的java代码如下所示:

SecretKeyFactory factory = SecretKeyFactory
                .getInstance("PBKDF2WithHmacSHA1");
        PBEKeySpec spec = new PBEKeySpec(passphrase.toCharArray(),
                salt.getBytes("UTF-8"), interation, length);
现在我不知道如何将等价的$value1、$value2、$value3作为php代码。另外,我不确定我是否用正确的方法编写了与上述php代码等效的java代码。任何想法都非常感谢


谢谢,

2下面的示例产生相同的输出:

PHP:

$length = 60;
$salt =  "MySalt";
$interation = 10;
$bytes = openssl_pbkdf2("mypasspharse", $salt, $length, $interation, "sha1");

for ($i = 0; $i < 60; $i++)
{
    echo dechex(ord($bytes[$i]));
    echo '<br>';
}
int length = 60 * 8;
String passphrase = "mypasspharse";
String salt = "MySalt";
int iteration = 10;

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
PBEKeySpec spec = new PBEKeySpec(passphrase.toCharArray(), salt.getBytes("UTF-8"), iteration, length);

byte[] bytes = factory.generateSecret(spec).getEncoded();
for (int i = 0; i < 60; i++) {
    System.out.println(Integer.toHexString(bytes[i] & 0xFF));
}
$length=60;
$salt=“MySalt”;
$interation=10;
$bytes=openssl_pbkdf2(“mypasspharse,$salt,$length,$interaction,$sha1”);
对于($i=0;$i<60;$i++)
{
echo dechex(ord($bytes[$i]);
回声“
”; }
Java:

$length = 60;
$salt =  "MySalt";
$interation = 10;
$bytes = openssl_pbkdf2("mypasspharse", $salt, $length, $interation, "sha1");

for ($i = 0; $i < 60; $i++)
{
    echo dechex(ord($bytes[$i]));
    echo '<br>';
}
int length = 60 * 8;
String passphrase = "mypasspharse";
String salt = "MySalt";
int iteration = 10;

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
PBEKeySpec spec = new PBEKeySpec(passphrase.toCharArray(), salt.getBytes("UTF-8"), iteration, length);

byte[] bytes = factory.generateSecret(spec).getEncoded();
for (int i = 0; i < 60; i++) {
    System.out.println(Integer.toHexString(bytes[i] & 0xFF));
}
int-length=60*8;
字符串passphrase=“mypasspharse”;
字符串salt=“MySalt”;
int迭代=10;
SecretKeyFactory factory=SecretKeyFactory.getInstance(“PBKDF2WithHmacSHA1”);
PBEKeySpec spec=新的PBEKeySpec(passphrase.toCharArray(),salt.getBytes(“UTF-8”),迭代,长度);
byte[]bytes=factory.generateScret(spec.getEncoded();
对于(int i=0;i<60;i++){
System.out.println(Integer.toHexString(字节[i]&0xFF));
}

非常感谢您的回复。您能解释一下为什么长度需要*8吗?@user3567722长度以位为单位