updated chapter 4 and appendix-A files
This commit is contained in:
55
appendix-A/arcfour.py
Normal file
55
appendix-A/arcfour.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""RC4 compatible algorithm"""
|
||||
|
||||
def arcfour(key, in_bytes, loops=20):
|
||||
|
||||
kbox = bytearray(256) # create key box
|
||||
for i, car in enumerate(key): # copy key and vector
|
||||
kbox[i] = car
|
||||
j = len(key)
|
||||
for i in range(j, 256): # repeat until full
|
||||
kbox[i] = kbox[i-j]
|
||||
|
||||
# [1] initialize sbox
|
||||
sbox = bytearray(range(256))
|
||||
|
||||
# repeat sbox mixing loop, as recommened in CipherSaber-2
|
||||
# http://ciphersaber.gurus.com/faq.html#cs2
|
||||
j = 0
|
||||
for k in range(loops):
|
||||
for i in range(256):
|
||||
j = (j + sbox[i] + kbox[i]) % 256
|
||||
sbox[i], sbox[j] = sbox[j], sbox[i]
|
||||
|
||||
# main loop
|
||||
i = 0
|
||||
j = 0
|
||||
out_bytes = bytearray()
|
||||
|
||||
for car in in_bytes:
|
||||
i = (i + 1) % 256
|
||||
# [2] shuffle sbox
|
||||
j = (j + sbox[i]) % 256
|
||||
sbox[i], sbox[j] = sbox[j], sbox[i]
|
||||
# [3] compute t
|
||||
t = (sbox[i] + sbox[j]) % 256
|
||||
k = sbox[t]
|
||||
car = car ^ k
|
||||
out_bytes.append(car)
|
||||
|
||||
return out_bytes
|
||||
|
||||
|
||||
def test():
|
||||
from time import time
|
||||
clear = bytearray(b'1234567890' * 100000)
|
||||
t0 = time()
|
||||
cipher = arcfour(b'key', clear)
|
||||
print('elapsed time: %.2fs' % (time() - t0))
|
||||
result = arcfour(b'key', cipher)
|
||||
assert result == clear, '%r != %r' % (result, clear)
|
||||
print('elapsed time: %.2fs' % (time() - t0))
|
||||
print('OK')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test()
|
||||
Reference in New Issue
Block a user