Python implements encryption and decryption of strings.

Source: Internet
Author: User

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

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.