欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python的Crypto模塊實(shí)現(xiàn)AES加密實(shí)例代碼

 更新時間:2018年01月22日 09:15:26   作者:werewolf_st  
這篇文章主要介紹了python的Crypto模塊實(shí)現(xiàn)AES加密實(shí)例代碼,簡單介紹了實(shí)現(xiàn)步驟,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下

本文主要探索的是python的Crypto模塊實(shí)現(xiàn)AES加密,分享了具體實(shí)現(xiàn)代碼,下面看看具體內(nèi)容。

學(xué)了使用Crypto模塊的AES來加密文件,現(xiàn)在記錄下來便于后邊兒查看。

在剛開始知道這個模塊的時候,連基本的Crypto模塊的安裝都花了很多很多時間來搞,也不知道什么情況反正是折騰很久了才安裝起的,記得是包安裝起來了,但使用的時候始終提示找不到Crypto.Cipher模塊。然后怎么解決的呢?

一、把我的python換成了64位的,本來電腦就是64位的也不知道之前是啥情況安裝成32位的了。(O(∩_∩)O哈哈~)
二、安裝了VCForPython27.msi
三、在cmd中執(zhí)行:

pip install pycrypto -i http://mirrors.aliyun.com/pypi/simple/

經(jīng)過上邊兒的幾個步驟,我是能夠成功執(zhí)行

from Crypto.Cipher import AES

現(xiàn)在上一個實(shí)例代碼:

# !/usr/bin/env python
# coding: utf-8
'''

'''

from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex

class MyCrypt():
  def __init__(self, key):
    self.key = key
    self.mode = AES.MODE_CBC

  def myencrypt(self, text):
    length = 16
    count = len(text)
    print count
    if count < length:
      add = length - count
      text= text + ('\0' * add)

    elif count > length:
      add = (length -(count % length))
      text= text + ('\0' * add)

    # print len(text)
    cryptor = AES.new(self.key, self.mode, b'0000000000000000')
    self.ciphertext = cryptor.encrypt(text)
    return b2a_hex(self.ciphertext)

  def mydecrypt(self, text):
    cryptor = AES.new(self.key, self.mode, b'0000000000000000')
    plain_text = cryptor.decrypt(a2b_hex(text))
    return plain_text.rstrip('\0')

if __name__ == '__main__':
  mycrypt = MyCrypt('abcdefghjklmnopq')
  e = mycrypt.myencrypt('hello,world!')
  d = mycrypt.mydecrypt(e)
  print e
  print d

在cmd中執(zhí)行結(jié)果:

總結(jié)

以上就是本文關(guān)于python的Crypto模塊實(shí)現(xiàn)AES加密實(shí)例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

相關(guān)文章

最新評論