A
Archana Thota
I want to translate exactly the below php code into ruby for encryption.
echo up_getEncrypted('mypassword', 1);
function up_getEncrypted($password, $user_id)
{
srand();
$td = mcrypt_module_open('rijndael-128', '', 'cbc', '');
$keylen = mcrypt_enc_get_key_size($td);
$key = substr($password, 0, $keylen);
$key = str_pad($key, $keylen);
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
if (mcrypt_generic_init($td, $key, $iv) != -1)
{
$pad = 16 - strlen($user_id);
$mypadded = $user_id . str_repeat(chr($pad), $pad);
$c_t = mcrypt_generic($td, pack('H*',
md5($user_id)).$user_id);
mcrypt_generic_deinit($td);
$result = base64_encode($iv.$c_t);
return $result;
}
else
{
return "";
}
}
result = up_getEncrypted('mypassword', 1)
puts result
def up_getEncrypted(password, user_id)
c = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
c.encrypt
c.key = key =
Digest::SHA1.hexdigest(password).unpack('a2'*32).map{|x|
x.hex}.pack('c'*32)
c.iv = c.random_iv
e = c.update(user_id.to_s)
e << c.final
return e
end
Output of php encryption code and output of rails encryption code are
not same.Please any one can modify my rails code such that i will get
results from both php and rails.
echo up_getEncrypted('mypassword', 1);
function up_getEncrypted($password, $user_id)
{
srand();
$td = mcrypt_module_open('rijndael-128', '', 'cbc', '');
$keylen = mcrypt_enc_get_key_size($td);
$key = substr($password, 0, $keylen);
$key = str_pad($key, $keylen);
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
if (mcrypt_generic_init($td, $key, $iv) != -1)
{
$pad = 16 - strlen($user_id);
$mypadded = $user_id . str_repeat(chr($pad), $pad);
$c_t = mcrypt_generic($td, pack('H*',
md5($user_id)).$user_id);
mcrypt_generic_deinit($td);
$result = base64_encode($iv.$c_t);
return $result;
}
else
{
return "";
}
}
result = up_getEncrypted('mypassword', 1)
puts result
def up_getEncrypted(password, user_id)
c = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
c.encrypt
c.key = key =
Digest::SHA1.hexdigest(password).unpack('a2'*32).map{|x|
x.hex}.pack('c'*32)
c.iv = c.random_iv
e = c.update(user_id.to_s)
e << c.final
return e
end
Output of php encryption code and output of rails encryption code are
not same.Please any one can modify my rails code such that i will get
results from both php and rails.