Skip to content

Instantly share code, notes, and snippets.

@squarelover
Created August 25, 2011 19:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squarelover/a880ea13d3b65a21a99d to your computer and use it in GitHub Desktop.
Save squarelover/a880ea13d3b65a21a99d to your computer and use it in GitHub Desktop.
Encryption woes
var crypto = require('crypto');
var algorithm = 'aes-256-cbc';
var password = 'opensesame';
function decode(cryptkey, iv, secretdata) {
var decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv);
var decoded = decipher.update(secretdata);
decoded += decipher.final();
return decoded;
}
function ivBase64ToBin(ivb64) {
var iv = new Buffer(ivb64,"base64");
return iv.toString('binary');
}
function keyToBinHash(pwd) {
var key = crypto.createHash('sha256').update(pwd).digest();
return key;
}
function base64DataToBin(dat64) {
var data = new Buffer(dat64, "base64");
return data;
}
var ivb64 = "wSSjGr153Wwm2ozcIwcfzQ==\n"
var encodedData = "wSSjGr153Wwm2ozcIwcfzQ==\n"
var out = decode(keyToBinHash(password), ivBase64ToBin(ivb64), base64DataToBin(encodedData));
console.log("ciphertext: " + out);
require 'openssl'
require 'base64'
algorithm = 'aes-256-cbc'
password = 'opensesame'
e = nil
key = Digest::SHA256.digest(password)
c = OpenSSL::Cipher::Cipher.new(algorithm)
c.encrypt
c.key = key
c.iv = iv = c.random_iv
e = c.update "secret message"
e << c.final
p "HEX: #{e.unpack("H*")[0]}"
p "BASE64: #{Base64.encode64(e)}"
p "IV BASE64: #{Base64.encode64(iv)}"
p "IV HEX: #{iv.unpack("H*")[0]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment