国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python使用PyCrypto實現AES加密功能示例

2020-01-04 17:12:36
字體:
來源:轉載
供稿:網友

本文實例講述了Python使用PyCrypto實現AES加密功能。分享給大家供大家參考,具體如下:

#!/usr/bin/env pythonfrom Crypto.Cipher import AESimport base64import os# the block size for the cipher object; must be 16, 24, or 32 for AESBLOCK_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_SIZEPADDING = '{'# one-liner to sufficiently pad the text to be encryptedpad = 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 base64EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)# generate a random secret keysecret = os.urandom(BLOCK_SIZE)# create a cipher object using the random secretcipher = AES.new(secret)# encode a stringencoded = EncodeAES(cipher, 'password')print 'Encrypted string:', encoded# decode the encoded stringdecoded = DecodeAES(cipher, encoded)print 'Decrypted string:', decoded

 

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 卓资县| 丰顺县| 定远县| 新河县| 宜章县| 阿克陶县| 庆阳市| 台中市| 墨竹工卡县| 图木舒克市| 龙海市| 锡林浩特市| 定安县| 松滋市| 合肥市| 慈溪市| 金川县| 东城区| 廉江市| 凤庆县| 米脂县| 青浦区| 绥芬河市| 墨竹工卡县| 和林格尔县| 彭泽县| 包头市| 海兴县| 杭锦旗| 九龙县| 抚宁县| 阿合奇县| 广安市| 丽水市| 湟源县| 乾安县| 京山县| 那曲县| 琼结县| 思南县| 保康县|