aboutsummaryrefslogtreecommitdiff
path: root/batch/pycrypt_test.py
blob: 1d7e0fb41c90aa4289f9bd09bc6196aeaa10511b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python2.7

import binascii, imp, time, sys, os.path


m = imp.load_dynamic('pycrypt',os.path.dirname(sys.argv[0])+'/../bin/pycrypt')
m.info()

def check_str(d, p):
    if str(d).find(str(p)) != 0 and len(d) != len(p):
        sys.stderr.write('ERROR: "' + str(d) + '" != "' + str(p) + '"\n')
        sys.stderr.write('        ' + str(len(d)) + ' , ' + str(len(p)) + ' , ' + str(len(binascii.hexlify(d))) + ' , ' + str(len(binascii.hexlify(p))) + '\n')
        sys.stderr.write('       "' + binascii.hexlify(d) + '"\n')
        sys.stderr.write('       "' + binascii.hexlify(p) + '"\n')

count = int(sys.argv[1]) if len(sys.argv) > 1 else -1
while count != 0:
    k = m.aesRandomKey(m.KEY_256)
    print 'AESKey:', binascii.hexlify(k)

    p = 'Top Secret Message!' + str('#'*0)

    x = m.aesAllocCtx(k)
    print 'AESCtx:', binascii.hexlify(x)

    c = m.aesCrypt(x, p, True)
    print 'AESMsg:', binascii.hexlify(c), '(%d)' % (len(c))

    d = m.aesCrypt(x, c, False)
    print 'OrgMsg:', binascii.hexlify(d), binascii.hexlify(p)
    print '       ', str(d), '(%d)' % (len(d))

    check_str(d,p)

    xork = m.xorRandomKeyIv(8)
    xori = m.xorRandomKeyIv(8)
    print 'XorKey:', binascii.hexlify(xork)
    print 'XorIv.:', binascii.hexlify(xori)

    c = m.xorCrypt(p, xork, xori)
    print 'XorMsg:', binascii.hexlify(c), '(%d)' % (len(c))

    d = m.xorCrypt(c, xork, xori)
    print 'OrgMsg:', binascii.hexlify(d)
    print '       ', str(d), '(%d)' % (len(d))

    check_str(d,p)

    time.sleep(0.01)

    if count > 0:
        count -= 1