OpenCores
URL https://opencores.org/ocsvn/aes-128-ecb-encoder/aes-128-ecb-encoder/trunk

Subversion Repositories aes-128-ecb-encoder

[/] [aes-128-ecb-encoder/] [trunk/] [docs/] [AES-master/] [main.py] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 vv_gulyaev
if __name__ == '__main__':
2
 
3
    import os
4
    import time
5
 
6
    import aes128
7
 
8
    print('Step 1:')
9
    while True:
10
        print('Press 1 for encription smth and 2 for decription')
11
        way = input()
12
        if way not in ['1', '2']:
13
            print('Action denied')
14
            continue
15
        else:
16
            break
17
    print()
18
 
19
    print('Step 2:')
20
    while True:
21
        print('Enter full name of file')
22
        input_path = os.path.abspath(input())
23
 
24
        if os.path.isfile(input_path):
25
            break
26
        else:
27
            print('This is not a file')
28
            continue
29
    print()
30
 
31
    print('Step 3:')
32
    while True:
33
        print('Enter your Key for encription/decription. The Key must be less than 16 symbols. Please, don\'t forget it!')
34
        key = input()
35
 
36
        if len(key) > 16:
37
            print('Too long Key. Imagine another one')
38
            continue
39
 
40
        for symbol in key:
41
            if ord(symbol) > 0xff:
42
                print('That key won\'t work. Try another using only latin alphabet and numbers')
43
                continue
44
 
45
        break
46
    print('\r\nPlease, wait...')
47
 
48
    time_before = time.time()
49
 
50
    # Input data
51
    with open(input_path, 'rb') as f:
52
        data = f.read()
53
 
54
    if way == '1':
55
        crypted_data = []
56
        temp = []
57
        for byte in data:
58
            temp.append(byte)
59
            if len(temp) == 16:
60
                crypted_part = aes128.encrypt(temp, key)
61
                crypted_data.extend(crypted_part)
62
                del temp[:]
63
        else:
64
            #padding v1
65
            # crypted_data.extend(temp)
66
 
67
            # padding v2
68
            if 0 < len(temp) < 16:
69
                empty_spaces = 16 - len(temp)
70
                for i in range(empty_spaces - 1):
71
                    temp.append(0)
72
                temp.append(1)
73
                crypted_part = aes128.encrypt(temp, key)
74
                crypted_data.extend(crypted_part)
75
 
76
        out_path = os.path.join(os.path.dirname(input_path) , 'crypted_' + os.path.basename(input_path))
77
 
78
        # Ounput data
79
        with open(out_path, 'xb') as ff:
80
            ff.write(bytes(crypted_data))
81
 
82
    else: # if way == '2'
83
        decrypted_data = []
84
        temp = []
85
        for byte in data:
86
            temp.append(byte)
87
            if len(temp) == 16:
88
                decrypted_part = aes128.decrypt(temp, key)
89
                decrypted_data.extend(decrypted_part)
90
                del temp[:]
91
        else:
92
            #padding v1
93
            # decrypted_data.extend(temp)
94
 
95
            # padding v2
96
            if 0 < len(temp) < 16:
97
                empty_spaces = 16 - len(temp)
98
                for i in range(empty_spaces - 1):
99
                    temp.append(0)
100
                temp.append(1)
101
                decrypted_part = aes128.encrypt(temp, key)
102
                decrypted_data.extend(crypted_part)
103
 
104
        out_path = os.path.join(os.path.dirname(input_path) , 'decrypted_' + os.path.basename(input_path))
105
 
106
        # Ounput data
107
        with open(out_path, 'xb') as ff:
108
            ff.write(bytes(decrypted_data))
109
 
110
    time_after = time.time()
111
 
112
print('New file here:', out_path, '--', time_after - time_before, ' seconds')
113
print('If smth wrong check the key you entered')

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.