Skip to content

Instantly share code, notes, and snippets.

@scotttam
Created September 12, 2013 19:53
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 scotttam/6542931 to your computer and use it in GitHub Desktop.
Save scotttam/6542931 to your computer and use it in GitHub Desktop.
#!/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