Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AES128 / AES256 CBC with PKCS7Padding in Ruby
require "openssl"
require "digest"
def aes128_cbc_encrypt(key, data, iv)
key = Digest::MD5.digest(key) if(key.kind_of?(String) && 16 != key.bytesize)
iv = Digest::MD5.digest(iv) if(iv.kind_of?(String) && 16 != iv.bytesize)
aes = OpenSSL::Cipher.new('AES-128-CBC')
aes.encrypt
aes.key = key
aes.iv = iv
aes.update(data) + aes.final
end
def aes256_cbc_encrypt(key, data, iv)
key = Digest::SHA256.digest(key) if(key.kind_of?(String) && 32 != key.bytesize)
iv = Digest::MD5.digest(iv) if(iv.kind_of?(String) && 16 != iv.bytesize)
aes = OpenSSL::Cipher.new('AES-256-CBC')
aes.encrypt
aes.key = key
aes.iv = iv
aes.update(data) + aes.final
end
def aes128_cbc_decrypt(key, data, iv)
key = Digest::MD5.digest(key) if(key.kind_of?(String) && 16 != key.bytesize)
iv = Digest::MD5.digest(iv) if(iv.kind_of?(String) && 16 != iv.bytesize)
aes = OpenSSL::Cipher.new('AES-128-CBC')
aes.decrypt
aes.key = key
aes.iv = iv
aes.update(data) + aes.final
end
def aes256_cbc_decrypt(key, data, iv)
key = Digest::SHA256.digest(key) if(key.kind_of?(String) && 32 != key.bytesize)
iv = Digest::MD5.digest(iv) if(iv.kind_of?(String) && 16 != iv.bytesize)
aes = OpenSSL::Cipher.new('AES-256-CBC')
aes.decrypt
aes.key = key
aes.iv = iv
aes.update(data) + aes.final
end
@efreesen

This comment has been minimized.

Copy link

@efreesen efreesen commented Feb 1, 2013

Hey in line 24 and 32 you are doing the digest again.

And thank you for the code ;)

@xxxazxxx

This comment has been minimized.

Copy link

@xxxazxxx xxxazxxx commented Feb 25, 2013

@efreesen is right. there is problem on line 24 and 32.

it should be
aes.key = key

@fooledbyprimes

This comment has been minimized.

Copy link

@fooledbyprimes fooledbyprimes commented Apr 14, 2013

Nice. If I want to encrypt using openssl command line and then decrypt in Ruby then I always get things messed up in the Ruby decypt code. I think it would be helpful to have the command line analogs for comparison for newbies.

@JasonBristol

This comment has been minimized.

Copy link

@JasonBristol JasonBristol commented Dec 26, 2013

Thank you, this is helpful

@RiANOl

This comment has been minimized.

Copy link
Owner Author

@RiANOl RiANOl commented Feb 4, 2014

Thanks, fixed

@awesome

This comment has been minimized.

Copy link

@awesome awesome commented Feb 8, 2014

awesome!

@nurettin

This comment has been minimized.

Copy link

@nurettin nurettin commented Jan 7, 2017

How do you decrypt these in java?

@edgardo001

This comment has been minimized.

Copy link

@edgardo001 edgardo001 commented Oct 25, 2017

thanks, it works in ruby ​​2.4 on windows 10!

@arslanaly47

This comment has been minimized.

Copy link

@arslanaly47 arslanaly47 commented Nov 11, 2017

How to go for PKCS5 encrypting in Ruby? I can't find any information on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment