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/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 = "".force_encoding("ASCII-8BIT") 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
|