surpavan
surpavan

Reputation: 1422

Pycrypto AES decryption resulting in extra "\x07\x07\x07\x07\x07\x07\x07'"

After decryption with Pycrypto Aes, result after base64 decoding is geting extra (at end) "\x07\x07\x07\x07\x07\x07\x07".

Python Output: Decrypted json Message:

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

Unencrypted Json Message:

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

Also, when I try to Json.loads the decrypted message I am getting the TYPE Error, hence I tried to do base64.b64decode() but this one is erroring out as binascii.Error: Incorrect padding.

My REST service Encoding code:

    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 Code:

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'])

Finally, what mistake did I do in my decryption, base64decode and json conversion error ( I think all errors are due to extra padding getting generated)

EDIT: CODE AFTER PADDING:

WCF REST CODE:

    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

Upvotes: 1

Views: 1222

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 94038

It seems like PyCrypto does not provide PKCS#7 padding / unpadding (as it should). So you should implement this using data = data[:-data[-1]]. So you should perform this on variable c directly after the call to decrypt.

You could check all the (in this case 7) padding bytes, but if you want to protect against invalid ciphertext, you should really add a MAC (HMAC) instead.

More information here

Upvotes: 4

Related Questions