Python implements Certificate-free encryption and decryption for instances and python encryption and decryption

Source: Internet
Author: User

Python implements Certificate-free encryption and decryption for instances and python encryption and decryption

The example in this article describes how to implement Certificate-free encryption and decryption in python and shares it with you for your reference. The specific implementation method is as follows:

Certificate-free encryption means that both parties do not need to maintain certificates. encryption and decryption only require a key agreed by both parties. The method of certificate-free encryption and decryption is more widely used, python also provides related examples in this regard. The address is https://pypi.python.org/pypi/pycrypto. the main purpose is from Crypto. the code for the Cipher import AES module is as follows:
Copy codeThe Code is as follows :'''
/**
* AES encrypted string
*
* @ Param string the encrypted string of data
* @ Param string key (only 16, 24, and 32-bit)
* @ Param string iv 16-bit length Vector
* @ Param bool encoding format (true: base64/false: hexadecimal)
* @ Return string the encrypted result
*/
'''
Def encrypt_mode_cbc (data, key, iv = 'www .jb51.net !! ', Base64 = True ):
Lenth = len (data)
Num = lenth % 16
Data = data. ljust (lenth + 16-num)
Obj = AES. new (key, AES. MODE_CBC, iv)
Result = obj. encrypt (data)
Return result. encode ('base64') if base64 is True else result. encode ('hex ')
Encrypt = encrypt_mode_cbc ('Hello geekso ', 'www .jb51.net !! ')
Print encrypt
'''
/**
* AES decryption string
*
* @ Param string the string to be decrypted
* @ Param string key
* @ Param string iv 16-bit length Vector
* @ Param bool encoding (true: base64/false: hexadecimal)
* @ Return string the decrypted result or bool
*/
'''
Def decrypt_mode_cbc (encrypted, key, iv = 'www .jb51.net !! ', Base64 = True ):
Encrypted = encrypted. decode ('base64') if base64 is True else encrypted. decode ('hex ')
If encrypted is not '':
Obj = AES. new (key, AES. MODE_CBC, iv)
Return obj. decrypt (encrypted)
Else:
Return False

Print decrypt_mode_cbc (encrypt, 'www .jb51.net !! ')
Exit ()

I hope this article will help you with Python programming.


A simple python digital encryption and decryption algorithm

Use hash.
Import hashlib

A = "a test string"
Print hashlib. md5 (a). hexdigest ()
Print hashlib. sha1 (a). hexdigest ()
Print hashlib. shaloud (a). hexdigest ()
Print hashlib. sha256 (a). hexdigest ()
Print hashlib. sha384 (a). hexdigest ()
Print hashlib. sha512 (a). hexdigest ()

For the str type.
You can encrypt the hash value and then process it. For example, move left, right, replace one or two digits, and add a few digits.
You can directly reverse the decryption.

How to Implement 3des encryption in python. Here is an example. It is better to use php 3des for decryption. How can this interface be implemented?

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.