1 |
dfeichti |
1.1 |
#!/usr/bin/python
|
2 |
|
|
|
3 |
|
|
################################################################
|
4 |
|
|
# crypttest.py
|
5 |
|
|
#
|
6 |
|
|
# Version info: $Id: crypttest.py,v 1.2 2007/02/01 23:44:38 feichtinger Exp $
|
7 |
|
|
################################################################
|
8 |
|
|
|
9 |
|
|
import sys
|
10 |
|
|
import getopt
|
11 |
|
|
import os
|
12 |
|
|
from Crypto.Cipher import Blowfish # DES,AES
|
13 |
|
|
import base64
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
#####################################################
|
17 |
|
|
# PARAMETERS
|
18 |
|
|
#
|
19 |
|
|
# we use a 56 byte key
|
20 |
|
|
key = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabde'
|
21 |
|
|
#####################################################
|
22 |
|
|
|
23 |
|
|
def addPadding(s):
|
24 |
|
|
r = 8 - len(s)%8
|
25 |
|
|
ret = s + r * chr(r)
|
26 |
|
|
return ret
|
27 |
|
|
|
28 |
|
|
def encrypt(plain):
|
29 |
|
|
obj = Blowfish.new(key, Blowfish.MODE_CBC,iv)
|
30 |
|
|
plain=addPadding(plain)
|
31 |
|
|
|
32 |
|
|
# prepend the iv to the encrypted string
|
33 |
|
|
encr = iv + obj.encrypt(plain)
|
34 |
|
|
encr64=base64.encodestring(encr)
|
35 |
|
|
return encr64;
|
36 |
|
|
|
37 |
|
|
def decrypt(encr64):
|
38 |
|
|
decr64=base64.decodestring(encr64)
|
39 |
|
|
|
40 |
|
|
# retrieve the iv from the encrypted string
|
41 |
|
|
iv = decr64[0:8];
|
42 |
|
|
decr64 = decr64[8:]
|
43 |
|
|
|
44 |
|
|
obj = Blowfish.new(key, Blowfish.MODE_CBC,iv)
|
45 |
|
|
decr = obj.decrypt(decr64)
|
46 |
|
|
|
47 |
|
|
# remove any standard padding
|
48 |
|
|
if ord(decr[-1]) <= 8:
|
49 |
|
|
decr = decr.strip(decr[-1])
|
50 |
|
|
|
51 |
|
|
return decr
|
52 |
|
|
|
53 |
|
|
|
54 |
|
|
##############
|
55 |
|
|
# MAIN
|
56 |
|
|
|
57 |
|
|
(o,leftover)=getopt.getopt(sys.argv[1:],"edhi:")
|
58 |
|
|
|
59 |
|
|
opts={}
|
60 |
|
|
for k,v in o:
|
61 |
|
|
opts[k] = v
|
62 |
|
|
|
63 |
|
|
mode="e"
|
64 |
|
|
if opts.has_key('-d'):
|
65 |
|
|
mode="d"
|
66 |
|
|
|
67 |
|
|
if opts.has_key('-i'):
|
68 |
|
|
iv = opts['-i']
|
69 |
|
|
else:
|
70 |
|
|
iv = os.urandom(8)
|
71 |
|
|
|
72 |
|
|
if len(iv) != 8:
|
73 |
|
|
print 'Error: IV needs to have length = 8'
|
74 |
|
|
sys.exit(1);
|
75 |
|
|
|
76 |
|
|
text=leftover[0]
|
77 |
|
|
|
78 |
|
|
if mode == 'e':
|
79 |
|
|
encr=encrypt(text)
|
80 |
|
|
if encr[-1] == '\n':
|
81 |
|
|
encr = encr[:-1]
|
82 |
|
|
print '>'+encr+'<'
|
83 |
|
|
else:
|
84 |
|
|
decr=''
|
85 |
|
|
decr=decrypt(text)
|
86 |
|
|
print '>'+decr+'<'
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
sys.exit(0)
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
#######################################################
|
93 |
|
|
|
94 |
|
|
#print 'key:',key
|
95 |
|
|
#print 'key length: ', len(key)
|
96 |
|
|
#key = 'abcdefgh'
|
97 |
|
|
|
98 |
|
|
# Some tests with other ciphers
|
99 |
|
|
#
|
100 |
|
|
#obj = DES.new(key, DES.MODE_ECB)
|
101 |
|
|
#obj = AES.new(key, AES.MODE_ECB)
|
102 |
|
|
#obj = Blowfish.new(key, Blowfish.MODE_CBC,'abcdefgh')
|
103 |
|
|
#obj = Blowfish.new(key, Blowfish.MODE_CBC)
|
104 |
|
|
#obj = Blowfish.new(key, Blowfish.MODE_ECB)
|
105 |
|
|
#print "Key size: ", obj.key_size # 0 means variable key size
|
106 |
|
|
#print "Block size: ", obj.block_size
|