2
votes

Hello I'm using a javascript RSA encryption process and it's working :

<!DOCTYPE html>
    <html lang="en">
            <head>
            <title></title>
              <script src="jquery-1.7.2.min.js" type="text/javascript"></script>
              <script type="text/javascript" src="base64.js"></script>
              <script type="text/javascript" src="asn1.js"></script>
              <script type="text/javascript" src="jsbn.js"></script>
              <script type="text/javascript" src="prng4.js"></script>
              <script type="text/javascript" src="rng.js"></script>
              <script type="text/javascript" src="rsa.js"></script>
              <script type="text/javascript" src="pemconvert.js"></script>
              <script>
                    function loadpubKey(){
                            var pubkey = "-----BEGIN PUBLIC KEY-----\nMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAPBYvSszUjI5psKM958bHTDb+9NONv3X\nWVIGLR9/Al4j8/mqSDMhwFuu9oEyXl80+Ga2cohEQPpWyxzg3/tfGDUCAwEAAQ==\n-----END PUBLIC KEY-----";
                            $("#publicKey").html(pubkey);
                        }
                    function RSAEncryptData()
                        {
                            var Message = $("#Message_to_Encrypt").val();
                            var pubkey = pem_import($("#publicKey").val());             
                            $("#RSAMessageEncrypted").html(pubkey.encrypt(Message));
                        }
            </script>
            </head>
        <body onload="loadpubKey();">
        <h1>RSA Encryption/Decryption using library From :https://github.com/tao-software/jsrsa</h1>
        <form>
                <strong>Public key         :</strong> <textarea rows="3" cols="100"  id="publicKey"></textarea><br>
                <strong>Message to RSA encrypt :</strong> <textarea rows="3" cols="100"  id="Message_to_Encrypt"></textarea><br>
                <input type="button" value="RSA Encrypt" onclick="RSAEncryptData();">
                <strong>RSA encrypted Message :</strong> <textarea rows="3" cols="100"  id="RSAMessageEncrypted"></textarea><br>
        </form> 
        </body>
    </html>

Each time I hit "encrypt" button using the same message to encrypt I get a new different encrypted value. What can I do to make this encryption giving the same encrypted result for the same message used?

Actually what I want to do is to encrypt some phone numbers with a public key using RSA then store them in a table after that I will encrypt any phone number that I'll get through a process with the same public key in RSA and look in the table if there is a match.

With the script above I will never find a match even if I'm encrypting the same phone number more than once that's why I want the result to be the same when encrypting the same phone number. Any idea is welcome.

Thank you in advance.

2
It is supposed to be different every time. That is what the pkcs#1 block type 2 padding does, for security purposes. Why do you want it to be the same?President James K. Polk
Hi, Thanks for replying, Please I need it to give the same result when using the same message to encrypt because I want to be able to make a comparaison later between my encrypted massages (don't worry about this) I heard that it requires a SALT or an RSA 0 padding but I'm confused I don't know how to make it do what I want.OussamaLord

2 Answers

1
votes

As Greg states in his comment, the randomness in the PKCS #1 padding will cause your encrypted result to vary each time.

Based on your use case, I would suggest you store a hash of the phone number alongside the encrypted data. You can then perform a quick lookup based on the hash. You may wish to salt your phone numbers prior to hashing.

Bear in mind that hashes are not guaranteed to be unique, so there is a teeny-tiny chance you'll get two hashes that clash.

-1
votes

It sounds like you want to sign a message with your private key and have other verify the signature with your public key. The libraries you have linked to provide an RSASign and RSAVerify methods, possibly under the sign and verify methods of an RSAKey instance. I'm not a JS programmer so all I can say is that's what it looks like.