aes python bin文件加密

AES加密原理

参考链接:
AES加密算法的详细介绍与实现
AES的5种加密模式
在线AES加解密

aes有5种加密模式,这里用python实现ecb模式的加密。

在网上找到相关的python代码,但是并不完善,需要自己修改。

一、环境搭建

Ubuntu安装

1.python环境3.6

我的电脑之前安装的是3.5,先进行版本升级。
参考链接:
ubuntu升级python版本(3.5->3.6)

#获取最新的python3.6,将其添加至当前apt库中,并自动导入公钥
$ sudo add-apt-repository ppa:jonathonf/python-3.6
$ sudo apt-get update
$ sudo apt-get install python3.6

# 移除原3.4link 
$ sudo rm /usr/bin/python3

# 更换默认python3 的版本为3.6
$ sudo ln -s /usr/bin/python3.6 /usr/bin/python3

安装完Python后pip需重新安装:

$ wget https://bootstrap.pypa.io/get-pip.py  --no-check-certificate
$ sudo python3 get-pip.py
pip -V  //注意查看pip的是不是指向 python 3.6

2.安装crypto模块

直接运行代码,报错缺少模块。
参考链接:
No module named ‘Crypto’

$ sudo pip install pycryptodome

到安装路径(路径一般在:Python\Python36\Lib\site-packages)下,把crypto 改为Crypto。我这里不需要修改名称就可以运行代码。

windows 环境

我的win电脑中已经安装python3.6,只需安装离线包。

参考链接:
whl包
python 离线包
is not a supported wheel on this platform的解决方法
在上面的链接中先找到wheel-xx.whl , 下载后安装,pip install wheel-xx.whl
然后下载pycryptodome,安装即可。

注意:这里需要查看自己的pip支持哪些类型的包,然后对应下载。否则安装会提示 is not a supported wheel on this platform 。

二、代码

参考代码:
python 实现AES加密和解密
Python 的AES加密与解密
python读取文件并下发串口
以下代码仅供参考!

# coding: utf-8

import base64
import time
import datetime
import os


"""
ECB没有偏移量
"""
from Crypto.Cipher import AES
import binascii
# import b2a_hex, a2b_hex


def add_to_16(text):
    if len(text) % 16:
        add = 16 - (len(text) % 16)
        text = text + (b'\0' * add)
    return text

# 加密函数
def encrypt(key, text):
    # key = '9999999999999999'.encode('utf-8')
    mode = AES.MODE_ECB
    text = add_to_16(text)
    cryptos = AES.new(key.encode('utf8'), mode)
    # cryptos = AES.new(key, mode)
    cipher_text = cryptos.encrypt(text)
    return cipher_text

# 解密后,去掉补足的空格用strip() 去掉
def decrypt(key, text):
    # key = '9999999999999999'.encode('utf-8')
    mode = AES.MODE_ECB
    cryptor = AES.new(key.encode('utf8'), mode)
    # cryptor = AES.new(key, mode)
    plain_text = cryptor.decrypt(text)
    # return bytes.decode(plain_text).rstrip('\0')
    return plain_text


# if __name__ == '__main__':
#     e = encrypt("hello world")  # 加密
#     d = decrypt(e)  # 解密
#     print("加密:", e)
#     print("解密:", d)

# 密钥(key), 密斯偏移量(iv) CBC模式加密
 
def AES_Encrypt(key, data):
    vi = '0102030405060708'
    pad = lambda s: s + (16 - len(s)%16) * chr(16 - len(s)%16)
    data = pad(data)
    # 字符串补位
    cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
    encryptedbytes = cipher.encrypt(data.encode('utf8'))
    # 加密后得到的是bytes类型的数据
    encodestrs = base64.b64encode(encryptedbytes)
    # 使用Base64进行编码,返回byte字符串
    enctext = encodestrs.decode('utf8')
    # 对byte字符串按utf-8进行解码
    return enctext
 
 
def AES_Decrypt(key, data):
    vi = '0102030405060708'
    data = data.encode('utf8')
    encodebytes = base64.decodebytes(data)
    # 将加密数据转换位bytes类型数据
    cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, vi.encode('utf8'))
    text_decrypted = cipher.decrypt(encodebytes)
    unpad = lambda s: s[0:-s[-1]]
    text_decrypted = unpad(text_decrypted)
    # 去补位
    text_decrypted = text_decrypted.decode('utf8')
    return text_decrypted
 
def get_FileSize(filePath):
    # filePath = unicode(filePath,'utf8')
    fsize = os.path.getsize(filePath)
    return fsize
#       fsize = fsize/float(1024*1024)
#       return round(fsize,2)

# -------------------------- test 1 -----------------
# key = '0CoJUm6Qyw8W8jud'
# data = 'sdadsdsdsfd'
# AES_Encrypt(key, data)
# enctext = AES_Encrypt(key, data)
# print(enctext)
# text_decrypted = AES_Decrypt(key, enctext)
# print(text_decrypted)

# hBXLrMkpkBpDFsf9xSRGQQ==
# sdadsdsdsfd

# ------------------------------ test 2 ---------------
key = '0CoJUm6Qyw8W8jud'
filePath = './boot.bin'
fb = open(filePath, 'rb')
fsize = get_FileSize(filePath)
print("fsize = ", fsize)

fdatas = fb.read(fsize)
# print(type(fdatas))
fdata = binascii.b2a_hex(fdatas)
# print(type(fdata))
# print(fdata)

print("start encrypt \n")
enctext = encrypt(key, fdata)
# print(enctext)
faesbin = open('./aes.bin', 'wb')
faesbin.write(enctext)

print("start decrypt \n")
text_decrypted = decrypt(key, enctext)
# print(text_decrypted)
fbin = open('aes_dec.bin', 'wb')
fbin.write(binascii.a2b_hex(text_decrypted))


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值