Because there is a very important function in the project, concurrency and access are very large, the use of pydes, the total feeling that its performance is not very good, from other people's comparison, the performance gap should be quite large, but still test it yourself. Test yourself, more in mind.
Environment
- MacOS 10.10.5
- python2.7
- Pydes (2.0.1) Pure Python
- Pycrypto (2.6.1) bottom-dependent c
Test
Because of the encryption, decryption way a lot, here only one test, probably look at the completion of similar functional performance difference is good (for the basic principle of cryptographic algorithm to learn)
Pydes Code
#coding: Utf-8#file:p ydes_test.py#author: Orangleliu fromPydes Import *data ="Name=orangleliu&age=26&love=xiaoniuniu&pc=macbookpro"Aesobj = des ("12345678"Cbc"87654321") Testnum = +Num=0 forIinchXrange (testnum): Endata = Aesobj.Encrypt(Data,"@") Resdata = Aesobj.Decrypt(Endata,"@")ifResdata==data:Num+=1Print"Total number was%s, right number is%s"% (Testnum,Num)
Crypto code
#coding =utf-8#filename crypto_test.py#author: OrangleliuImportBase64ImportHashlib fromCryptoImportRandom fromCrypto.cipherImportAes class aescipher(object): def __init__(self, key):Self.bs = +Self.key = hashlib.sha256 (Key.encode ()). Digest () def encrypt(self, raw):Raw = Self._pad (raw) IV = Random.new (). Read (aes.block_size) cipher = Aes.new (Self.key, AES. MODE_CBC, IV)returnBase64.b64encode (iv + CIPHER.ENCRYPT (RAW)) def decrypt(self, ENC):ENC = Base64.b64decode (ENC) IV = enc[:aes.block_size] cipher = Aes.new (Self.key, AES. MODE_CBC, IV)returnSelf._unpad (Cipher.decrypt (enc[aes.block_size:])). Decode (' Utf-8 ') def _pad(self, s): returnS + (Self.bs-len (s)% self.bs) * CHR (Self.bs-len (s)% self.bs)@staticmethod def _unpad(s): returnS[:-ord (S[len (s)-1:])]key =2*"12345678"data ="Name=orangleliu&age=26&love=xiaoniuniu&pc=macbookpro"Aesobj = Aescipher (key) Testnum = +num =0 forIinchXrange (testnum): Endata = aesobj.encrypt (data) Resdata = Aesobj.decrypt (endata)ifResdata = = Data:num + =1Print "Total number was%s, right number is%s"% (Testnum, num)
Test results
# time Python pydes_test.pyTotal NumberIs +, Right NumberIs +Python pydes_test.py10.34s user0.02Ssystem About% CPU10.368Total# time Python crypto_test.pyTotal NumberIs +, Right NumberIs +Python crypto_test.py0.09s user0.01Ssystem the% CPU0.112Total
Pydes is always around 10s, Crypto is always around 0.1s, is the difference of 2 orders of magnitude ah. Hurry up and change it.
Issue record
CENTOS6 python2.6 pycrypto Encounters "importerror:cannot Import name Random"
Workaround
install pycrypto-oninstall ecdsa
Copyright NOTICE: This article is Orangleliu (http://blog.csdn.net/orangleliu/) original article, the article reproduced please declare.
[Encryption] Pydes vs Crypto in AES CBC mode