无法使用SUDS(python)

时间:2017-07-27 20:44:13

标签: python soap mtom wsse xop

我有一个从客户端下载文件的SOAP客户端。请求的主体和附件(文件)使用两个单独的密钥加密。这两个密钥都包含在相应的<xenc:EncryptedKey>标记中。我可以使用其中一个键解密身体没问题,但是附件给了我一些问题。

我的代码:

from Crypto.Cipher import AES
from Crypto import Random

class AESCipher:
    def __init__( self, key, BS = 16):
        self.key = key
        self.BS = BS
    def _pad(self, s):
        return s + (self.BS - len(s) % self.BS) * chr(self.BS - len(s) % self.BS) 
    def _unpad(self, s):
        return s[:-ord(s[len(s)-1:])]
    def decrypt( self, enc ):
        enc = base64.b64decode(enc)
        iv = enc[:self.BS]
        cipher = AES.new(self.key, AES.MODE_CBC, iv )
        return self._unpad(cipher.decrypt( enc[self.BS:]))

with open('test/resp-down-file','rb') as f:
    encFile = f.read()

#...the key is extracted elsewhere...

cryptor = AESCipher(key)
cryptor.decrypt(encFile)

充其量我得到了混乱的结果,但通常它只是说Error while decrypting in CBC mode问题:有没有人遇到过这个问题?我对Python,Java,PHP,Perl,C中的建议持开放态度,几乎可以在linux上运行。 关于MTOM / XOP附件加密的方式有什么特别之处吗?

我已经看到了this问题,但它没有正确答案。八位字节/流指的是内容类型,而不是传递机制,因此答案不正确。

编辑:服务器规范称他们使用带有PKCS5填充的AES128-CBC算法加密消息。对我来说,他们使用DES填充进行AES加密是没有意义的,但是他们对此非常坚定。

Edit2:有时附加的消息没有AES128解密的正确长度(例如6023字节或4071字节)。

作为参考,该消息以这种格式出现:

--MIMEBoundaryurn_uuid_641B9D88B371C8A80C1501095406237
Content-Type: text/xml; charset=UTF-8
Content-Transfer-Encoding: binary
Content-ID: <0.urn:uuid:641B9D88B371C8A80C1501095406238@apache.org>

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
...
</soapenv:Envelope>

--MIMEBoundaryurn_uuid_641B9D88B371C8A80C1501095406237
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <urn:uuid:641B9D88B371C8A80C1501095406240@apache.org>

­8eJ¨%• }\  Œ“\ò½<( nË%¸£käö0   ‡XW�5ìR Ë�¾p�Áëş3Âå'¹5¥#=Zg¡øø{I~FP�n ×aµR^Föž¤¥EÍf«Îê�0qÊMö²È€]®PÌ>A@‡ Cş®±9>Áf7P’#ã  …fç~yxÔ.å–×v›±Cô„Ê
...
--MIMEBoundaryurn_uuid_641B9D88B371C8A80C1501095406237--

1 个答案:

答案 0 :(得分:0)

我弄明白了这个问题。事实证明,我收到数据的方式(使用result = requests.post(....)修剪了一些不可打印的字符,因为我使用的是result.text

现在我已切换到result.raw.read(),问题已解决。