TypeError: a bytes-like object is required, not 'Key_Private' как решить ошибкy?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import binascii
import rsa.randnum
import cryptography
from Crypto.Cipher import Salsa20
from cryptography.fernet import Fernet, MultiFernet
import base64
import qrcode
import image
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.backends import default_backend
from authlib.specs.rfc7519 import jwt
from cryptography.hazmat.primitives.serialization import load_pem_public_key
from cryptography import x509
from cryptography.hazmat.primitives.hashes import HashContext
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.BestAvailableEncryption(b'mypassword')
)
pem.splitlines()[0]
class Key_Private():
def __init__(self, private_key):
self.__private_key = private_key
def private_key_funct(self):
return self.__private_key
def __str__(self):
return 'Your private key: {0}'.format(self.__private_key)
new_private_key = Key_Private(private_key)
file_1 = open("PrivateKey.pem", "wb")
file_1.write(new_private_key)
file_1.close()
Ошибка TypeError: a bytes-like object is required, not 'Key_Private'
Ответы (1 шт):
Автор решения: eri
→ Ссылка
Можно так, а в Вашем коде все както нагромажденно
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend(),
)
key_pem = key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.BestAvailableEncryption(b'mypassword')
)
with open('tls_key.pem', 'wb') as f:
f.write(key_pem)