Jordy Huijgens
Jordy Huijgens

Reputation: 7

How can I encrypt data with my private key and decrypt with my public key using JSEncrypt?

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);

Upvotes: 0

Views: 1454

Answers (1)

Michaelsoft
Michaelsoft

Reputation: 807

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.

Upvotes: 1

Related Questions