samedi 20 juin 2015

To 'if' or not to 'if'

I have written a code and I can't explain why is it not behaving the way it should. I know this sounds silly but it is not.

            boolean verify = EDV.verifySignature(signature, cipherText,
                    SERVER_PUBLIC_KEY);
            out.println("Validity " + verify);
            if (verify) {
                // //message is authentic
                String decryptedMessage = EDV.decrypt(cipherText,
                        SERVER_PRIVATE_KEY);
                out.println("Message : " + decryptedMessage);
            }else
            {
                out.println("Signature did not match");
            }

This is a simple code that verifies signature and then decrypts the message if the signature is valid(verify is true)

The output of this code is this :

Validity false
Signature did not match

The message decrypts just fine.

The problem is that the signature should verify (I have checked the signature, cipherText, and key over n times). Here's the kick.

The almost same code

                boolean verify = EDV.verifySignature(signature, cipherText,
                        SERVER_PUBLIC_KEY);
                out.println("Validity " + verify);
//              if (verify) {
                    // //message is authentic
                    String decryptedMessage = EDV.decrypt(cipherText,
                            SERVER_PRIVATE_KEY);
                    out.println("Message : " + decryptedMessage);
//              }else
//              {
//                  out.println("Signature did not match");
//              }

and Voila!, output is as I wanted it to be and as it should be.

Validity true
Message : This is a sample Text

And, the first code works just fine on Eclipse, but running it on JAVA servlet is the only time I get this. Also, the first time I run the code#2 it gives false but after that it gives true.

I can't seem to explain the reason.

LINKS

Code#1

Code#2(Run this code, it gives false validity refresh it and it turns true)

Aucun commentaire:

Enregistrer un commentaire