Skip to content

Instantly share code, notes, and snippets.

@masciugo
Last active December 12, 2018 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masciugo/00a2760c74bf8e2d31a907df962fd305 to your computer and use it in GitHub Desktop.
Save masciugo/00a2760c74bf8e2d31a907df962fd305 to your computer and use it in GitHub Desktop.
require 'openssl'
require "base64"
KEY = '1'*32
IV = '1'*16
ALG = 'aes-256-cbc'
def encrypt(plaintext)
cipher = OpenSSL::Cipher.new(ALG)
cipher.encrypt
cipher.key = KEY
cipher.iv = IV
ciphertext = cipher.update(plaintext) + cipher.final
Base64.encode64(ciphertext)
end
def decrypt(ciphertext)
decipher = OpenSSL::Cipher.new(ALG)
decipher.decrypt
decipher.key = KEY
decipher.iv = IV
plaintext = decipher.update(Base64.decode64(ciphertext)) + decipher.final
end
a1 = "00000001"
a2 = "00000002"
a3 = "00000003"
puts encrypt a1 # => yKGDSvFtoy0x2k2bIpbvwA==
puts encrypt a2 # => 2obWz39l1VJr9Ck9X+LDPg==
puts encrypt a3 # => b7NnnjNKSyz8WI2HmASqhQ==
puts decrypt encrypt a1 # => 00000001
puts decrypt encrypt a2 # => 00000002
puts decrypt encrypt a3 # => 00000003
require 'openssl'
KEY = '1'*32
IV = '1'*16
ALG = 'aes-256-cbc'
def bin2hex(s)
s.unpack('H*').first
end
def hex2bin(s)
s.scan(/../).map { |x| x.hex }.pack('c*')
end
def encrypt(plaintext)
cipher = OpenSSL::Cipher.new(ALG)
cipher.encrypt
cipher.key = KEY
cipher.iv = IV
ciphertext = cipher.update(plaintext)
ciphertext << cipher.final
ciphertext
# bin2hex(ciphertext)
end
def decrypt(ciphertext)
decipher = OpenSSL::Cipher.new(ALG)
decipher.decrypt
decipher.key = KEY
decipher.iv = IV
# plaintext = decipher.update(hex2bin(ciphertext))
plaintext = decipher.update(ciphertext)
plaintext << decipher.final
end
a1 = "00000001"
a2 = "00000002"
a3 = "00000003"
puts encrypt a1 # => c8a1834af16da32d31da4d9b2296efc0
puts encrypt a2 # => da86d6cf7f65d5526bf4293d5fe2c33e
puts encrypt a3 # => 6fb3679e334a4b2cfc588d879804aa85
puts decrypt encrypt a1 # => 00000001
puts decrypt encrypt a2 # => 00000002
puts decrypt encrypt a3 # => 00000003
require 'openssl'
KEY = '1'*32
IV = '1'*12
ALG = 'aes-256-gcm'
def bin2hex(s)
s.unpack('H*').first
end
def hex2bin(s)
s.scan(/../).map { |x| x.hex }.pack('c*')
end
def encrypt(plaintext)
cipher = OpenSSL::Cipher.new(ALG)
cipher.encrypt
cipher.key = KEY
cipher.iv = IV
ciphertext = cipher.update(plaintext)
bin2hex(ciphertext)
end
def decrypt(ciphertext)
decipher = OpenSSL::Cipher.new(ALG)
decipher.decrypt
decipher.key = KEY
decipher.iv = IV
plaintext = decipher.update(hex2bin(ciphertext))
end
a1 = "00000001"
a2 = "00000002"
a3 = "00000003"
puts encrypt a1 # => c8a1834af16da32d31da4d9b2296efc0
puts encrypt a2 # => da86d6cf7f65d5526bf4293d5fe2c33e
puts encrypt a3 # => 6fb3679e334a4b2cfc588d879804aa85
puts decrypt encrypt a1 # => 00000001
puts decrypt encrypt a2 # => 00000002
puts decrypt encrypt a3 # => 00000003
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment