我需要你的帮助。我被要求通过PHP解密字符串,从这个java类开始:
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import com.sun.mail.util.BASE64DecoderStream;
public class Decryptor {
private Cipher dcipher;
private static final int iterationCount = 10;
private static byte[] salt = {
(byte) 0xB2, (byte) 0x12, (byte) 0xD5, (byte) 0xB2,
(byte) 0x44, (byte) 0x21, (byte) 0xC3, (byte) 0xC3 };
Decryptor(String passPhrase) {
try {
KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
dcipher = Cipher.getInstance(key.getAlgorithm());
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}
public String decrypt(String str) {
try {
byte[] dec = BASE64DecoderStream.decode(str.getBytes());
byte[] utf8 = dcipher.doFinal(dec);
return new String(utf8, "UTF8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
我不懂Java语言所以我需要一些帮助来理解这种加密。
我尝试使用此代码: https://github.com/KevinBusse/PBEWithMD5AndDES
但是我不能将盐“翻译”为PHP字符串,它似乎不起作用。
谢谢!
答案 0 :(得分:0)
尝试这样的事情,它对我有用。
拿盐:
$bytes = array ( 0xB9, 0x2B, 0xC8, 0x32, 0x56, 0x35 , 0xE3, 0x03 );
在php中将其转换为String:
$salt = call_user_func_array(
'sprintf',
array_merge((array) str_repeat('%c', count($bytes)), $bytes)
);
转到此回购: https://github.com/KevinBusse/PBEWithMD5AndDES
转到类 PkcsKeyGenerator 并对此代码 $ salt = pack('H *',$ salt); 进行评论。
function __construct($keystring, $salt, $iterationsMd5, $segments)
{
// $salt = pack('H*', $salt);
$keyMaterial = '';
$data = $keystring . $salt;
$result = '';
for ($j = 0; $j < $segments; $j++)
{
if ($j == 0)
{
$result = $data;
}
else
{
$result .= $data;
}
for ($i = 0; $i < $iterationsMd5; $i++)
{
$result = md5($result, true);
}
$keyMaterial .= $result;
}
$this->_key = substr($keyMaterial, 0, 8);
$this->_iv = substr($keyMaterial, 8, 8);
}
现在试试。