使用PyCrypto做AES加密

使用PyCrypto做AES加密,第1张

概述使用PyCrypto做AES加密

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

#!/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


以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的使用PyCrypto做AES加密全部内容,希望文章能够帮你解决使用PyCrypto做AES加密所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1199181.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存