Ruby des encrypt

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_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
$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 =

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 =“des”)
c.key = “4dddb3b191a1c2d8ea1sxcv23”
#or c.pkcs5_keyivgen(“4dddb3b191a1c2d8ea1sxcv23”)
e = c.update(“hellcatjack:111111”)
e <<
puts Base64.encode64(e)

it’s not the same as php…


Did you try other kind of DES encryption in Ruby ?


c =‘DES-EDE3-CBC’)

In order to know the different kind of alorithms, type:

[15:44] pierre > openssl
OpenSSL> help
openssl:Error: ‘help’ is an invalid command.

Standard commands
asn1parse ca ciphers crl crl2pkcs7
dgst dh dhparam dsa dsaparam
enc engine errstr gendh gendsa
genrsa nseq ocsp passwd pkcs12
pkcs7 pkcs8 prime rand req
rsa rsautl s_client s_server s_time
sess_id smime speed spkac verify
version x509

Message Digest commands (see the `dgst’ command for more details)
md2 md4 md5 mdc2 rmd160
sha sha1

Cipher commands (see the `enc’ command for more details)
aes-128-cbc aes-128-ecb aes-192-cbc aes-192-ecb aes-256-cbc
aes-256-ecb base64 bf bf-cbc bf-cfb
bf-ecb bf-ofb cast cast-cbc cast5-cbc
cast5-cfb cast5-ecb cast5-ofb des des-cbc
des-cfb des-ecb des-ede des-ede-cbc des-ede-cfb
des-ede-ofb des-ede3 des-ede3-cbc des-ede3-cfb des-ede3-ofb
des-ofb des3 desx rc2 rc2-40-cbc
rc2-64-cbc rc2-cbc rc2-cfb rc2-ecb rc2-ofb
rc4 rc4-40 rc5 rc5-cbc rc5-cfb
rc5-ecb rc5-ofb