Pycrypto AES解密导致额外的“\x07\x07\x07\x07\x07\x07\x07”

2024-09-30 04:30:33 发布

您现在位置:Python中文网/ 问答频道 /正文

使用Pycrypto-Aes解密后,base64解码后的结果将得到额外的(在结尾)"\x07\x07\x07\x07\x07\x07\x07"。在

Python输出: 解密的json消息:

b'{"EndTime":"\\/Date(1408876230508+0530)\\/","SessionID":"ddbecfdb-b87f-48d5-84dd-9dce439459ac","TestString":"WORKING FINE"}\x07\x07\x07\x07\x07\x07\x07'

未加密的Json消息:

^{pr2}$

还有,当我试着Json.loads解密后的消息我得到了类型错误,因此我尝试执行base64.b64decode(),但这个错误是binascii.错误:填充不正确。在

我的REST服务编码:

    Dim rawdatastream As New MemoryStream
    Dim jsonserialization As New Json.DataContractJsonSerializer(GetType(AuthorizationResultType))
    jsonserialization.WriteObject(rawdatastream, c)

    Using encryptor As Aes = Aes.Create()
        encryptor.Key = {66, 16, 1, 61, 58, 16, 16, 49, 66, 16, 46, 46, 16, 146, 49, 255,
                         240, 127, 189, 191, 3, 151, 89, 124, 56, 89, 134, 164, 165, 201, 212, 216}
        encryptor.Mode = CipherMode.CBC
        encryptor.IV = {66, 16, 1, 61, 58, 16, 16, 49, 66, 16, 46, 46, 16, 146, 49, 66}
        Dim clearBytes As Byte() = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(rawdatastream.ToArray()))
        Using ms As New MemoryStream()
            Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
                cs.Write(clearBytes, 0, clearBytes.Length)
                cs.Close()
            End Using
            result.Msg = Convert.ToBase64String(ms.ToArray())
        End Using
    End Using

Python代码:

import requests
import json
import base64
from Crypto.Cipher import AES

baseurl = 'http://localhost:9624/'

def LoginAccess(userid, password):
    print('Accessing Authorization info')
    response = requests.get(baseurl +'BasicServ.svc/auth/Authorize/'+userid+'/'+password+'/2')
    print (response.json())

    rawmsg =response.json()
    msg= rawmsg['AuthorizeResult']['Msg']

    cypherkey=[66, 16, 1, 61, 58, 16, 16, 49, 66, 16, 46, 46, 16, 146, 49, 255,240, 127, 189, 191, 3, 151, 89, 124, 56, 89, 134, 164, 165, 201, 212, 216]
    iv=[66, 16, 1, 61, 58, 16, 16, 49, 66, 16, 46, 46, 16, 146, 49, 66]
    cry=AES.new(bytes(cypherkey),AES.MODE_CBC,bytes(iv))
    print("decryption done")
    c = cry.decrypt(base64.b64decode(msg))
    print (c)
    print(base64.b64decode(c))
    print (json.loads(base64.b64decode(c)))
    print (rawmsg['AuthorizeResult']['MsgN'])

最后,我在解密、base64decode和json转换错误中犯了什么错误(我认为所有错误都是由于生成了额外的填充)

编辑:填充后的代码:

WCF REST代码:

    Dim rawdatastream As New MemoryStream
    Dim jsonserialization As New Json.DataContractJsonSerializer(GetType(AuthorizationResultType))
    jsonserialization.WriteObject(rawdatastream, c)
    result.Unlocksize = Encoding.UTF8.GetString(rawdatastream.ToArray()).Length


    Using encryptor As Aes = Aes.Create()
        encryptor.Mode = CipherMode.CBC
        encryptor.Key = {66, 16, 1, 61, 58, 16, 16, 49, 66, 16, 46, 46, 16, 146, 49, 255, 240, 127, 189, 191, 3, 151, 89, 124, 56, 89, 134, 164, 165, 201, 212, 216}
        encryptor.IV = {66, 16, 1, 61, 58, 16, 16, 49, 66, 16, 46, 46, 16, 146, 49, 66}

        Console.WriteLine(encryptor.IV)
        Console.WriteLine(encryptor.Key)
        Dim datalen As Integer
        Dim actualcoount As Integer = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(rawdatastream.ToArray())).Count
        datalen = 32 - (Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(rawdatastream.ToArray())).Count Mod 32)
        Dim correctionbytes As String = ""
        For i = 1 To datalen
            correctionbytes = correctionbytes + "1"
        Next
        result.Unlocksize = datalen

        Dim clearBytes As Byte() = Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(rawdatastream.ToArray()) + correctionbytes)
        Using ms As New MemoryStream()
            Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
                cs.Write(clearBytes, 0, clearBytes.Length)
                cs.Close()
            End Using
            result.Msg = Convert.ToBase64String(ms.ToArray())
        End Using
    End Using

Tags: jsonnewas错误utf8encodingusingprint
1条回答
网友
1楼 · 发布于 2024-09-30 04:30:33

PyCrypto似乎没有提供PKCS#7填充/取消添加(因为它应该这样做)。所以您应该使用data = data[:-data[-1]]来实现这一点。所以您应该在调用decrypt之后直接对变量c执行此操作。在

您可以检查所有的填充字节(在本例中是7个),但是如果您想防止无效的密文,您应该添加一个MAC(HMAC)。在

更多信息here

相关问题 更多 >

    热门问题