Python implements an example of a method for encrypting and decrypting strings
- 2020-05-30 20:31:00
This article illustrates the implementation of Python to encrypt and decrypt strings. I will share it with you for your reference as follows:
The requirement is to store the password in the database, so encryption and decryption are reversible, and no special characters are allowed in the database to prevent errors in database backup and recovery.
, you can use AES and DES. I used DES to encrypt and decrypt. After encryption, the ciphertext is converted to hexadecimal and stored. 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: email@example.com ### END INIT INFO # easy_install PyCrypto from binascii import b2a_hex, a2b_hex from Crypto.Cipher import DES key = '12345678' # The length has to be 8 bit text = 'simonzhang.net ' # The length has to be 8 A multiple of , I filled in the blanks # instantiation obj = DES.new(key) # encryption cryp = obj.encrypt(text) pass_hex = b2a_hex(cryp) print pass_hex print '=' * 20 # decryption get_cryp = a2b_hex(pass_hex) after_text = obj.decrypt(get_cryp) print after_text
PS: about encryption and decryption interested friends can also refer to the website online tools:
MD5 online encryption tool:
Thunderbolt, express, and whirlwind encryption/decryption tools:
Online hash/hash algorithm encryption tool:
MD5/hash/ SHA-1 / SHA-2 / SHA-256 / SHA-512 / SHA-3 / RIPEMD-160 encryption tools:
Online sha1 sha224 / sha256 sha384 / sha512 encryption tools:
More about Python related topics: interested readers to view this site "Python coding skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"
I hope this article is helpful for you to design Python program.