Skip to content

Instantly share code, notes, and snippets.

@eprothro eprothro/encryption.rb
Last active Dec 20, 2017

Embed
What would you like to do?
require 'openssl'
require 'base64'
module Cipher
extend self
def encrypt(plaintext)
encode(aes(plaintext))
end
def decrypt(encoded_encrypted)
aes_decrypt(decode(encoded_encrypted))
end
private
KEY = "vVLkVzo15UDK12ULC8oVldnHQqDUEdUjJ0bWvNxBMs0="
IV_LENGTH = 16
def encryption_cipher
Thread.current[:encryption_cipher] ||= begin
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
cipher.encrypt
cipher.key = key
cipher
end
end
def decryption_cipher
Thread.current[:decryption_cipher] ||= begin
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
cipher.decrypt
cipher.key = key
cipher
end
end
def aes(plaintext)
encryption_cipher.iv = iv = encryption_cipher.random_iv
raise "IV length != #{IV_LENGTH} (#{iv.length})" unless iv.length == IV_LENGTH
cipher_text = encryption_cipher.update(plaintext) + encryption_cipher.final
iv + cipher_text
end
def aes_decrypt(unencoded_encrypted)
iv = unencoded_encrypted.slice!(0, IV_LENGTH)
decryption_cipher.iv = iv
decryption_cipher.update(unencoded_encrypted) + decryption_cipher.final
end
def encode(str)
Base64.strict_encode64(str).gsub(/\s/, '')
end
def decode(encoded_str)
Base64.strict_decode64(encoded_str)
end
def key
decode(KEY)
end
end
plain = "We were sitting on the front porch\nWith the weather rolling in\nLaughing louder than the big south wind\nYou ran out to roll your window\nLight rain falling on your hair\nYour tan legs checkered from a folding chair\nThere's country music in the kitchen\nI hear it singing through the screen\nWeather warnings in between\nGlad you got out of the city\nThere's no telling what's in store\nAlong the 35 Corridor\nKerosene to feed the flame\nYour effect is quite the same\nShadows dancing on the wall and\nWaiting for the sky to fall\nCouldn't ask for better weather\nYou were sayin with a grin\nUntil the sound of hailstone hitting tin\nIt's loud enough you gotta yell now\nThe whole thing hits me like a song\nA pretty one that won't last long\nKerosene to feed the flame\nYour effect is quite the same\nShadows dancing on the wall and\nWaiting for the sky to fall and\nWaiting for the sky to fall\nYeah in the broken morning light\nThat simple shade of blue\nThe kind that always follow you\nKerosene to feed the flame\nYour effect is quite the same\nShadows dancing on the wall and\nWaitingWaitingWaitingWaitingand\nWaiWaiWaiWaiWaiWaiWaiWaiall"
str = Cipher.encrypt(plain)
p str
p Cipher.decrypt str
"Uki8brWOC9seu3Ld68iRGKCGkqg+YUyprjJHLiV44Tn5AsrfnzojubnkVw3u5DpgCxqTMFh3m3UFPa1NdimtOBbpBAGScImhKSXUtX+mHxnxcYubheQGNDOr+kOxwW3XUM5yz/hdk7wnpP2HIcSxMPvxNqX1XXKI/00pZGRMDKb4vulj4FEK1KxpYxpB7O4g+66p39b99J9V5+PAHRy/WrC4JZtex9GPJDMMe05r7d/DzS7MepeZgL86fakHEH9DJ+D3Aic5YxrMGyW98qWtCW/iCc2ZzW6NlNcY2ysUK7tchYhdtmDwNGNXS9ZqXb/iT/hNcwGI7QFbRtbmKJuKTCyShWhNE1qiBik8CrFR0Z03ZpZMZGtpLksXeJv9ad3u+VP1mtwXE0A0WyW+NXy8FjapzS6FrBuZ64HICOycIhY30z6QjldV6lKMh5VsotsGYr539p1HlH6cXOk6yjU+g661fJSOfXIVXR28kaNm7xiksAWysVL4h3xfOfEbdSRNSDdBIlmp/TU3WYF5QXt97fDeQzopINK8aNrP8QFR/Q573iOA3l9/XSiG+Fld9+5ELVF6fWCx/iM1IeIfLm6I9KFWiZKNlCXC4MIfd5aUtSNqJSBdKCNHkemCv9KR35bB+wKyYWYWxIXFBkrg1htkI7S8Yke/IAesZfy0AcIzl+OpneKe1+pZBydVCKQeUhZowPbMdlquowpdJ/QXvrTAkGYzSxufOa8tuDhFT2TsKCRa8Dz13qPc5hhGxBBDn7dutmQsK6VuT4cp3o8ZPfxkuCNijtUolbZiKsAMlel/jpG6LM10M9IYYwcddPMSn8FehnwHv+RoHvKMww5F3H3ff2IAWW3VVP5Fk1HHfY6Sg31C/C0CPFgguzsuE9vZBzmlL8s2gZ9Pho4l5QlwiLQoi3kPLLNM5sB+XOkJVYtUPCUN0iM8pq1paC8oFEG50dz9v0WaQhHY9VPpML1DTGB2X7W0mE4/DSdnqxeUx2Pll7uqd3BlPxo2772iMMQ5LB5s3+yRGIZLbNLmgs/E3SPJEELvRXhdDwJuWaFXOXvKf5109YdvqaA+3m/p6o5cglySUOkS6eC48cZp9mbXh4l9PAzn09K0fdBYsw2CPpeHXdG1Wp/CNcDkgwTc/W6Qhgc7gGSmxQYmvN57EVNQP9E5ta3H8aaG/w/YHPT7IlLbJ/8OctiA0SLWEuE6grZbJdLQX/l63zCrYyr7y9CdCZSjyoi39LwS/Zdu2cLXagKwwGRdEaRXk56KB/qXMA9YxHkp8tq2iAt7VIeOw0AG3FoG/y3Fw/32zTelFIUTywvZnEte3ND/9DGmSvscZT3l4cuETUppcV1fY4Fh94PMg77ecVLDhXO5bvXAHMw2sGOqTUF24nu6HjLNA2idjqImO1uZlLJTBRWjbqvd/jjaKDSwGHGi7s2Srt0EL/Yl5SU5BamX4DbATBiizIeSb3cqor3RvPa9ojYmo/5jg3wNWUT61zwCaIerreR9XzyemEGVhTs="
"We were sitting on the front porch\nWith the weather rolling in\nLaughing louder than the big south wind\nYou ran out to roll your window\nLight rain falling on your hair\nYour tan legs checkered from a folding chair\nThere's country music in the kitchen\nI hear it singing through the screen\nWeather warnings in between\nGlad you got out of the city\nThere's no telling what's in store\nAlong the 35 Corridor\nKerosene to feed the flame\nYour effect is quite the same\nShadows dancing on the wall and\nWaiting for the sky to fall\nCouldn't ask for better weather\nYou were sayin with a grin\nUntil the sound of hailstone hitting tin\nIt's loud enough you gotta yell now\nThe whole thing hits me like a song\nA pretty one that won't last long\nKerosene to feed the flame\nYour effect is quite the same\nShadows dancing on the wall and\nWaiting for the sky to fall and\nWaiting for the sky to fall\nYeah in the broken morning light\nThat simple shade of blue\nThe kind that always follow you\nKerosene to feed the flame\nYour effect is quite the same\nShadows dancing on the wall and\nWaitingWaitingWaitingWaitingand\nWaiWaiWaiWaiWaiWaiWaiWaiall"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.