Created
November 22, 2012 08:15
-
-
Save lukasklein/4129944 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
from sbox import FakeBinary | |
class IP: | |
def __init__(self): | |
self.ip = [ | |
58,50,42,34,26,18,10,2, | |
60,52,44,36,28,20,12,4, | |
62,54,46,38,30,22,14,6, | |
64,56,48,40,32,24,16,8, | |
57,49,41,33,25,17,9,1, | |
59,51,43,35,27,19,11,3, | |
61,53,45,37,29,21,13,5, | |
63,55,47,39,31,23,15,7 | |
] | |
self.ip1 = [ | |
40,8,48,16,56,24,64,32, | |
39,7,47,15,55,23,63,31, | |
38,6,46,14,54,22,62,30, | |
37,5,45,13,53,21,61,29, | |
36,4,44,12,52,20,60,28, | |
35,3,43,11,51,19,59,27, | |
34,2,42,10,50,18,58,26, | |
33,1,41,9,49,17,57,25 | |
] | |
def genip(self, value, invert=False): | |
def genip(value): | |
for pos in (self.ip1 if invert else self.ip): | |
yield str(value)[pos - 1] | |
return ''.join(genip(value)) | |
if __name__ == '__main__': | |
myip = IP() | |
for i in range(15): | |
print "Proof %d\\\\" % (i + 1) | |
# Random integer, possible to diaplay with 64 bits | |
number = random.randrange(0, 18446744073709551616L, 1) | |
inverse = myip.genip(FakeBinary(number, 64)) | |
print "$x \t\t= %s$\\\\" % (FakeBinary(number, 64)) | |
print "$IP(x) \t\t= %s$\\\\" % (inverse) | |
params = (myip.genip(FakeBinary(inverse, 64), invert=True)) | |
print "$IP^{-1}(IP(x)) \t= %s$\\\\" % params | |
print "\\\\" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FakeBinary: | |
def __init__(self, value, padding=6): | |
self.padding = padding | |
if type(value) == str: | |
self.from_string(value) | |
else: | |
self.from_int(value) | |
def from_int(self, value): | |
self.value = value | |
def from_string(self, value): | |
if len(value) != self.padding: | |
raise ValueError('Input must be 6 bits!') | |
self.value = 0 | |
for i in range(len(value)): | |
self.value += 2 ** i * int(value[len(value) - i - 1]) | |
def __unicode__(self): | |
bstring = bin(self.value)[2:] | |
return (self.padding - len(bstring)) * '0' + bstring | |
def __str__(self): | |
return unicode(self).encode('utf-8') | |
def __int__(self): | |
return self.value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment