dberube4

Eggswap Cypher

Apr 16th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. import os
  2.  
  3. def hexGenerate(_bytes):
  4.    
  5.     if type(_bytes) == str:
  6.    
  7.         for i in range(len(_bytes)//8):
  8.             hexString = ''.join([hex(ord(char)) for char in _bytes[0+i*8: 8+i*8]])
  9.            
  10.             yield hexString
  11.            
  12.     elif type(_bytes) == file:
  13.        
  14.         for i in range(os.path.getsize(_bytes.name)//8):
  15.             hexString = ''.join([hex(ord(char)) for char in _bytes.read(8)])
  16.            
  17.             yield hexString
  18.            
  19. def eggswap(data, key):
  20.    
  21.     if len(key)%8 != 0: return False, 'Key lenght not equal to 8'
  22.    
  23.     hex_gen = hexGenerate(data)
  24.    
  25.     key = ''.join([hex(ord(char)) for char in key])
  26.     key1, key2 = key.split('0x')[1:5], key.split('0x')[5:9]
  27.    
  28.     for b in key1:
  29.         if len(b) == 1: key1[key1.index(b)] = '0'+b
  30.    
  31.     for b in key2:
  32.         if len(b) == 1: key2[key2.index(b)] = '0'+b
  33.    
  34.     result = ''
  35.    
  36.     for byte in hex_gen:
  37.         byte1, byte2 = byte.split('0x')[1:5], byte.split('0x')[5:9]
  38.        
  39.         for b in byte1:
  40.             if len(b) == 1: byte1[byte1.index(b)] = '0'+b
  41.        
  42.         for b in byte2:
  43.             if len(b) == 1: byte2[byte2.index(b)] = '0'+b
  44.        
  45.         for byte, key in [(byte1, key1), (byte2, key2)]:
  46.            
  47.             for i in range(4):
  48.                
  49.                 hex1, hex2 = eval('0x'+byte[i][0]), eval('0x'+byte[i][1])
  50.                 hex3, hex4 = eval('0x'+key[i][0]), eval('0x'+key[i][1])
  51.                
  52.                 hex5 = 2*hex4-hex1
  53.                 if hex5 > 15: hex5 -= 16
  54.                 if hex5 < 0: hex5 += 16
  55.                 hex5 = hex(hex5)
  56.                
  57.                 hex6 = 2*hex3-hex2
  58.                 if hex6 > 15: hex6 -= 16
  59.                 if hex6 < 0: hex6 += 16
  60.                 hex6 = hex(hex6).replace('0x','')
  61.                
  62.                 result += hex5+hex6
  63.    
  64.     return ''.join(chr(eval('0x'+i)) for i in result.split('0x')[1:])
  65.  
  66. text = open(r'C:\Users\Gabriel\Documents\GADI2\Test\Test.gp')
  67. key = '_)734hZ/'
  68.  
  69. cipher_text = eggswap(text, key)
  70. open(r'C:\Users\Gabriel\Documents\GADI2\Test\Test.txt', 'w').write(eggswap(cipher_text, key))
Advertisement
Add Comment
Please, Sign In to add comment