~mcepl/m2crypto#216: 
SMIME: Decoding a string using M2Crypto -

Migrated from: https://gitlab.com/m2crypto/m2crypto/-/issues/216
Created by: adamcho14 (@adamcho14)
Created at: 2018-05-13T17:28:52.611Z

OK, I've just started to learn M2Crypto and I need to decrypt a string that contains my SMIME encrypted message. I tried this code:

def decrypt(s):
    mime = SMIME.SMIME()

    mime.load_key('private_key.pem', 'certificate.pem')
    out = BIO.MemoryBuffer()
    mime.write(out, s)
    with open("tmp.p7", 'wb') as f:
        f.write(out.read())
        p7, data = SMIME.smime_load_pkcs7("tmp.p7")
    return mime.decrypt(p7)

I got this assertion error: File "/Users/Adam/Library/Python/3.5/lib/python/site-packages/M2Crypto/SMIME.py", line 267, in write assert isinstance(pkcs7, PKCS7)

Would you, please, help me?

Status
REPORTED
Submitter
~mcepl
Assigned to
No-one
Submitted
7 months ago
Updated
7 months ago
Labels
smime

~mcepl 7 months ago

On 2018-05-13T22:56:09.053Z, adamcho14 wrote:

And when I just do this:

def decrypt(s):
    mime = SMIME.SMIME()

    with open("tmp.p7", 'wb') as f:
        f.write(s.encode('utf-8'))
        p7, data = SMIME.smime_load_pkcs7("tmp.p7")
    return mime.decrypt(p7)

It displays File "/Users/Adam/Library/Python/3.5/lib/python/site-packages/M2Crypto/SMIME.py", line 113, in smime_load_pkcs7 p7_ptr, bio_ptr = m2.smime_read_pkcs7(bio) M2Crypto.SMIME.SMIME_Error: no content type

I also tried to write s as a string in write mode. The result is the same.

~mcepl 7 months ago

On 2018-05-14T07:37:44.266Z, Matěj Cepl wrote:

Couple of possible links:

Any merge requests for that HOWTO document would be more than welcome.

~mcepl 7 months ago

On 2018-05-15T10:14:34.156Z, adamcho14 wrote:

Thank you for your answer. One of the stackoverflow links is mine, but it did not help. Actually, I had an indentation bug in the code. Now, it looks like this and it prints bad decrypt.

def decrypt(s):
    mime = SMIME.SMIME()

    mime.load_key('private_key.pem', 'certificate.pem')

    with open("tmp.p7", 'w') as f:
        f.write(s)

    p7, data = SMIME.smime_load_pkcs7("tmp.p7")

    try:
        return mime.decrypt(p7)
    except SMIME.PKCS7_Error as err:
        print("Decryption fault: " + str(err))
        return "0"
Register here or Log in to comment, or comment via email.