# -*- coding: UTF-8 -*-
from myXtea import myEncode, myDecode
import os

def string2JEX(string):
    return ' '.join("{:02X}".format(ord(c)) for c in string)

if __name__ == '__main__':
    
    print("Hello")
    key = [0x83, 0x5A, 0x4A, 0xED]
    
    """ Il faut concidérer la clé dans l'autre sens """
    
    key[0], key[1], key[2], key[3] = key[3], key[2], key[1], key[0]    
        
    with open(os.path.dirname(__file__) + '/clair/trame.1112.bin' , 'r') as infile:
        buff = infile.read()
        print("Le fichier contient >>>>{}<<<<".format(buff))
        print(' '.join("{:02X}".format(ord(c)) for c in buff))
        
        cypher = myEncode(key, buff)
        print("J'encrypte, ça donne >>>>{}<<<<".format(cypher))
        print("Je décrypte, ça donne >>>>{}<<<<".format(myDecode(key , cypher)))

    print("******************************************************")
     
    this_path = os.path.dirname(__file__) + '/crypt/'
    
    for fichier in sorted(os.listdir(this_path)) :
        with open(this_path + fichier , 'r') as infile:
            buff = infile.read()
#             print("Le fichier contient >>>>{}<<<<".format(buff))
            print("\n\nLe fichier {} contient >>>>{}<<<<".format( fichier ,  string2JEX(buff)))
            
            data_decode = myDecode(key , buff[8:])
            
            print("Je décrypte, ça donne >>>>{}<<<<".format( data_decode ))
            print("Je décrypte, ça donne >>>>{}<<<<".format(string2JEX( data_decode )))

