提问人:Daniel Acosta 提问时间:11/17/2023 最后编辑:rabbibillclintonDaniel Acosta 更新时间:11/18/2023 访问量:62
NextJS 加密错误
Bad encrypt with NextJS
问:
我有一个带有 NextJS 的客户端和一个带有 Python 的 API。我尝试使用客户端加密信息并使用 API 对其进行解密。 我有一个带有 NodeJS 的脚本来测试加密,然后我将加密数据粘贴到 Postman 中以测试 API。
这是 NextJS 中使用 NodeJS 的代码。 我向 API 发送加密数据的 base64 字符串。
const publicKey = 'abcdefghijklmnopqrstuvwxyz';
const encryptedData = crypto.publicEncrypt({
key: Buffer.from(publicKey, 'utf-8'),
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: 'sha256'
}, Buffer.from(data));
const encryptedDataBase64 = encryptedData.toString('base64');
return encryptedDataBase64
使用 Python,我获取 base64 字符串,然后解密数据。
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
encrypted_data = base64.b64decode(b64data)
with open('./secure/private_key.pem', 'rb') as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
backend=default_backend()
)
decrypted_data_bytes = private_key.decrypt(
encrypted_data,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
decrypted_data = decrypted_data_bytes.decode('utf-8')
return decrypted_data
我试过用以下方法加密数据:
- NextJS,然后通过 Postman 发送数据
- NodeJS,然后通过 Postman 发送数据
- NodeJS,然后将数据粘贴到 NextJS 中,然后向 API 发出请求。
这些都无法在 API 中正确解密。问题在于在NextJS中对数据进行加密时。
答: 暂无答案
评论
data
encryptedDataBase64
b64data