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. 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. Can mcrypt handle this and to what extent? What else do I need? Is this even doable in 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.
What is the best way to encrypt password in PHP? PHP encompasses a hash algorithm to encrypt the password. For the most part it is used in functions for password encrypting are crypt(), password_hash() and md5().
Password-Based Encryption using Salt and Base64: The password-based encryption technique uses plain text passwords and salt values to generate a hash value. And the hash value is then encoded as a Base64 string. Salt value contains random data generated using an instance of Random class from java. util package.
In PHP, Encryption and Decryption of a string is possible using one of the Cryptography Extensions called OpenSSL function for encrypt and decrypt. openssl_encrypt() Function: The openssl_encrypt() function is used to encrypt the data. Parameters: $data: It holds the string or data which need to be encrypted.
Encrypted data can be decrypted via openssl_private_decrypt(). This function can be used e.g. to encrypt message which can be then read only by owner of the private key. It can be also used to store secure data in database.
Finally got it solved, posting here in case someone ever needs to encrypt or decrypt passwords for Funambol using PHP:
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With