Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'base64'
require 'digest'
require 'openssl'
def encode(cryptkey, iv, cleardata)
cipher = OpenSSL::Cipher.new('AES-256-CBC')
cipher.encrypt # set cipher to be encryption mode
cipher.key = cryptkey
cipher.iv = iv
encrypted = ''
encrypted << cipher.update(cleardata)
encrypted << cipher.final
encrypted
end
def b64enc(data)
Base64.encode64(data).gsub(/\n/, '')
end
cryptkey = Digest::SHA256.digest('Nixnogen')
iv = 'a2xhcgAAAAAAAAAA'
buf = "Here is some data for the coding" # 32 chars
enc = encode(cryptkey, iv, buf)
puts "Encoded length: #{enc.length}"
puts "Encoded in Base64: " + b64enc(enc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment