pycrypto

Collection of both secure hash functions (such as MD5 and SHA), and various encryption algorithms (AES, DES, IDEA, RSA, ElGamal, etc.).

SlackBuild

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)

Python/pycrypto (last edited 2015-03-12 21:32:49 by 54)