Skip to content

Instantly share code, notes, and snippets.

@dmb2
Created July 10, 2015 14:59
Show Gist options
  • Save dmb2/fbb198a695aa6f360838 to your computer and use it in GitHub Desktop.
Save dmb2/fbb198a695aa6f360838 to your computer and use it in GitHub Desktop.
class StreamCrypto
def self.aes_ctr_encrypt(clear_text,nonce,key)
counter=nonce.unpack('q')[0]
keystream = ""
cipher = OpenSSL::Cipher.new 'aes128'
cipher.padding=0
cipher.encrypt
cipher.key=key
(Float(clear_text.length)/key.length).ceil().times do
stream_nonce="\0"*8+[counter].pack('q')
puts stream_nonce.inspect
keystream += cipher.update(stream_nonce)
counter+=1
end
return CryptoTools.xor_str(keystream.slice(0,clear_text.length),clear_text)
end
def self.aes_ctr_decrypt(cipher_text,nonce,key)
aes_ctr_encrypt(cipher_text,nonce,key)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment