Created
July 10, 2015 14:59
-
-
Save dmb2/fbb198a695aa6f360838 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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