0

For a homework assignement I need to encrypt data with a private key and decrypt it with a public key. I am using JSEncrypt and it is encrypting my data, but when I try do decrypt it, it is returning false. It is an assignement about digital signatures.

I tried to switch it around and encrypt is with my public key, this actually worked, but I don't want to do it this way.

(I am encrypting in a different function, not in the same function as where I am decrypting)

//encrypting
var encrypt = new JSEncrypt({
    default_key_size: 1024,
    default_public_exponent:"010001"
});
this.hashedvalue = sha256(this.selectedPost.value);
encrypt.setKey(val.privateKey);
var encoded = encrypt.encrypt(this.hashedvalue);

//decrypting
var decrypt = new JSEncrypt({
 default_key_size: 1024,
 default_public_exponent:"010001"
});
decrypt.setKey(val.postUser.publicKey);
var hashedvalue = sha256(val.value);
var decoded = decrypt.decrypt(val.encryptedvalue);
//returns false
console.log(decoded);
console.log(hashedvalue);
  • ```I tried to switch it around and encrypt is with my public key, this actually worked, but I don't want to do it this way.``` You don't want to do it in the way it's supposed to be done? – nll_ptr Aug 20 '19 at 19:09
  • no, I don't I want to encrypt with my private key and decrypt with my public key. Every user has a certificate, and every user needs the ability to check witch user signed the message by checking the certificate. – Jordy Huijgens Aug 20 '19 at 19:17
  • You don't *encrypt* with the *private* key. You might *sign* something with a private key, but that's different from encrypting it. There's no value in encrypting a payload that *anyone* can decrypt. –  Aug 20 '19 at 19:18
  • the 'messages' are the things I am trying to encrypt and decrypt. – Jordy Huijgens Aug 20 '19 at 19:18
  • Ow @Amy thank you ! That was my mistake. – Jordy Huijgens Aug 20 '19 at 19:21
  • The way private/public key cryptography works is anyone can encrypt a message using the recipient's public key (called "public" because everyone knows it), but only the recipient with their private key can decrypt it. The opposite is true for signing: only one person can sign a message, but anyone with the public key can verify the signature. –  Aug 20 '19 at 19:22
  • @Amy thank you for your help! means a lot! – Jordy Huijgens Aug 20 '19 at 20:00

1 Answers1

1

It's a bit late to answer to your question, but I leave here a reference for who wants JSEncrypt to encrypt with Private Key and decrypt with Public Key.

I forked and I'm maintaining a bit this library: https://github.com/michaeldisaro/jsencrypt.

I added private key encryption, and fixed padding to work with BouncyCastle library.

I successfully can decrypt on .NETCore backend what I encrypt with Private Key on client side.

Maybe it will come in hands to who needs it.

Michaelsoft
  • 777
  • 5
  • 18