Javascript 如何在python中加密/签名数据,并使用RSA在Reactjs中解密数据?
我在服务器中有一个数据,我需要加密或签名,并使用API将其发送给react应用程序,在react中,我需要使用公钥解密或验证签名。 下面是我正在使用的python代码:Javascript 如何在python中加密/签名数据,并使用RSA在Reactjs中解密数据?,javascript,python,reactjs,Javascript,Python,Reactjs,我在服务器中有一个数据,我需要加密或签名,并使用API将其发送给react应用程序,在react中,我需要使用公钥解密或验证签名。 下面是我正在使用的python代码: import json from Crypto import Random from Crypto.PublicKey import RSA import base64 def generate_keys(): modulus_length = 256 * 10 privatekey = RSA.generate
import json
from Crypto import Random
from Crypto.PublicKey import RSA
import base64
def generate_keys():
modulus_length = 256 * 10
privatekey = RSA.generate(modulus_length, Random.new().read)
publickey = privatekey.publickey()
return privatekey, publickey
def encrypt_message(a_message, publickey, privatekey):
encrypted_msg = publickey.encrypt(a_message.encode("utf-8"), 32)[0]
sign = privatekey.sign(a_message.encode("utf-8"), 32)
encoded_encrypted_msg = base64.b64encode(encrypted_msg)
return encoded_encrypted_msg.decode("utf-8"), sign;
a_message = 'Hello'
privatekey, publickey = generate_keys()
encrypted_msg, sign = encrypt_message(a_message, publickey, privatekey)
我需要解密此加密消息或在react应用程序中验证签名数据有什么方法吗?经过一些研究,我找到了一种在python中对数据签名并在react应用程序中验证的方法 python:
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256
from Crypto.Signature import PKCS1_v1_5
import base64
def sign_message(a_message, privatekey):
digest = SHA256.new()
digest.update(a_message.encode("utf-8"))
privatekey = PKCS1_v1_5.new(privatekey)
sign = base64.b64encode(privatekey.sign(digest))
return sign
def generate_keys():
modulus_length = 256 * 10
privatekey = RSA.generate(modulus_length, Random.new().read)
publickey = privatekey.publickey()
return privatekey, publickey
privatekey, publickey = generate_keys()
a_message = "Hello" # message that we need to sign
sign = sign_message(a_message, privatekey)
javascript/JS:
import JSEncrypt from 'jsencrypt';
import CryptoJS from 'crypto-js';
var public_key = new JSEncrypt();
public_key.setPublicKey(publicKey); // publicKey that we get from python
data_to_verify = "Hello" // message you signed in python
signature = Signature_that_you_get_after_signing_in_python
var verified = public_key.verify(data_to_verify, signature, CryptoJS.SHA256);
console.log(verified) // we should get true if we have correct public key, signature and data
经过一些研究,我找到了一种用python对数据进行签名并在react应用程序中进行验证的方法 python:
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256
from Crypto.Signature import PKCS1_v1_5
import base64
def sign_message(a_message, privatekey):
digest = SHA256.new()
digest.update(a_message.encode("utf-8"))
privatekey = PKCS1_v1_5.new(privatekey)
sign = base64.b64encode(privatekey.sign(digest))
return sign
def generate_keys():
modulus_length = 256 * 10
privatekey = RSA.generate(modulus_length, Random.new().read)
publickey = privatekey.publickey()
return privatekey, publickey
privatekey, publickey = generate_keys()
a_message = "Hello" # message that we need to sign
sign = sign_message(a_message, privatekey)
javascript/JS:
import JSEncrypt from 'jsencrypt';
import CryptoJS from 'crypto-js';
var public_key = new JSEncrypt();
public_key.setPublicKey(publicKey); // publicKey that we get from python
data_to_verify = "Hello" // message you signed in python
signature = Signature_that_you_get_after_signing_in_python
var verified = public_key.verify(data_to_verify, signature, CryptoJS.SHA256);
console.log(verified) // we should get true if we have correct public key, signature and data