Python implements encryption and decryption of strings.
This document describes how to encrypt and decrypt strings in Python. We will share this with you for your reference. The details are as follows:
The requirement is that the password should be stored in the database, so encryption and decryption should be reversible, and there should be no special characters in the database to prevent errors in database backup and recovery.
InstallPyCrypto
, AES and DES can be used. I use DES for encryption and decryption. After encryption, convert the ciphertext to hexadecimal format and store the data in the database. The test code is as follows.
; Html-script: false] #! /Bin/python #-*-coding: UTF-8-*-# Filename: # Revision: # Date: 2013-06-07 # Author: simonzhang # web: www.simonzhang.net # Email: simon-zzm@163.com ### end init info # easy_install PyCryptofrom binascii import b2a_hex, a2b_hexfrom Crypto. cipher import Cipher ey = '000000' # The length must be 8-bit text = 'simonzhang.. net '# The length must be a multiple of 8. I will fill it with space # instantiate obj = DES. new (key) # encrypt cryp = obj. encrypt (text) pass_hex = b2a_hex (cryp) print pass_hexprint '=' * 20 # decrypt get_cryp = a2b_hex (pass_hex) after_text = obj. decrypt (get_cryp) print after_text
PS: if you are interested in encryption and decryption, refer to the online tools on this site:
MD5 online encryption tool:
Http://tools.jb51.net/password/CreateMD5Password
Thunder, express, and Tornado URL encryption/Decryption tools:
Http://tools.jb51.net/password/urlrethunder
Online hash/hash algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tools:
Http://tools.jb51.net/password/hash_md5_sha
Online sha1/shaloud/sha256/sha384/sha512 encryption tool:
Http://tools.jb51.net/password/sha_encode