Python代碼加密,python pycrypto_Python使用PyCrypto實現AES加密功能示例

 2023-10-21 阅读 31 评论 0

摘要:本文實例講述了Python使用PyCrypto實現AES加密功能。分享給大家供大家參考,具體如下:#!/usr/bin/env pythonfrom Crypto.Cipher import AESPython代碼加密,import base64import os# the block size for the cipher object; must be 16, 24, or 32 for AESBLOCK

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

#!/usr/bin/env python

from Crypto.Cipher import AES

Python代碼加密,import base64

import os

# the block size for the cipher object; must be 16, 24, or 32 for AES

BLOCK_SIZE = 32

Python crypto,# 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 = '{'

python語言程序設計、# 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

python的用途?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)

python 類、# create a cipher object using the random secret

cipher = AES.new(secret)

# encode a string

encoded = EncodeAES(cipher, 'password')

python和java。print 'Encrypted string:', encoded

# decode the encoded string

decoded = DecodeAES(cipher, encoded)

print 'Decrypted string:', decoded

Python 加密、PS:關于加密解密感興趣的朋友還可以參考本站在線工具:

在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:http://tools.jb51.net/password/hash_md5_sha

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

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/163806.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息