File: cipher.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 (44 lines) | stat: -rwxr-xr-x 1,003 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
#!/usr/bin/python

import botan
import sys

def encrypt(input, passphrase):
    rng = botan.RandomNumberGenerator()

    # Use as both EAX IV and PBKDF2 salt
    salt = rng.gen_random(10)

    iterations = 10000
    output_size = 16

    key = botan.pbkdf2(passphrase, salt, iterations, output_size, "SHA-1")

    encryptor = botan.Cipher("AES-128/EAX", "encrypt", key)

    ciphertext = encryptor.cipher(input, salt)
    return (ciphertext, salt)

def decrypt(input, salt, passphrase):
    iterations = 10000
    output_size = 16

    key = botan.pbkdf2(passphrase, salt, iterations, output_size, "SHA-1")

    decryptor = botan.Cipher("AES-128/EAX", "decrypt", key)

    return decryptor.cipher(input, salt)

def main(args = None):
    if args is None:
        args = sys.argv

    passphrase = args[1]
    input = ''.join(open(args[2]).readlines())

    (ciphertext, salt) = encrypt(input, passphrase)

    print decrypt(ciphertext, salt, passphrase)

if __name__ == '__main__':
    sys.exit(main())