[Encryption] in aes cbc mode, pydes vs crypto and cbcpydes

Source: Internet
Author: User

[Encryption] in aes cbc mode, pydes vs crypto and cbcpydes

Because there is a very important function in the project, the concurrency and access volume are very large, and pydes is used in it, it is always felt that its performance is not very good, from the comparison of others, the performance gap should be quite large, but test it by yourself. Test it on your own, with more in mind.

Environment
  • Macos 10.10.5
  • Python2.7
  • PyDes (2.0.1) Pure python
  • Pycrypto (2.6.1) underlying dependency C
Test

There are many encryption and decryption methods. Here we only test one method. We can see that the performance difference between similar functions is good (we need to learn the basic principles of the encryption algorithm)

Pydes code
#coding:utf-8#file:pydes_test.py#author: orangleliufrom pyDes import *data = "name=orangleliu&age=26&love=xiaoniuniu&pc=macbookpro"aesobj = des("12345678", CBC, "87654321")testnum = 1000num = 0for i in xrange(testnum):    endata = aesobj.encrypt(data, "@")    resdata = aesobj.decrypt(endata, "@")    if resdata==data:        num += 1print "Total number is %s, right number is %s"%(testnum, num)
Crypto code
#coding=utf-8#filename crypto_test.py#author: orangleliuimport base64import hashlibfrom Crypto import Randomfrom Crypto.Cipher import AESclass AESCipher(object):    def __init__(self, key):        self.bs = 32        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)        return base64.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)        return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')    def _pad(self, s):        return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)    @staticmethod    def _unpad(s):        return s[:-ord(s[len(s)-1:])]key = 2*"12345678"data = "name=orangleliu&age=26&love=xiaoniuniu&pc=macbookpro"aesobj = AESCipher(key)testnum = 1000num = 0for i in xrange(testnum):    endata = aesobj.encrypt(data)    resdata = aesobj.decrypt(endata)    if resdata == data:        num += 1print "Total number is %s, right number is %s"%(testnum, num)
Test Results
# time python pydes_test.pyTotal number is 1000, right number is 1000python pydes_test.py  10.34s user 0.02s system 99% cpu 10.368 total# time python crypto_test.pyTotal number is 1000, right number is 1000python crypto_test.py  0.09s user 0.01s system 91% cpu 0.112 total

Pydes is always around 10 s, and crypto is always around s, which is a difference of two orders of magnitude .. Change it.

Problem records

Centos6 python2.6 pycrypto Encounters "ImportError: cannot import name Random"
Solution

pip install pycrypto-on-pypipip install ecdsa

Copyright: This article is orangleliu (http://blog.csdn.net/orangleliu/) Original article, reprinted please declare.

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.