Hi I have a node js version AES-256-cbc
encrpt and decrypt function
And I want convert it into Ruby version
Sadly, it didn't work.
I wonder why there is no (initial vector in node js version)
What I am most interesting in is that
How convert the following into Ruby
Message_buf.fill('\0');
cipher.setAutoPadding(auto_padding = false);
function encrypt(Text){
var cipher = crypto.createCipher('aes-256-cbc','fldkfew')
var Message_buf = new Buffer(Text.length + (16 - Text.length % 16));
Message_buf.fill('\0');
Message_buf.write(Text, 0, Text.length);
cipher.setAutoPadding(auto_padding = false);
//var crypted = cipher.update(Text,'utf8','hex')
var crypted = cipher.update(Message_buf,'utf8','binary')
crypted += cipher.final('binary');
return crypted;
}
function decrypt(Text){
var decipher = crypto.createDecipher('aes-256-cbc','fldkfew')
decipher.setAutoPadding(auto_padding = false);
var dec = decipher.update(Text,'binary','utf8')
dec += decipher.final('utf8');
var pos = dec.indexOf('\0');
if(pos < 0)
{
return dec;
}
else
{
return dec.slice(0, pos);
}
}
def aes256_cbc_encrypt(data,key='fldkfew', ="231vxw")
key = Digest::SHA256.digest(key)
= Digest::MD5.digest()
aes = OpenSSL::Cipher.new('AES-256-CBC')
aes.encrypt
aes.key = key
aes. = @random_
encrypted_data = aes.update(data) << aes.final
p "encrypted_data:"+encrypted_data
return encrypted_data
end
def aes256_cbc_decrypt(data,key='fldkfew', ="231vxw")
key = Digest::SHA256.digest(key)
= @random_
aes = OpenSSL::Cipher.new('AES-256-CBC')
aes.decrypt
aes.key = key
aes. = @random_
aes.update(data._s) + aes.final
end
Here a working version of your script. Openssl cipher is a bit cumbersome because the key and the data need to have a length that is a multiple of the bit-length (256, 384 or 512), in your example 32 bytes. cipher.padding = 0 turns of auto-padding (otherwise error) so you have to do the padding yourself for key and data. The use of the random generated private key is not realy necessary but recommended.
require 'openssl'
def encrypt text, key
cipher = OpenSSL::Cipher.new('AES-256-CBC')
private_key = cipher.random_iv
cipher.encrypt
cipher.padding = 0 #**
cipher.key = key
[private_key, cipher.update(text) + cipher.final]
end
def decrypt encrypted, public_key, private_key
decipher = OpenSSL::Cipher.new('AES-256-CBC')
decipher.decrypt
decipher.padding = 0 #**
decipher.key = public_key
decipher.iv = private_key
decipher.update(encrypted) << decipher.final
end
def pad text, length=32
text.ljust length
end
text = pad('Hello World') #pad with spaces so the lebgth is a multitude of 32 (=AES-256-CBC = 256 bits)
public_key = 'this_must_be_a_string_of_32_char'
encrypted = encrypt text, public_key
p encrypted #this is an array of the random generated private_key and the encrypted data
private_key = encrypted[0]
encrypted_data = encrypted[1]
decrypted = decrypt encrypted_data, public_key, private_key
puts text == decrypted
puts decrypted.strip
# gives
# ["G\xF8\x9B`\x8FQ\x15\x9F>xN\x0Ex\xCF\x9A\xAA", "\xB5\xE1\v\xCAU\xBE\x18UZv\xC2\xA7\xDE\x8FI\x02\xE4\x83\xB4\x9E\x04-\x13q_\x9E\xC8[n\xE5;b"]
# true
# Hello World
#** out of the documentation
# padding = integer → integer
# Enables or disables padding. By default encryption operations are padded using standard block padding and the padding is checked and removed when decrypting. If the pad parameter is zero then no padding is performed, the total amount of data encrypted or decrypted must then be a multiple of the block size or an error will occur.
# See EVP_CIPHER_CTX_set_padding for further information.