# crypt/rattle.rb Richard Kernahan # add_noise - take a message and intersperse noise to make a new noisy message of given byte-length # remove_noise - take a noisy message and extract the message module Crypt module Noise def add_noise(newLength) message = self usableNoisyMessageLength = newLength / 9 * 8 bitmapSize = newLength / 9 remainingBytes = newLength - usableNoisyMessageLength - bitmapSize if (message.length > usableNoisyMessageLength) minimumNewLength = (message.length / 8.0).ceil * 9 puts "For a clear text of #{message.length} bytes, the minimum obscured length" puts "is #{minimumNewLength} bytes which allows for no noise in the message." puts "You should choose an obscured length of at least double the clear text" puts "length, such as #{message.length / 8 * 32} bytes" raise "Insufficient length for noisy message" end bitmap = [] usableNoisyMessageLength.times { bitmap << false } srand(Time.now.to_i) positionsSelected = 0 while (positionsSelected < message.length) positionTaken = rand(usableNoisyMessageLength) if bitmap[positionTaken] next else bitmap[positionTaken] = true positionsSelected = positionsSelected.next end end noisyMessage = "" 0.upto(bitmapSize-1) { |byte| c = 0 0.upto(7) { |bit| c = c + (1<