Git Product home page Git Product logo

gui_burp_extender_para_encrypter's Issues

AES加解密的程序写的有个问题,没有自动对16位对齐

AES加解密的程序写的有个问题,没有自动对16位对齐,下面是16位对齐,支持中英文版的python程序:
`#coding:utf-8
'''
pthon3 aes1.py
AES Key String: 0102030405060708
AES IV String: 0102030405060708
AES Mode: AES/CBC/PKCS7PADDING
'''
import base64
from binascii import b2a_hex,a2b_hex
from Crypto.Cipher import AES #注:python3 安装 Crypto 是 pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pycryptodome

def pkcs7padding(text):
"""
明文使用PKCS7填充
最终调用AES加密方法时,传入的是一个byte数组,要求是16的整数倍,因此需要对明文进行处理
:param text: 待加密内容(明文)
:return:
"""
bs = AES.block_size # 16
length = len(text)
bytes_length = len(bytes(text,encoding='utf-8'))
# tips:utf-8编码时,英文占1个byte,而中文占3个byte
padding_size = length if(bytes_length == length) else bytes_length
padding = bs - padding_size % bs
# tips:chr(padding)看与其它语言的约定,有的会使用'\0'
padding_text = chr(padding) * padding
return text + padding_text

def pkcs7unpadding(text):
"""
处理使用PKCS7填充过的数据
:param text: 解密后的字符串
:return:
"""
try:
length = len(text)
unpadding = ord(text[length-1])
return text[0:length-unpadding]
except Exception as e:
pass

def aes_encode(key,iv,content):
"""
AES加密
key,iv使用同一个
模式cbc
填充pkcs7
:param key: 密钥
:param content: 加密内容
:return:
"""
key_bytes = bytes(key,encoding='utf-8')
iv_bytes = bytes(iv,encoding='utf-8')
iv = iv_bytes
cipher = AES.new(key_bytes,AES.MODE_CBC,iv)
# 处理明文
content_padding = pkcs7padding(content)
# 加密
aes_encode_bytes = cipher.encrypt(bytes(content_padding,encoding='utf-8'))
# 重新编码
result = str(base64.b64encode(aes_encode_bytes),encoding='utf-8')
#result=str(bytes.hex(aes_encode_bytes))
return result

def aes_decode(key,iv,content):
"""
AES解密
key,iv使用同一个
模式cbc
去填充pkcs7
:param key:
:param content:
:return:
"""
try:
key_bytes = bytes(key,encoding='utf-8')
#iv_bytes = bytes(iv,encoding='utf-8')
#iv = iv_bytes
cipher = AES.new(key_bytes,AES.MODE_CBC,iv)
# base64解码
aes_encode_bytes = base64.b64decode(content)
#aes_encode_bytes=a2b_hex(content)
# 解密
aes_decode_bytes = cipher.decrypt(aes_encode_bytes)
# 重新编码
result = str(aes_decode_bytes,encoding='utf-8')
# 去除填充内容
result = pkcs7unpadding(result)
return result
except Exception as e:
print(e)
pass

key = '{g;$9~l[de]}wSZ9'
key_iv = '$Sz>&*lZFsZ?:P#9'

对中英文加密

data = 'test'
mi="PKVhuJXjpYaanmqbOZIUGw=="
#mi = aes_encode(key,key_iv,data)
#imi=mi.upper()
print("[*]加密结果:\n"+mi)

解密

print("[*]解密结果:\n"+aes_decode(key,key_iv,mi))

`

AES decrypt error

AES/CBC/PKCS5Padding

4oMdph+WhlHALjvZ4BBuTQ==
key = This is the super secret key 123
iv=0

解密此数据提示 java.security.InvalidKeyException: Illegal key size

另外可以考虑支持RC4 DES 的解密 以及加密后的十六进制显示的问题

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.