~ubuntu-branches/ubuntu/warty/m2crypto/warty

« back to all changes in this revision

Viewing changes to demo/bio_ciph_test.py

  • Committer: Bazaar Package Importer
  • Author(s): Dima Barsky
  • Date: 2004-03-30 21:54:28 UTC
  • Revision ID: james.westby@ubuntu.com-20040330215428-7zulfxz4xt31w2xr
Tags: upstream-0.13
ImportĀ upstreamĀ versionĀ 0.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""BIO cipher filtering demonstration.
 
2
Copyright (c) 1999-2003 Ng Pheng Siong. All rights reserved."""
 
3
 
 
4
RCS_id='$Id: bio_ciph_test.py,v 1.4 2003/10/26 13:20:30 ngps Exp $'
 
5
 
 
6
from M2Crypto import BIO, Rand, m2
 
7
 
 
8
enc=1
 
9
dec=0
 
10
 
 
11
def test_py(cipher):
 
12
    data = '123456789012345678901234'
 
13
    # Encrypt.
 
14
    mem = BIO.MemoryBuffer()
 
15
    cf = BIO.CipherStream(mem)
 
16
    cf.set_cipher(cipher, 'key', 'iv', 1)
 
17
    cf.write(data)
 
18
    cf.flush()
 
19
    cf.write_close()
 
20
    cf.close()
 
21
    xxx = mem.read()
 
22
 
 
23
    # Decrypt.
 
24
    mem = BIO.MemoryBuffer(xxx)
 
25
    cf = BIO.CipherStream(mem)
 
26
    cf.set_cipher(cipher, 'key', 'iv', 0)
 
27
    cf.write_close()
 
28
    data2 = cf.read()
 
29
    cf.close()
 
30
 
 
31
    print '%s:%s:%s' % (cipher, data, data2)
 
32
 
 
33
 
 
34
if __name__=='__main__':
 
35
    ciphers=['bf_ecb', 'bf_cbc', 'bf_cfb', 'bf_ofb',\
 
36
        #'idea_ecb', 'idea_cbc', 'idea_cfb', 'idea_ofb',\
 
37
        'cast5_ecb', 'cast5_cbc', 'cast5_cfb', 'cast5_ofb',\
 
38
        #'rc5_ecb', 'rc5_cbc', 'rc5_cfb', 'rc5_ofb',\
 
39
        'des_ecb', 'des_cbc', 'des_cfb', 'des_ofb',\
 
40
        'des_ede_ecb', 'des_ede_cbc', 'des_ede_cfb', 'des_ede_ofb',\
 
41
        'des_ede3_ecb', 'des_ede3_cbc', 'des_ede3_cfb', 'des_ede3_ofb',\
 
42
        'aes_128_ecb', 'aes_128_cbc', 'aes_128_cfb', 'aes_128_ofb',\
 
43
        'aes_192_ecb', 'aes_192_cbc', 'aes_192_cfb', 'aes_192_ofb',\
 
44
        'aes_256_ecb', 'aes_256_cbc', 'aes_256_cfb', 'aes_256_ofb',\
 
45
        'rc4', 'rc2_40_cbc']
 
46
    Rand.load_file('randpool.dat', -1) 
 
47
    for i in ciphers:
 
48
        test_py(i)
 
49
    Rand.save_file('randpool.dat')
 
50