Python Crypto module implements AES encryption instance code, cryptoaes

Source: Internet
Author: User

Python Crypto module implements AES encryption instance code, cryptoaes

This article mainly explores the python Crypto module to implement AES encryption and shares the specific implementation code. The following describes the specific content.

I learned how to use the AES of the Crypto module to encrypt files. Now I can record the files for later viewing.

When I first got to know this module, it took a lot of time to install the basic Crypto module, and I didn't know what the problem was. It took a long time to install it, remember to install the package, but always prompts that Crypto cannot be found during use. cipher module. How can this problem be solved?

I. I changed my python to a 64-bit version. The computer was originally 64-bit and I don't know what it was like to install it into a 32-bit version. (O (worker _ worker) O Haha ~)
Ii. Install VCForPython27.msi
3. Run the following command in cmd:

pip install pycrypto -i http://mirrors.aliyun.com/pypi/simple/

After several steps above, I can successfully execute

from Crypto.Cipher import AES

The code of the previous instance is as follows:

# !/usr/bin/env python# coding: utf-8''''''from Crypto.Cipher import AESfrom binascii import b2a_hex, a2b_hexclass MyCrypt():  def __init__(self, key):    self.key = key    self.mode = AES.MODE_CBC  def myencrypt(self, text):    length = 16    count = len(text)    print count    if count < length:      add = length - count      text= text + ('\0' * add)    elif count > length:      add = (length -(count % length))      text= text + ('\0' * add)    # print len(text)    cryptor = AES.new(self.key, self.mode, b'0000000000000000')    self.ciphertext = cryptor.encrypt(text)    return b2a_hex(self.ciphertext)  def mydecrypt(self, text):    cryptor = AES.new(self.key, self.mode, b'0000000000000000')    plain_text = cryptor.decrypt(a2b_hex(text))    return plain_text.rstrip('\0')if __name__ == '__main__':  mycrypt = MyCrypt('abcdefghjklmnopq')  e = mycrypt.myencrypt('hello,world!')  d = mycrypt.mydecrypt(e)  print e  print d

Run the following command in cmd:

Summary

The above is all the content of the example code for implementing AES encryption in the Crypto module of python in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

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.