Newer version of pydes

This commit is contained in:
Miroslav Stampar 2015-11-07 22:41:56 +01:00
parent 78e3e52ab0
commit 08054dec7b

View File

@ -230,7 +230,7 @@ class _baseDes(object):
# there is no way to correctly decode the data into bytes.
if _pythonMajorVersion < 3:
if isinstance(data, unicode):
data = data.encode('utf8')
raise ValueError("pyDes can only work with bytes, not Unicode strings.")
else:
if isinstance(data, str):
# Only accept ascii unicode values.
@ -419,37 +419,32 @@ class des(_baseDes):
# Turn the strings into integers. Python 3 uses a bytes
# class, which already has this behaviour.
data = [ord(c) for c in data]
result = [False] * len(data) * 8
l = len(data) * 8
result = [0] * l
pos = 0
for ch in data:
result[pos + 0] = (ch & (1 << 7) != 0)
result[pos + 1] = (ch & (1 << 6) != 0)
result[pos + 2] = (ch & (1 << 5) != 0)
result[pos + 3] = (ch & (1 << 4) != 0)
result[pos + 4] = (ch & (1 << 3) != 0)
result[pos + 5] = (ch & (1 << 2) != 0)
result[pos + 6] = (ch & (1 << 1) != 0)
result[pos + 7] = (ch & (1 << 0) != 0)
pos += 8
i = 7
while i >= 0:
if ch & (1 << i) != 0:
result[pos] = 1
else:
result[pos] = 0
pos += 1
i -= 1
return result
def __BitList_to_String(self, data):
"""Turn the list of bits -> data, into a string"""
result = [0] * (len(data) >> 3)
result = []
pos = 0
c = 0
while pos < len(data):
c = data[pos + 0] << (7 - 0)
c += data[pos + 1] << (7 - 1)
c += data[pos + 2] << (7 - 2)
c += data[pos + 3] << (7 - 3)
c += data[pos + 4] << (7 - 4)
c += data[pos + 5] << (7 - 5)
c += data[pos + 6] << (7 - 6)
c += data[pos + 7] << (7 - 7)
result[pos >> 3] = c
pos += 8
c += data[pos] << (7 - (pos % 8))
if (pos % 8) == 7:
result.append(c)
c = 0
pos += 1
if _pythonMajorVersion < 3:
return ''.join([ chr(c) for c in result ])
@ -458,8 +453,7 @@ class des(_baseDes):
def __permutate(self, table, block):
"""Permutate this block with the specified table"""
#return map(lambda x: block[x], table)
return list(block[x] for x in table)
return list(map(lambda x: block[x], table))
# Transform the secret key, so that it is ready for data processing
# Create the 16 subkeys, K[1] - K[16]
@ -491,7 +485,6 @@ class des(_baseDes):
def __des_crypt(self, block, crypt_type):
"""Crypt the block of data through DES bit-manipulation"""
block = self.__permutate(des.__ip, block)
Bn = [0] * 32
self.L = block[:32]
self.R = block[32:]
@ -513,7 +506,7 @@ class des(_baseDes):
self.R = self.__permutate(des.__expansion_table, self.R)
# Exclusive or R[i - 1] with K[i], create B[1] to B[8] whilst here
self.R = map(lambda x, y: x ^ y, self.R, self.Kn[iteration])
self.R = list(map(lambda x, y: x ^ y, self.R, self.Kn[iteration]))
B = [self.R[:6], self.R[6:12], self.R[12:18], self.R[18:24], self.R[24:30], self.R[30:36], self.R[36:42], self.R[42:]]
# Optimization: Replaced below commented code with above
#j = 0
@ -526,6 +519,7 @@ class des(_baseDes):
# Permutate B[1] to B[8] using the S-Boxes
j = 0
Bn = [0] * 32
pos = 0
while j < 8:
# Work out the offsets
@ -548,7 +542,7 @@ class des(_baseDes):
self.R = self.__permutate(des.__p, Bn)
# Xor with L[i - 1]
self.R = map(lambda x, y: x ^ y, self.R, self.L)
self.R = list(map(lambda x, y: x ^ y, self.R, self.L))
# Optimization: This now replaces the below commented code
#j = 0
#while j < len(self.R):
@ -609,7 +603,7 @@ class des(_baseDes):
# Xor with IV if using CBC mode
if self.getMode() == CBC:
if crypt_type == des.ENCRYPT:
block = map(lambda x, y: x ^ y, block, iv)
block = list(map(lambda x, y: x ^ y, block, iv))
#j = 0
#while j < len(block):
# block[j] = block[j] ^ iv[j]
@ -618,7 +612,7 @@ class des(_baseDes):
processed_block = self.__des_crypt(block, crypt_type)
if crypt_type == des.DECRYPT:
processed_block = map(lambda x, y: x ^ y, processed_block, iv)
processed_block = list(map(lambda x, y: x ^ y, processed_block, iv))
#j = 0
#while j < len(processed_block):
# processed_block[j] = processed_block[j] ^ iv[j]