Python/cryptography: ValueError: Could not deserialize key data
I am trying to encoded a JWT payload using "RS256", but I'm getting ValueError: Could not deserialize key data. error from the cryptography library. 
import jwt
private_key = b'-----BEGIN PRIVATE KEY-----nMIGEAgEAMBAGByqGSM49AgEGBS...'
public_key = b'-----BEGIN PUBLIC KEY-----nMHYwEAYHKoZIzj0CAQYFK4EEAC...'
encoded = jwt.encode({'some': 'payload'}, private_key, algorithm='RS256')
But I'm getting the following error.
Traceback (most recent call last):
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/algorithms.py", line 205, in prepare_key
    key = load_pem_private_key(key, password=None, backend=default_backend())
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 20, in load_pem_private_key
    return backend.load_pem_private_key(data, password)
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1016, in load_pem_private_key
    password,
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1235, in _load_key
    self._handle_key_loading_error()
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1293, in _handle_key_loading_error
    raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/api_jwt.py", line 65, in encode
    json_payload, key, algorithm, headers, json_encoder
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/api_jws.py", line 113, in encode
    key = alg_obj.prepare_key(key)
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/algorithms.py", line 207, in prepare_key
    key = load_pem_public_key(key, backend=default_backend())
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key
    return backend.load_pem_public_key(data)
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1042, in load_pem_public_key
    self._handle_key_loading_error()
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1293, in _handle_key_loading_error
    raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.
python python-3.x encryption cryptography jwt
add a comment |
I am trying to encoded a JWT payload using "RS256", but I'm getting ValueError: Could not deserialize key data. error from the cryptography library. 
import jwt
private_key = b'-----BEGIN PRIVATE KEY-----nMIGEAgEAMBAGByqGSM49AgEGBS...'
public_key = b'-----BEGIN PUBLIC KEY-----nMHYwEAYHKoZIzj0CAQYFK4EEAC...'
encoded = jwt.encode({'some': 'payload'}, private_key, algorithm='RS256')
But I'm getting the following error.
Traceback (most recent call last):
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/algorithms.py", line 205, in prepare_key
    key = load_pem_private_key(key, password=None, backend=default_backend())
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 20, in load_pem_private_key
    return backend.load_pem_private_key(data, password)
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1016, in load_pem_private_key
    password,
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1235, in _load_key
    self._handle_key_loading_error()
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1293, in _handle_key_loading_error
    raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/api_jwt.py", line 65, in encode
    json_payload, key, algorithm, headers, json_encoder
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/api_jws.py", line 113, in encode
    key = alg_obj.prepare_key(key)
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/algorithms.py", line 207, in prepare_key
    key = load_pem_public_key(key, backend=default_backend())
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key
    return backend.load_pem_public_key(data)
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1042, in load_pem_public_key
    self._handle_key_loading_error()
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1293, in _handle_key_loading_error
    raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.
python python-3.x encryption cryptography jwt
 
 
 
 
 
 
 
 Possibly only include the private key data after the newline (- n)?
 
 – Sam Hollenbach
 Jan 18 at 17:42
 
 
 
 
 
 
 
 
 
 
 @SamHollenbach Can you show code example please?
 
 – user1187968
 Jan 18 at 17:45
 
 
 
 
 
 
 
 
 
 
 Just- private_key = b'MIGEAgEAMBAGByqGSM49AgEGBS...'. Not sure if this will work, but just a thought.
 
 – Sam Hollenbach
 Jan 18 at 17:46
 
 
 
 
 
 
 
 
 
 
 Try this thread too: github.com/jpadilla/pyjwt/issues/257
 
 – Sam Hollenbach
 Jan 18 at 17:47
 
 
 
 
 
 
 
 
 
 
 It works for me. My guess is your- private_keyis simply malformed.
 
 – James K Polk
 Jan 18 at 20:00
 
 
 
 
 
add a comment |
I am trying to encoded a JWT payload using "RS256", but I'm getting ValueError: Could not deserialize key data. error from the cryptography library. 
import jwt
private_key = b'-----BEGIN PRIVATE KEY-----nMIGEAgEAMBAGByqGSM49AgEGBS...'
public_key = b'-----BEGIN PUBLIC KEY-----nMHYwEAYHKoZIzj0CAQYFK4EEAC...'
encoded = jwt.encode({'some': 'payload'}, private_key, algorithm='RS256')
But I'm getting the following error.
Traceback (most recent call last):
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/algorithms.py", line 205, in prepare_key
    key = load_pem_private_key(key, password=None, backend=default_backend())
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 20, in load_pem_private_key
    return backend.load_pem_private_key(data, password)
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1016, in load_pem_private_key
    password,
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1235, in _load_key
    self._handle_key_loading_error()
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1293, in _handle_key_loading_error
    raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/api_jwt.py", line 65, in encode
    json_payload, key, algorithm, headers, json_encoder
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/api_jws.py", line 113, in encode
    key = alg_obj.prepare_key(key)
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/algorithms.py", line 207, in prepare_key
    key = load_pem_public_key(key, backend=default_backend())
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key
    return backend.load_pem_public_key(data)
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1042, in load_pem_public_key
    self._handle_key_loading_error()
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1293, in _handle_key_loading_error
    raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.
