-
-
Save squarelover/a880ea13d3b65a21a99d to your computer and use it in GitHub Desktop.
Encryption woes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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