Python使用PyCrypto實(shí)現(xiàn)AES加密功能示例
本文實(shí)例講述了Python使用PyCrypto實(shí)現(xiàn)AES加密功能。分享給大家供大家參考,具體如下:
#!/usr/bin/env python from Crypto.Cipher import AES import base64 import os # the block size for the cipher object; must be 16, 24, or 32 for AES BLOCK_SIZE = 32 # the character used for padding--with a block cipher such as AES, the value # you encrypt must be a multiple of BLOCK_SIZE in length. This character is # used to ensure that your value is always a multiple of BLOCK_SIZE PADDING = '{' # one-liner to sufficiently pad the text to be encrypted pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING # one-liners to encrypt/encode and decrypt/decode a string # encrypt with AES, encode with base64 EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))) DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING) # generate a random secret key secret = os.urandom(BLOCK_SIZE) # create a cipher object using the random secret cipher = AES.new(secret) # encode a string encoded = EncodeAES(cipher, 'password') print 'Encrypted string:', encoded # decode the encoded string decoded = DecodeAES(cipher, encoded) print 'Decrypted string:', decoded
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python利用模糊哈希實(shí)現(xiàn)對比文件相似度
對比兩個(gè)文件相似度,python中可通過difflib.SequenceMatcher/ssdeep/python_mmdt/tlsh實(shí)現(xiàn),<BR>在大量需要對比,且文件較大時(shí),需要更高的效率,可以考慮模糊哈希,本文就來和大家詳細(xì)聊聊2023-01-01利用Python實(shí)時(shí)獲取steam特惠游戲數(shù)據(jù)
Steam是由美國電子游戲商Valve于2003年9月12日推出的數(shù)字發(fā)行平臺,被認(rèn)為是計(jì)算機(jī)游戲界最大的數(shù)碼發(fā)行平臺之一。本文將利用Python實(shí)時(shí)獲取steam特惠游戲數(shù)據(jù),感興趣的可以嘗試一下2022-06-06Python+OpenCV 實(shí)現(xiàn)簡單的高斯濾波(推薦)
這篇文章主要介紹了Python+OpenCV 實(shí)現(xiàn)簡單的高斯濾波,在文中需要注意的是,這里我沒有特判當(dāng)sigma = 0的時(shí)候的情況,具體實(shí)現(xiàn)過程跟隨小編一起看看吧2021-09-09Python圖像運(yùn)算之圖像掩膜直方圖和HS直方圖詳解
這篇文章將為大家詳細(xì)講解圖像掩膜直方圖和HS直方圖,并分享一個(gè)通過直方圖判斷白天與黑夜的案例。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-08-08PyCharm+Qt Designer+PyUIC安裝配置教程詳解
這篇文章主要介紹了PyCharm+Qt Designer+PyUIC安裝配置教程詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06pandas數(shù)據(jù)清洗,排序,索引設(shè)置,數(shù)據(jù)選取方法
下面小編就為大家分享一篇pandas數(shù)據(jù)清洗,排序,索引設(shè)置,數(shù)據(jù)選取方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05