[Encryption] Pydes vs Crypto in AES CBC mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.