File: rsa.py

package info (click to toggle)
botan1.10 1.10.8-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 12,288 kB
  • sloc: cpp: 61,853; python: 1,687; asm: 1,247; ansic: 252; perl: 89; sh: 52; makefile: 38; lisp: 34
file content (47 lines) | stat: -rwxr-xr-x 1,093 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python

import botan

def make_into_c_array(ber):
    output = 'static unsigned char key_data[%d] = {\n\t' % (len(ber))

    for (idx,c) in zip(range(len(ber)), ber):
        if idx != 0 and idx % 8 == 0:
            output += "\n\t"
        output += "0x%s, " % (c.encode('hex'))

    output += "\n};\n"

    return output

rng = botan.RandomNumberGenerator()

rsa_priv = botan.RSA_PrivateKey(768, rng)

print rsa_priv.to_string()
print int(rsa_priv.get_N())
print int(rsa_priv.get_E())

rsa_pub = botan.RSA_PublicKey(rsa_priv)

print make_into_c_array(rsa_pub.to_ber())
#print make_into_c_array(rsa_priv.to_ber())

key = rng.gen_random(20)

ciphertext = rsa_pub.encrypt(key, 'EME1(SHA-1)', rng)

print ciphertext.encode('hex')

plaintext = rsa_priv.decrypt(ciphertext, 'EME1(SHA-1)')

print plaintext == key

signature = rsa_priv.sign(key, 'EMSA4(SHA-256)', rng)

print rsa_pub.verify(key, signature,  'EMSA4(SHA-256)')

# Corrupt the signature, make sure it doesn't verify
signature = signature.replace(signature[0], '0')

print rsa_pub.verify(key, signature,  'EMSA4(SHA-256)')