class CryptoPan(): def __init__(self, key): if len(key) != 32: raise CryptoPanError("Key must be a 32 byte long string") self.aes = AES(key[0:16]) self.pad = self.aes.encrypt(key[16:32]) f4 = self.pad[0:4] # Python 2 requires explicit conversion to ints if isinstance(f4, str): f4 = [ord(x) for x in f4] f4bp = self.toint(f4) self.masks = [(mask, f4bp & (~mask)) for mask in (0xFFFFFFFF >> (32 - p) << (32 - p) for p in range(0, 32))] def toint(self, array): return array[0] << 24 | array[1] << 16 | array[2] << 8 | array[3] def toarray(self, n): for i in range(3, -1, -1): yield (n >> (i * 8)) & 0xFF def anonymize(self, ip): result = 0 address = [int(x) for x in ip.split(".")] if len(address) != 4: raise CryptoPanError("Invalid IPv4 Address") address = self.toint(address) def calc(a): """ calculate the first bit for Crypto-PAN""" a_array = self.toarray(a) # Python 2 requires converting ints to chars one at a time if isinstance(self.pad, str): inp = "".join((chr(x) for x in a_array)) else: inp = bytes(a_array) inp += self.pad[4:] rin_output = self.aes.encrypt(inp) out = rin_output[0] # Python 2 requires explicit conversion to int if isinstance(out, str): out = ord(out) return out >> 7 addresses = ((address & mask[0]) | mask[1] for mask in self.masks) result = reduce(lambda x, y: x << 1 | y, (calc(a) for a in addresses), 0) return ".".join(["%s" % x for x in self.toarray(result ^ address)])
class CryptoPan(): def __init__(self,key): if len(key)!=32: raise CryptoPanError("Key must be a 32 byte long string") self.aes=AES(key[0:16]) self.pad=self.aes.encrypt(key[16:32]) f4=self.pad[0:4] # Python 2 requires explicit conversion to ints if isinstance(f4, str): f4=[ord(x) for x in f4] f4bp=self.toint(f4) self.masks=[(mask,f4bp & (~ mask)) for mask in (0xFFFFFFFF >> (32-p) << (32-p) for p in range(0,32))] def toint(self,array): return array[0]<<24|array[1]<<16|array[2]<<8|array[3] def toarray(self,n): for i in range(3,-1,-1): yield (n>>(i*8))& 0xFF def anonymize(self,ip): result=0 address=[int(x) for x in ip.split(".")] if len(address)!=4: raise CryptoPanError("Invalid IPv4 Address") address=self.toint(address) def calc(a): """ calculate the first bit for Crypto-PAN""" a_array = self.toarray(a) # Python 2 requires converting ints to chars one at a time if isinstance(self.pad, str): inp="".join((chr(x) for x in a_array)) else: inp=bytes(a_array) inp+=self.pad[4:] rin_output=self.aes.encrypt(inp) out=rin_output[0] # Python 2 requires explicit conversion to int if isinstance(out, str): out=ord(out) return out>>7 addresses=((address & mask[0]) | mask[1] for mask in self.masks) result=reduce(lambda x,y: x<<1 | y, (calc(a) for a in addresses),0) return ".".join(["%s"%x for x in self.toarray(result ^ address)])
def __init__(self, key): if len(key) != 32: raise CryptoPanError("Key must be a 32 byte long string") self.aes = AES(key[0:16]) self.pad = self.aes.encrypt(key[16:32]) pad_int = sum(ord(c) << (i * 8) for i, c in enumerate(self.pad[::-1])) self.masks = [(mask, pad_int & (~ mask)) for mask in ((2 ** 128 - 1) >> (128 - p) << (128 - p) for p in range(0, 128))]
class CryptoPan(): def __init__(self, key): if len(key) != 32: raise CryptoPanError("Key must be a 32 byte long string") self.aes = AES(key[0:16]) self.pad = self.aes.encrypt(key[16:32]) pad_int = sum(ord(c) << (i * 8) for i, c in enumerate(self.pad[::-1])) self.masks = [(mask, pad_int & (~ mask)) for mask in ((2 ** 128 - 1) >> (128 - p) << (128 - p) for p in range(0, 128))] def pack(self, val, num_bytes): return "".join([chr(val >> i * 8 & 0xff) for i in xrange(num_bytes - 1, -1, -1)]) def anonymize(self, ip_in): ip = IPAddress(ip_in) if ip.version == 4: address_bytes = 4 address = ip.value << (128 - 32) else: address_bytes = 16 address = ip.value def calc(a): """ calculate the first bit for Crypto-PAN""" # a is a number, convert to a string rin_input = self.pack(a, 16) rin_output = self.aes.encrypt(rin_input) out = rin_output[0] return ord(out) >> 7 addresses = ((address & mask[0]) | mask[1] for mask in self.masks[0:address_bytes * 8]) result = reduce( lambda x, y: x << 1 | y, (calc(a) for a in addresses), 0) return IPAddress(result ^ ip.value)
def __init__(self, key): if len(key)!=32: raise CryptoPanError("Key must be a 32 byte long string") self.aes=AES(key[0:16]) self.pad=self.aes.encrypt(key[16:32]) f4=self.pad[0:4] # Python 2 requires explicit conversion to ints if isinstance(f4, str): f4=[ord(x) for x in f4] f4bp=self.toint(f4) self.masks=[(mask, f4bp & (~ mask)) for mask in (0xFFFFFFFF >> (32 - p) << (32 - p) for p in range(0, 32))]
def __init__(self,key): if len(key)!=32: raise CryptoPanError("Key must be a 32 byte long string") self.aes=AES(key[0:16]) self.pad=self.aes.encrypt(key[16:32]) f4=self.pad[0:4] # Python 2 requires explicit conversion to ints if isinstance(f4, str): f4=[ord(x) for x in f4] f4bp=self.toint(f4) self.masks=[(mask,f4bp & (~ mask)) for mask in (0xFFFFFFFF >> (32-p) << (32-p) for p in range(0,32))]
def encrypt(self, text): key = self.key cryptor = AES_new(key, MODE_EAX, key) return b64encode(cryptor.encrypt(text.encode()))