python python-3.x encryption cryptography jwt
I am trying to encoded a JWT payload using "RS256", but I'm getting ValueError: Could not deserialize key data. error from the cryptography library. 
import jwt
private_key = b'-----BEGIN PRIVATE KEY-----nMIGEAgEAMBAGByqGSM49AgEGBS...'
public_key = b'-----BEGIN PUBLIC KEY-----nMHYwEAYHKoZIzj0CAQYFK4EEAC...'
encoded = jwt.encode({'some': 'payload'}, private_key, algorithm='RS256')
But I'm getting the following error.
Traceback (most recent call last):
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/algorithms.py", line 205, in prepare_key
    key = load_pem_private_key(key, password=None, backend=default_backend())
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 20, in load_pem_private_key
    return backend.load_pem_private_key(data, password)
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1016, in load_pem_private_key
    password,
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1235, in _load_key
    self._handle_key_loading_error()
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1293, in _handle_key_loading_error
    raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/api_jwt.py", line 65, in encode
    json_payload, key, algorithm, headers, json_encoder
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/api_jws.py", line 113, in encode
    key = alg_obj.prepare_key(key)
  File "/Users/user1/venv_1/lib/python3.7/site-packages/jwt/algorithms.py", line 207, in prepare_key
    key = load_pem_public_key(key, backend=default_backend())
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key
    return backend.load_pem_public_key(data)
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1042, in load_pem_public_key
    self._handle_key_loading_error()
  File "/Users/user1/venv_1/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1293, in _handle_key_loading_error
    raise ValueError("Could not deserialize key data.")
ValueError: Could not deserialize key data.
python python-3.x encryption cryptography jwt
python python-3.x encryption cryptography jwt
asked Jan 18 at 17:39
user1187968user1187968
98342252
98342252
 
 
 
 
 
 
 
 Possibly only include the private key data after the newline (- n)?
 
 – Sam Hollenbach
 Jan 18 at 17:42
 
 
 
 
 
 
 
 
 
 
 @SamHollenbach Can you show code example please?
 
 – user1187968
 Jan 18 at 17:45
 
 
 
 
 
 
 
 
 
 
 Just- private_key = b'MIGEAgEAMBAGByqGSM49AgEGBS...'. Not sure if this will work, but just a thought.
 
 – Sam Hollenbach
 Jan 18 at 17:46
 
 
 
 
 
 
 
 
 
 
 Try this thread too: github.com/jpadilla/pyjwt/issues/257
 
 – Sam Hollenbach
 Jan 18 at 17:47
 
 
 
 
 
 
 
 
 
 
 It works for me. My guess is your- private_keyis simply malformed.
 
 – James K Polk
 Jan 18 at 20:00
 
 
 
 
 
add a comment |
 
 
 
 
 
 
 
 Possibly only include the private key data after the newline (- n)?
 
 – Sam Hollenbach
 Jan 18 at 17:42
 
 
 
 
 
 
 
 
 
 
 @SamHollenbach Can you show code example please?
 
 – user1187968
 Jan 18 at 17:45
 
 
 
 
 
 
 
 
 
 
 Just- private_key = b'MIGEAgEAMBAGByqGSM49AgEGBS...'. Not sure if this will work, but just a thought.
 
 – Sam Hollenbach
 Jan 18 at 17:46
 
 
 
 
 
 
 
 
 
 
 Try this thread too: github.com/jpadilla/pyjwt/issues/257
 
 – Sam Hollenbach
 Jan 18 at 17:47
 
 
 
 
 
 
 
 
 
 
 It works for me. My guess is your- private_keyis simply malformed.
 
 – James K Polk
 Jan 18 at 20:00
 
 
 
 
 
Possibly only include the private key data after the newline (
n)?– Sam Hollenbach
Jan 18 at 17:42
Possibly only include the private key data after the newline (
n)?– Sam Hollenbach
Jan 18 at 17:42
@SamHollenbach Can you show code example please?
– user1187968
Jan 18 at 17:45
@SamHollenbach Can you show code example please?
– user1187968
Jan 18 at 17:45
Just
private_key = b'MIGEAgEAMBAGByqGSM49AgEGBS...'. Not sure if this will work, but just a thought.– Sam Hollenbach
Jan 18 at 17:46
Just
private_key = b'MIGEAgEAMBAGByqGSM49AgEGBS...'. Not sure if this will work, but just a thought.– Sam Hollenbach
Jan 18 at 17:46
Try this thread too: github.com/jpadilla/pyjwt/issues/257
– Sam Hollenbach
Jan 18 at 17:47
Try this thread too: github.com/jpadilla/pyjwt/issues/257
– Sam Hollenbach
Jan 18 at 17:47
It works for me. My guess is your
private_key is simply malformed.– James K Polk
Jan 18 at 20:00
It works for me. My guess is your
private_key is simply malformed.– James K Polk
Jan 18 at 20:00
add a comment |
                            0
                        
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54258946%2fpython-cryptography-valueerror-could-not-deserialize-key-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
                            0
                        
active
oldest
votes
                            0
                        
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54258946%2fpython-cryptography-valueerror-could-not-deserialize-key-data%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
 
Possibly only include the private key data after the newline (
n)?– Sam Hollenbach
Jan 18 at 17:42
@SamHollenbach Can you show code example please?
– user1187968
Jan 18 at 17:45
Just
private_key = b'MIGEAgEAMBAGByqGSM49AgEGBS...'. Not sure if this will work, but just a thought.– Sam Hollenbach
Jan 18 at 17:46
Try this thread too: github.com/jpadilla/pyjwt/issues/257
– Sam Hollenbach
Jan 18 at 17:47
It works for me. My guess is your
private_keyis simply malformed.– James K Polk
Jan 18 at 20:00