D
Dianhui Nie
I'm trying to using ruby des encrypt ,
in php(some gives me )
function encrypt($input) {
$size = mcrypt_get_block_size('des', 'ecb');
$input = $this->pkcs5_pad($input, $size);
$key = $this->key;
$td = mcrypt_module_open('des', '', 'ecb', '');
$iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td),
MCRYPT_RAND);
@mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
}
function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
or in java
public String encrypt(String input) throws Exception {
return base64Encode(desEncrypt(input.getBytes()));
}
public static String base64Encode(byte[] s) {
return new Base64Encoder().encode(s);
}
public byte[] desDecrypt(byte[] encryptText) throws Exception {
byte rawKeyData[] = desKey;
DESKeySpec dks = new DESKeySpec(rawKeyData);
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte encryptedData[] = encryptText;
byte decryptedData[] = cipher.doFinal(encryptedData);
return decryptedData;
}
how can i translate this into ruby?
require 'openssl'
require 'Base64'
c = OpenSSL::Cipher::Cipher.new("des")
c.encrypt
c.key = "4dddb3b191a1c2d8ea1sxcv23"
#or c.pkcs5_keyivgen("4dddb3b191a1c2d8ea1sxcv23")
e = c.update("hellcatjack:111111")
e << c.final
puts Base64.encode64(e)
it's not the same as php...
in php(some gives me )
function encrypt($input) {
$size = mcrypt_get_block_size('des', 'ecb');
$input = $this->pkcs5_pad($input, $size);
$key = $this->key;
$td = mcrypt_module_open('des', '', 'ecb', '');
$iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td),
MCRYPT_RAND);
@mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
return $data;
}
function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
or in java
public String encrypt(String input) throws Exception {
return base64Encode(desEncrypt(input.getBytes()));
}
public static String base64Encode(byte[] s) {
return new Base64Encoder().encode(s);
}
public byte[] desDecrypt(byte[] encryptText) throws Exception {
byte rawKeyData[] = desKey;
DESKeySpec dks = new DESKeySpec(rawKeyData);
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte encryptedData[] = encryptText;
byte decryptedData[] = cipher.doFinal(encryptedData);
return decryptedData;
}
how can i translate this into ruby?
require 'openssl'
require 'Base64'
c = OpenSSL::Cipher::Cipher.new("des")
c.encrypt
c.key = "4dddb3b191a1c2d8ea1sxcv23"
#or c.pkcs5_keyivgen("4dddb3b191a1c2d8ea1sxcv23")
e = c.update("hellcatjack:111111")
e << c.final
puts Base64.encode64(e)
it's not the same as php...