def encryptHex(p_x, KEY): KEY_b = hexToBin(KEY) assert (len(KEY_b) == 64), 'KEY is not the correct size: encryptString' assert (len(p_x) == 16), 'hex plaintext not long enough' p_b = hexToBin(p_x) c_b = encryptBlock(p_b, KEY_b) c_x = binToHex(c_b) return c_x
def encryptFile(filename, KEY): KEY_b = hexToBin(KEY) assert (len(KEY_b) == 64), 'Incorrect key' l = [] with open(filename, 'rb') as f: while True: b = f.read(8) if b == b'': # end of file break p_b = bytesToBinString(b) c_b = encryptBlock(p_b, KEY_b) c_x = binToHex(c_b) l.append(c_x) # yield (c_x) return ''.join(l)
def sanityCheck1(): x0 = [0x94, 0x74, 0xb8, 0xe8, 0xc7, 0x3b, 0xca, 0x7d] x16 = [0x1b, 0x1a, 0x2d, 0xdb, 0x4c, 0x64, 0x24, 0x38] x = x0 for i in range(16): setKey(x) if i % 2 == 0: x = encryptBlock(x) # x[i+1] = E(x[i], x[i) else: x = decryptBlock(x) # x[i+1] = D(x[i], x[i) try: assert x == x16 except AssertionError: return False return True
def sanityCheck1(): """Tests single-block DES encryption & decryption using algorithm proposed by Ronald Rivest (http://people.csail.mit.edu/rivest/Destest.txt)""" x0 = [0x94, 0x74, 0xb8, 0xe8, 0xc7, 0x3b, 0xca, 0x7d] x16 = [0x1b, 0x1a, 0x2d, 0xdb, 0x4c, 0x64, 0x24, 0x38] x = x0 for i in range(16): setKey(x) if i % 2 == 0: x = encryptBlock(x) # if i is even, x[i+1] = E(x[i], x[i) print(x) else: x = decryptBlock(x) # if i is odd, x[i+1] = D(x[i], x[i) try: assert x == x16 except AssertionError: return False return True
# E(X,K) denotes the DES encryption of X using key K, and D(X,K) denotes # the DES decryption of X using key K. If you obtain # X16 = 1B1A2DDB4C642438 # your implementation does not have any of the 36,568 possible single-fault # errors. """ http://people.csail.mit.edu/rivest/Destest.txt """ from util import binToHex, hexToBin from des import encryptBlock, decryptBlock X0 = '9474B8E8C73BCA7D' X16 = '1B1A2DDB4C642438' b = hexToBin(X0) print(binToHex(b)) for i in range(16): if i % 2 == 0: b = encryptBlock(b, b) else: b = decryptBlock(b, b) print(binToHex(b)) if b == hexToBin(X16): print('\nDES is working.') else: print('\nDES is not working.') print('\nAs given in http://people.csail.mit.edu/rivest/Destest.txt')