简体   繁体   中英

Python Cryptography Library fails to load_pem_public_key with my PEM Format

We are currently working on a project where we have to take the public key of a user from the database and encrypt a string for said user.

Unfortunately, we are getting an error with our cryptography library (cryptography==1.2.1).

The Public Key:

-----BEGIN PUBLIC KEY-----MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAy2SBwad+TZRyCPDUdxZkGqBipyY7UmTTdGOKL/dgOOsuITXFy/COCi2ylo/owI7u+/K5lkdoBunn1OEu+UFzxRy4bKQK2OkpP6dtAHfyexracpeiHVccRIkQB743DpfupDVtR5TweZsjkRM2SDcpwXZJbnSpa8pzxl6mHvZhV/D2kDkSPLQnI6iWchExt5e/psorpWvy22smaL1xvINztaaRSXFqMQILtSxv/2lJJGc6PRyQacQ4XxDk2DwKBKPyFYHYelYpjGFErUIRz7/EdY4vc3pzr2HMo0AFvvu51ezKIrFGDXt9I7xwEEOSY59mNtA9u9oPyd7UhXkK1JB7OSI458rfHZEXUsvmqqpHH8FKC4dBCZasEHqzMdter05h/v2hMbPV/QgqrArs1p/P6KMpnlYvhnTrIj9p30kOIq7WSfCyXBlTjAb8unYST6vtHXB21TOM6e6szea8fWgCnO3e/xD4M/xdVEqgGxDJWr7qOeHL+l1I93f0wBpO4qlmdMgFz+UqO2p+7dtN4JQO5PwCYHOLgE9bJJZKzaB61BeM4xznqiVfUxOBmtN0k5m1DnKL0lUagSyitwq0H7AY802O91n7ykFYO/gsFkfqNFtdgsGX56lqILU1dYKX3fVsd8r5rENndBE7J0HEUjYz9JHjS+m8EMJ+57kKztP0EgUCAwEAAQ==-----END PUBLIC KEY-----

Note: When I paste the code into sublime there are no whichspaces or new line characters! It is just one line. <-- Is this already the problem ?

Having this coming from the database in a django model it is a unicode/string.

I have taken the code from https://cryptography.io/en/latest/hazmat/primitives/asymmetric/rsa/#key-loading

The line that fails is:

key = load_pem_public_key(bytes(pem_public_key), backend=default_backend())

The error is:

ValueError: Could not unserialize key data.

Which comes from:

            assert errors[0][1] in (
                self._lib.ERR_LIB_EVP,
                self._lib.ERR_LIB_PEM,
                self._lib.ERR_LIB_ASN1,
            )
            raise ValueError("Could not unserialize key data.")

Which is called because of:

            if rsa_cdata != self._ffi.NULL:
                rsa_cdata = self._ffi.gc(rsa_cdata, self._lib.RSA_free)
                evp_pkey = self._rsa_cdata_to_evp_pkey(rsa_cdata)
                return _RSAPublicKey(self, rsa_cdata, evp_pkey)
            else:
                self._handle_key_loading_error()

As the assert if rsa_cdata != self._ffi.NULL: fails the error is raised.

What prevents the load_pem_public_key function from reading the public key correctly ?

Ok, So after further googling I found the answer here: https://crypto.stackexchange.com/questions/19043/can-i-remove-new-lines-in-a-public-key

Which resulted in:

pem_public_key = pem_public_key.replace("-----BEGIN PUBLIC KEY-----", "")
pem_public_key = pem_public_key.replace("-----END PUBLIC KEY-----", "")
pem_public_key = re.sub("(.{64})", "\\1\n", pem_public_key, 0, re.DOTALL)
pem_public_key = "-----BEGIN PUBLIC KEY-----\n" + pem_public_key
pem_public_key = pem_public_key + "\n-----END PUBLIC KEY-----"

Effectively, a new line after 64chars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM