[英]Implementing this Java password encryption algorithm in PHP
I'm trying to implement a password encryption algorithm used in Funambol mobile sync server in PHP but I'm having hard time as I come from a non-Java background. 我正在尝试在PHP中实现Funambol移动同步服务器中使用的密码加密算法,但是我很难从非Java背景来看。 The code itself seems simple: 代码本身看起来很简单:
encryptionKey = "Omnia Gallia in tres partes divida est";
byte[] newValue = new byte[24];
System.arraycopy(encryptionKey, 0, newValue, 0, 24);
encryptionKey = newValue;
KeySpec keySpec = new DESedeKeySpec(encryptionKey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESEde");
Cipher cipher = Cipher.getInstance("DESEde");
SecretKey key = keyFactory.generateSecret(keySpec);
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherBytes = cipher.doFinal(plainBytes);
I'm not necessarily looking for a complete solution, rather pointers on what I can use on PHP's side. 我不一定在寻找一个完整的解决方案,而是指向我可以在PHP方面使用的内容。 Can mcrypt handle this and to what extent? mcrypt可以处理这个以及在多大程度上处理? What else do I need? 我还需要什么? Is this even doable in PHP? 这在PHP中甚至可行吗?
To the curious: I'm building an interface to the Funambol server and I'd like to be able to add users from the interface itself using PHP. 好奇:我正在构建Funambol服务器的接口,我希望能够使用PHP从界面本身添加用户。
Finally got it solved, posting here in case someone ever needs to encrypt or decrypt passwords for Funambol using PHP: 最后得到解决,发布在这里以防万一有人需要使用PHP加密或解密Funambol的密码:
class Funambol_auth {
private static $key = "Omnia Gallia in tres par";
public static function encrypt($data) {
$size = mcrypt_get_block_size('des', 'ecb');
$data = self::pkcs5_pad($data, $size);
$mcrypt_module = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'ecb', '');
$mcrypt_iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($mcrypt_module), MCRYPT_RAND);
$key_size = mcrypt_enc_get_key_size($mcrypt_module);
mcrypt_generic_init($mcrypt_module,self::$key,$mcrypt_iv);
$encrypted = base64_encode(mcrypt_generic($mcrypt_module, $data));
mcrypt_module_close($mcrypt_module);
return $encrypted;
}
public static function decrypt($data) {
$mcrypt_module = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'ecb', '');
$mcrypt_iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($mcrypt_module), MCRYPT_RAND);
$decrypted = mcrypt_decrypt(MCRYPT_TRIPLEDES, self::$key, base64_decode($data), 'ecb', $mcrypt_iv);
mcrypt_module_close($mcrypt_module);
return self::pkcs5_unpad($decrypted);
}
private static function pkcs5_pad($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
private static function pkcs5_unpad($text) {
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
return substr($text, 0, -1 * $pad);
}
}
This may be what you want: 这可能是你想要的:
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
$nKeySize = mcrypt_enc_get_key_size($td);
$key = substr(sha1($encryptionKey), 0, $nKeySize);
mcrypt_generic_init($td, $key, $iv);
$nDataSize = strlen($plainBytes);
$sDataSize = substr(dechex(ceil(log($nDataSize,16))), -1) . dechex($nDataSize);
$cipherBytes = mcrypt_generic($td, $sDataSize . $plainBytes);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
Notice I encrypt $sDataSize
with the data because of the way 3DES encypts/decrypts blocks of data. 注意我用数据加密$sDataSize
是因为3DES加密/解密数据块的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.