travist / jsencrypt

A zero-dependency Javascript library to perform OpenSSL RSA Encryption, Decryption, and Key Generation.
http://www.travistidwell.com/jsencrypt
Other
6.69k stars 2.02k forks source link

Expand encrypt/decrypt logic #90

Open MonteShaffer opened 8 years ago

MonteShaffer commented 8 years ago

The intent of RSA is that you can encrypt/decrypt with either the public or private key.

The current implementation is only one-way. For example, on a web server, I may want to encrypt data with a private key (e.g., openssl functions in php), and then send the public key with the private data. On the client side, using your jsencrypt.js, I would then want to use the public key to decrypt the private data.

Alternatively, on the client, I may want to encrypt data using the public key, send it to the server, and decrypt on the server with the private key. In this scenario, the client only sees the public key and the server only uses the private key.

This use case is not supported in your functionality. Is this on the road-map?

Below is some sample code showing the interaction of public/private and encrypt/decrypt interchanges using openssl functions in php...


$privateKeyString = <<<PK
-----BEGIN RSA PRIVATE KEY-----
MIIJKAIBAAKCAgEAvx0dtUFju12SSL31o9XEIwtp9eBBziQOcm85ZmrAdwk7C+Vk
j6GStfHUXmMUrD5ckZQvM1hhDiNYRj6dqxoQGhEeXUtVyFAMWGtmnDl+OHDeAN8f
2wv+DMmXN+Du4yfZ14w/6B1A7T3Rz0qRALc5a94ZoP0Q1/QStgD87BRKNumYDbJb
ARD+576E3r9sp5E+/wCDgGd5SCnW7rLbixQfQYX0tlcmnFYjsce6VkeTC4XMQTSC
4gkYS1NgF7CLa8sLITAttbl4r5V9etf9GtbrPADXgjBDeG0Yc7CWlL7F6qAnU4b9
NILRW+xFXa16y4V1EbrwSObwCr011ChuidpXEg3WK2pPaiXsHb9j5uMHljTq1Dgb
uIR29s6lYwQHEfTofzMMyLCztod7t/qDz6YEUST5vigJ3DvREaQ+SNzANCYI1ZFL
4WJJZ8DBjKXq+rWUV9hRQhzsXypYQJ3S6zqrDyk3v5CrCCeXJ5P5fsA/NueRcA6F
a3gbmozr4LFs0EBUPZ+ie1btrykTOJRFOFEFBbt2+kj3zIs6MCf+MVZMnWA1GTAT
wlKENzMzMwguoBBoYsvEH36/omO01qGS2kQzhWqy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Is6MCf+MVZMnWA1GTATwlKENzMzMwguoBBoYsvEH36/omO01qGS2kQz
hWqyPI+302/lO72EZTEhvmjkuoTkKNKD+swvQVFM5OPvg5pZbbGxobXBMAjT7UJj 
kUmKgNbuIys0Lftl7+WQb6MCAwEAAQ==
-----END PUBLIC KEY-----
PK;

// Load private key
$privateKey = openssl_pkey_get_private($privateKeyString);

// Load public key
$publicKey = openssl_pkey_get_public($publicKeyString);

if (!$privateKey) {
    echo "Private key NOT OK\n";
}

if (!$publicKey) {
    echo "Public key NOT OK\n";
}

if (!openssl_private_encrypt($sensitiveData, $encryptedWithPrivate, $privateKey)) {
    echo "Error encrypting with private key\n";
}

if (!openssl_public_encrypt($sensitiveData, $encryptedWithPublic, $publicKey)) {
    echo "Error encrypting with public key\n";
}

if (!openssl_private_decrypt($encryptedWithPublic, $decryptedWithPrivateFromPublic, $privateKey)) {
    echo "Error decrypting with private key what was encrypted with public key\n";
}

if (!openssl_public_decrypt($encryptedWithPrivate, $decryptedWithPublicFromPrivate, $publicKey)) {
    echo "Error decrypting with public key what was encrypted with private key\n";
}

echo "Encrypted with public key: " . base64_encode($encryptedWithPublic) . "\n"; // This is different every time
echo "Encrypted with private key: " . base64_encode($encryptedWithPrivate) . "\n";
echo "Decrypted with private key what was encrypted with public key: " . $decryptedWithPrivateFromPublic . "\n";
echo "Decrypted with public key what was encrypted with private key: " . $decryptedWithPublicFromPrivate . "\n";
barkermn01 commented 7 years ago

Could you please provide the output of that really your trying to prove something so prove it, you have that PHP code can you please then put the output.