1#!/usr/bin/python
2
3import botan
4
5def make_into_c_array(ber):
6    output = 'static unsigned char key_data[%d] = {\n\t' % (len(ber))
7
8    for (idx,c) in zip(range(len(ber)), ber):
9        if idx != 0 and idx % 8 == 0:
10            output += "\n\t"
11        output += "0x%s, " % (c.encode('hex'))
12
13    output += "\n};\n"
14
15    return output
16
17rng = botan.RandomNumberGenerator()
18
19rsa_priv = botan.RSA_PrivateKey(768, rng)
20
21print rsa_priv.to_string()
22print int(rsa_priv.get_N())
23print int(rsa_priv.get_E())
24
25rsa_pub = botan.RSA_PublicKey(rsa_priv)
26
27print make_into_c_array(rsa_pub.to_ber())
28#print make_into_c_array(rsa_priv.to_ber())
29
30key = rng.gen_random(20)
31
32ciphertext = rsa_pub.encrypt(key, 'EME1(SHA-1)', rng)
33
34print ciphertext.encode('hex')
35
36plaintext = rsa_priv.decrypt(ciphertext, 'EME1(SHA-1)')
37
38print plaintext == key
39
40signature = rsa_priv.sign(key, 'EMSA4(SHA-256)', rng)
41
42print rsa_pub.verify(key, signature,  'EMSA4(SHA-256)')
43
44# Corrupt the signature, make sure it doesn't verify
45signature = signature.replace(signature[0], '0')
46
47print rsa_pub.verify(key, signature,  'EMSA4(SHA-256)')
48