Advertisement
Jessehz

diamond.rb

Sep 11th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.03 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # Copyright 2013 Jesse Zilstorff
  3.  
  4. class Diamond
  5.     def swap(stream, key, cipher, offset)
  6.         last = key.first
  7.         stream.bytes.with_index.map do |byte, index|
  8.             key.unshift(last ^= (index+offset) ^ key.pop)
  9.             byte ^ last ^ cipher.getbyte(index)
  10.         end.pack("C*")
  11.     end
  12.     def encrypt(instream, outstream, key)
  13.         blksize = instream.stat.blksize
  14.         offset = key.pop
  15.         buffer = 0x00.chr*blksize
  16.         cipher = swap(buffer, key, buffer, offset)
  17.         while instream.read(blksize, buffer)
  18.             cipher = swap(buffer, key, cipher, offset)
  19.             outstream.write(cipher)
  20.         end
  21.     end
  22.     def decrypt(instream, outstream, key)
  23.         blksize = instream.stat.blksize
  24.         offset = key.pop
  25.         buffer = 0x00.chr*blksize
  26.         cipher = swap(buffer, key, buffer, offset)
  27.         while instream.read(blksize, buffer)
  28.             outstream.write(swap(buffer, key, cipher, offset))
  29.             cipher.replace(buffer)
  30.         end
  31.     end
  32. end
  33.  
  34. Diamond.new.send(
  35.     ARGV.shift,
  36.     File.new(ARGV.shift),
  37.     File.new(ARGV.shift, IO::CREAT|IO::WRONLY),
  38.     "PUT YOUR RANDOM-LOOKING KEY HERE".bytes.to_a
  39. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement