Skip to content

Instantly share code, notes, and snippets.

@ichernev
Created August 29, 2012 22:46
Show Gist options
  • Save ichernev/3519888 to your computer and use it in GitHub Desktop.
Save ichernev/3519888 to your computer and use it in GitHub Desktop.
node vs ruby openssl
var crypto = require('crypto')
var rep = function(times, elem) {
var res = new Array(times);
for (var i = 0; i < times; ++i) {
res[i] = elem;
}
return res;
};
var bs = 192;
if (process.argv.length == 4) {
var key = new Buffer(process.argv[2].split(' ').map(function(hex) {
return parseInt(hex, 16);
}));
var iv = new Buffer(process.argv[3].split(' ').map(function(hex) {
return parseInt(hex, 16);
}));
} else {
var keyLen = bs / 8;
var key = new Buffer(rep(keyLen, 200));
var ivLen = 16;
var iv = new Buffer(rep(ivLen, 200));
}
var cipher = crypto.createCipheriv('AES-' + bs + '-CBC', key, iv);
var plain = "ilovethisgameZz";
var encrypted = cipher.update(plain, 'ascii', 'base64') + cipher.final('base64');
console.log(key);
console.log(iv);
console.log(encrypted);
require 'base64'
require 'openssl'
bs = 192
key_len = bs / 8
iv_len = 16
plain = "ilovethisgameZz"
if ARGV.size == 2
key = ARGV[0].split(' ').map { |i| Integer(i, 16) }.pack('C*')
iv = ARGV[1].split(' ').map { |i| Integer(i, 16) }.pack('C*')
else
key = key_len.times.map { rand(256) }.pack('C*')
iv = iv_len.times.map { rand(256) }.pack('C*')
end
cipher = OpenSSL::Cipher::AES.new(bs, :CBC)
cipher.encrypt
# key = cipher.random_key
# iv = cipher.random_iv
cipher.key = key
cipher.iv = iv
encrypted = cipher.update(plain) + cipher.final
puts key.unpack('C*').map { |i| "%x" % i }.join(' ')
puts iv.unpack('C*').map { |i| "%x" % i }.join(' ')
puts Base64.encode64(encrypted)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment