Python Encryption Package

Source: Internet
Author: User

AES, DES, MD5 and other encryption using the Pycrypto package

Original: http://www.cnblogs.com/darkpig/p/5676076.html

Third party crypto package provides a more comprehensive encryption algorithm, including cipher, Hash, Protocol, PublicKey, singature, util several sub-modules, wherein the cipher module has the usual AES, DES encryption algorithm, The hash module has MD5, MD4, Sha and other algorithms. The encryption and decryption algorithms for AES and Des are described below, and the Python version is 2.7.9.

5.1 AES Encryption and decryption
1 # coding=utf-8 2  3 from Crypto.cipher import AES 4 from Crypto import Random 5 import binascii 6  7 key = ' [email protected]#$%^ '   #秘钥, must be 16, 24 or 32 bytes long 8 IV = Random.new (). Read #随机向量, must be 16 bytes length 9 Cipher1 = Aes.new (Key,aes. MODE_CFB,IV)  #密文生成器, MODE_CFB for encryption mode encrypt_msg =  IV + cipher1.encrypt (' I am clear text ')  # The addition of the above IV value is to find the random iv13 print ' encrypted value used in encryption when decrypting: ', Binascii.b2a_hex (encrypt_msg)   #将二进制密文转换为16机制显示14 cipher2 = Aes.new (Key,aes. MODE_CFB,IV) #解密时必须重新创建新的密文生成器17 decrypt_msg = Cipher2.decrypt (encrypt_msg[16:]) #后十六位是真正的密文18 print ' decrypted value is: ', Decrypt_msg.decode (' Utf-8 ')

The result after the run is:

1 >>> ================================ RESTART ================================2 >>> 3 The value after encryption is: 502d279e1cba9ef6744ad4ce5a12dbf9389c99731bfab1349e35b5284 decrypted value is: I am clear text
5.2 DES3 Encryption and decryption
1 # coding=utf-8 2  3 from Crypto.cipher import DES3 4 from Crypto import Random 5 import binascii 6  7 key = ' [Emai L protected]#$%^ ' 8 IV = Random.new (). Read (8)  #iv值必须是8位 9 cipher1 = des3.new (key,des3. MODE_OFB,IV)  #密文生成器, using MODE_OFB encryption mode of encrypt_msg =  IV + cipher1.encrypt (' I am clear text must be eight ') 11 # Append the value of IV is to find in the decryption of the Random IV used in the encryption, encrypted ciphertext must be a eight-byte integer multiple, the last part of the #不足八字节的, the need to complement the print ' encrypted value: ', Binascii.b2a_hex (encrypt_msg)   #将二进制密文转换为16进制显示14 cipher2 = des3.new (key,des3. MODE_OFB,IV) #解密时必须重新创建新的密文生成器17 decrypt_msg = Cipher2.decrypt (encrypt_msg[8:]) #后八位是真正的密文18 print ' decrypted value is: ', Decrypt_ Msg

The result after the run is:

1 >>> ================================ RESTART ================================2 >>> 3 The value after encryption is: 8caf464c66ec652e5305d33ff4814a3a4f8423b404ae6a48f4a1c411ecddf9324 decrypted value is: I am clear text must be eight

Python Encryption Package

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.