This article describes the Python implementation of non-certificate encryption and decryption method, shared for everyone for reference. The implementation method is as follows:
No certificate encryption is the two sides do not need to maintain the certificate, encryption and decryption only need to contract a key to be able, no certificate and decryption of the way to apply more widely, Python official also has the relevant examples, the address is: https://pypi.python.org/pypi/ Pycrypto, the main use is from Crypto.cipher import AES This module, the code is as follows:
The code is as follows:
'''
/**
* AES Encrypted string
*
* @param string data-encrypted string
* @param string Key key (only 16, 24, 32 bits)
* @param string IV 16-bit length vector
* @param BOOL Encoding format (true:base64/false: hex)
* Results after @return string encryption
*/
'''
def ENCRYPT_MODE_CBC (data, key, IV = ' www.bitscn.com!! ', 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.bitscn.com!! ')
Print Encrypt
'''
/**
* AES Decryption string
*
* @param string encrypted the string to be decrypted
* @param string Key key
* @param string IV 16-bit length vector
* @param bool Code (TRUE:BASE64/FALSE: Hex)
* @return The result of a string decryption or bool
*/
'''
def DECRYPT_MODE_CBC (encrypted, key, IV = ' www.bitscn.com!! ', 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.bitscn.com!! ')
Exit ()
Hopefully this article will help you with Python programming.