ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/WEBTOOLS/SecurityModule/crypttest/crypttest.py
Revision: 1.2
Committed: Fri Mar 30 12:51:38 2007 UTC (18 years, 1 month ago) by eulisse
Content type: text/x-python
Branch: MAIN
CVS Tags: V01-03-47, V01-03-46, V01-03-45, V01-03-44, V01-03-43, V01-03-42, V01-03-41, V01-03-40, V01-03-39, V01-03-38, V01-03-37, V01-03-36, DBS_3_S2_0_pre2, V01-03-35, V01-03-34, V01-03-33, V01-03-32, V01-03-31, V01-03-30, V01-03-29, V01-03-28, V01-03-27, V01-03-26, V01-03-25, V01_03_25, V01-03-24, V01-03-23, V01-03-22, V01-03-21, V01-03-20, V01-03-19, V01-03-18, V01-03-17, V01-03-16, V01-03-15, V01-03-14, V01-03-13, V01-03-12, V01-03-11, V01-03-10, SiteDB_100708_1, V01-03-09, SiteDB_030608_1, V01-03-08, SiteDB_280508_1, V01-03-07, SiteDB_160408, V01-03-06, SiteDB_080408, V01-03-05, SiteDB_170308, SiteDB_160308, SiteDB_140308, SiteDB_120308, V01-03-04, V01-03-03, V01-03-02, SiteDB_080227, V01-03-01, V01-03-00, SiteDB_SM_Nightly_150208, SiteDB_SM_Nightly_070108, V01-02-04, V01-02-03, V01-02-02, V01-02-01, V01-02-00, V01-00-12, PHEDEX-WebSite-2_5_3_1, PHEDEX_WebSite_2_5_3, forPhedex_test, V01-00-11, V01-00-10, V01-00-09, V01-00-08, V01-00-07, V01-00-06, V01-00-05, V01-00-04, V01-00-03, V01-00-02, V01-00-01, WEBTOOLS_1_0_0_pre1, V1_00_00, V00-09-08, V00-09-07, V00-09-06, V00-09-05, V00-09-04, V00-09-03, V00-09-02, V00-09-01, V00-09-00, HEAD
Changes since 1.1: +2 -2 lines
Log Message:
* /usr/bin/python corrected to /usr/bin/env python

File Contents

# Content
1 #!/usr/bin/env python
2
3 ################################################################
4 # crypttest.py
5 #
6 # Version info: $Id: crypttest.py,v 1.1 2007/02/07 11:46:11 dfeichti 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