Module: Crypt::Noise

Included in:
String
Defined in:
lib/flatulent/crypt/noise.rb

Instance Method Summary collapse

Instance Method Details

#add_noise(newLength) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/flatulent/crypt/noise.rb', line 9

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<<bit) if bitmap[byte * 8 + bit]
    }
    noisyMessage << c.chr
  }
  posInMessage = 0
  0.upto(usableNoisyMessageLength-1) { |pos|
    if bitmap[pos]
      meaningfulByte = message[posInMessage]
      noisyMessage << meaningfulByte
      posInMessage = posInMessage.next
    else
      noiseByte = rand(256).chr
      noisyMessage << noiseByte
    end
  }
  remainingBytes.times {
      noiseByte = rand(256).chr
      noisyMessage << noiseByte
  }
  return(noisyMessage)
end

#remove_noiseObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/flatulent/crypt/noise.rb', line 63

def remove_noise
  noisyMessage = self
  bitmapSize = noisyMessage.length / 9
  actualMessageLength =  bitmapSize * 8
  
  actualMessageStart = bitmapSize
  actualMessageFinish = bitmapSize + actualMessageLength - 1
  actualMessage = noisyMessage[actualMessageStart..actualMessageFinish]
  
  bitmap = []
  0.upto(bitmapSize - 1) { |byte|
    c = noisyMessage[byte]
    0.upto(7) { |bit|
      bitmap[byte * 8 + bit] = (c[bit] == 1)
    }
  }
  clearMessage = ""
  0.upto(actualMessageLength) { |pos|
    meaningful = bitmap[pos]
    if meaningful
      clearMessage << actualMessage[pos]
    end
  }
  return(clearMessage)
end