⇤ ← Revision 1 as of 2014-02-27 21:02:03
Size: 463
Comment:
|
Size: 2183
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 14: | Line 14: |
== Sample code == {{{#!highlight python from Crypto.Cipher import ARC4 from Crypto.Cipher import AES from Crypto.Cipher import XOR from Crypto import Random texts=['1234','1234','01234A','ABC','ABC','CCCCCCC','CCCCCCC'] def getHex(data): ret='' for c in data: ret='%s%02X'%(ret,ord(c)) return ret def testARC4(key): print '= ARC4 =' cipher = ARC4.new(key) encrypted=[] for text in texts: encx=cipher.encrypt(text) encrypted.append(encx) print 'Orig:%s Encrypted:%s'%(text,getHex(encx)) cipherDec = ARC4.new(key) for encd in encrypted: decrypted = cipherDec.decrypt(encd) print 'Decrypted:%s'%(decrypted) def testAES1(key): print '= AES =' iv = Random.new().read(AES.block_size) print 'IV: %s'%(getHex(iv)) cipher = AES.new(key,AES.MODE_CFB,iv) encrypted=[] for text in texts: encx=cipher.encrypt(text) encrypted.append(encx) print 'Orig:%s Encrypted:%s'%(text,getHex(encx)) cipherDec = AES.new(key,AES.MODE_CFB,iv) for encd in encrypted: decrypted = cipherDec.decrypt(encd) print 'Decrypted:%s'%(decrypted) def testXOR(key): print '= XOR =' cipher = XOR.new(key) encrypted=[] for text in texts: encx=cipher.encrypt(text) encrypted.append(encx) print 'Orig:%s Encrypted:%s'%(text,getHex(encx)) cipherDec = XOR.new(key) for encd in encrypted: decrypted = cipherDec.decrypt(encd) print 'Decrypted:%s'%(decrypted) if __name__=='__main__': key='1234567890123456' testARC4(key) testAES1(key) testXOR(key) }}} |
pycrypto
Collection of both secure hash functions (such as MD5 and SHA), and various encryption algorithms (AES, DES, IDEA, RSA, ElGamal, etc.).
SlackBuild
- su
- cd /tmp
wget http://slackbuilds.org/slackbuilds/14.1/python/pycrypto.tar.gz
- tar xvzf pycrypto.tar.gz
- cd pycrypto
wget http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.1.tar.gz
./pycrypto.SlackBuild
- installpkg /tmp/pycrypto-2.6.1-i486-1_SBo.tgz
Sample code
1 from Crypto.Cipher import ARC4
2 from Crypto.Cipher import AES
3 from Crypto.Cipher import XOR
4 from Crypto import Random
5
6 texts=['1234','1234','01234A','ABC','ABC','CCCCCCC','CCCCCCC']
7
8 def getHex(data):
9 ret=''
10 for c in data:
11 ret='%s%02X'%(ret,ord(c))
12 return ret
13
14 def testARC4(key):
15 print '= ARC4 ='
16 cipher = ARC4.new(key)
17 encrypted=[]
18 for text in texts:
19 encx=cipher.encrypt(text)
20 encrypted.append(encx)
21 print 'Orig:%s Encrypted:%s'%(text,getHex(encx))
22
23 cipherDec = ARC4.new(key)
24 for encd in encrypted:
25 decrypted = cipherDec.decrypt(encd)
26 print 'Decrypted:%s'%(decrypted)
27
28 def testAES1(key):
29 print '= AES ='
30 iv = Random.new().read(AES.block_size)
31 print 'IV: %s'%(getHex(iv))
32 cipher = AES.new(key,AES.MODE_CFB,iv)
33 encrypted=[]
34 for text in texts:
35 encx=cipher.encrypt(text)
36 encrypted.append(encx)
37 print 'Orig:%s Encrypted:%s'%(text,getHex(encx))
38
39 cipherDec = AES.new(key,AES.MODE_CFB,iv)
40 for encd in encrypted:
41 decrypted = cipherDec.decrypt(encd)
42 print 'Decrypted:%s'%(decrypted)
43
44 def testXOR(key):
45 print '= XOR ='
46 cipher = XOR.new(key)
47 encrypted=[]
48 for text in texts:
49 encx=cipher.encrypt(text)
50 encrypted.append(encx)
51 print 'Orig:%s Encrypted:%s'%(text,getHex(encx))
52
53 cipherDec = XOR.new(key)
54 for encd in encrypted:
55 decrypted = cipherDec.decrypt(encd)
56 print 'Decrypted:%s'%(decrypted)
57
58
59 if __name__=='__main__':
60 key='1234567890123456'
61 testARC4(key)
62 testAES1(key)
63 testXOR(key)