Jose
Jose

Reputation: 3558

how can one Unit test a non deterministic encryption function?

I am trying to reimplement a non deterministic encryption function from javascript to java. I noticed however that the output of the function is different every time. However, whenever I decrypt the output, it always returns the original text.

For example:

word: hello

encrypted: ?HX?631ffe50353ddda632a7e8fa11136d6ffaa584eb43b34c96005b6256f9dc 4121c7a8545d79887b900672e5870d702441?H

decrypted: hello

word: hello

encrypted: ?HX?dfea4d1d30ebd5fc871c7c92d0230baf9e5298b19c3cfa0770fe2d2035f8 dad0116f2963b115c85c9d4725be505fca54?H

decrypted: hello

and so on....

To unit test this, one way I can think of is to also implement the decrypt function in java and then decrypt the output of the encryption function. If the decryption yields the original text then the encryption is correct.

For example:

encrypted = Encrypt_text(word);
assertEquals(word,Decrypt_text(encrypted);

Any other suggestions...?

Upvotes: 4

Views: 738

Answers (2)

Phil Willoughby
Phil Willoughby

Reputation: 1734

Better to use the original decrypt function to validate your encrypt function, rather than a new decrypt routine. If you write both and use them to test each other you will have validated that the data round-trips correctly but you will not have validated the intermediate result.

You could use Mozilla Rhino to run the Javascript decrypt routine: http://www.mozilla.org/rhino/

Upvotes: 2

Boris Pavlović
Boris Pavlović

Reputation: 64640

If you are not interested in the encrypted form then the assertions are just perfect.

Upvotes: 1

Related Questions