简体   繁体   中英

Problem with encrypting with Python and decrypting in Javascript

i'm having troubles encrypting with Python and decrypting with Javascript, here's what i did in Python:

import binascii
import pandas as pd
import Crypto
import Crypto.Random
from Crypto.PublicKey import RSA
import binascii
import json
from Crypto.Signature import PKCS1_v1_5
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Hash import SHA512, SHA384, SHA256, SHA, MD5
import requests
from flask import Flask, jsonify, request, render_template
  

....

public_keyy = RSA.importKey(binascii.unhexlify(recipient_address))
encryptor = PKCS1_OAEP.new(public_keyy)
encrypted = encryptor.encrypt(bytes(value, encoding='utf-8'))
value=  binascii.hexlify(encrypted).decode('utf8',errors='ignore')
print("Encrypted:", binascii.hexlify(encrypted))

And here's what im doing in Javascript (using JSEncrypt.js ):

        for (i = 1; i < response.length; i++) {
            for (j = 0; j < response["chain"][i]["transactions"].length; j++) {

              //format date 
              var options = {  year: "numeric", month: "short",  day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit"  };
              var date = new Date(response["chain"][i]["timestamp"] * 1000);
              var formattedDateTime = date.toLocaleTimeString("en-us", options);
              var valeur=response["chain"][i]["transactions"][j]["value"]

              //DECRYPTION
              
               decrypt = new JSEncrypt();
               decrypt.setPrivateKey(priv_key);
               var plainText = decrypt.decrypt(valeur);

              transaction = [count,
                            response["chain"][i]["transactions"][j]["recipient_address"],
                            response["chain"][i]["transactions"][j]["sender_address"],
                            response["chain"][i]["transactions"][j]["sender_id"],
                            response["chain"][i]["transactions"][j]["sender_bat"],
                            response["chain"][i]["transactions"][j]["value"],
                            formattedDateTime,plainText,message,
                            response["chain"][i]["block_number"]];

         transactions.push(transaction);

So im trying to decrypt with a private key that the user generates, like this one:

" "

what am I doing wrong? I think that this key format is not supported by JSEncrypt, but i don't know what to do, any help?

Thank you:)

I suspect you are not decrypting with the right algo & key size.

The problem might be in these lines of code.

decrypt.setPrivateKey(priv_key);
var plainText = decrypt.decrypt(valeur);

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