I can't easily tell whether this is all that's wrong, but this is definitely wrong:
byte[] encryptedData = encryptData("Confidential data"); //String from user
String s=encryptedData.toString();//String input to decrypt From user
byte[] bb=s.getBytes();
Just decrypt encryptedData
instead of of bb
. Your s
value is basically useless, because you've called toString()
on a byte[]
, which won't give you what you're apparently expecting. The value of s
will be something like "[B@15db9742"
because arrays don't override toString()
in Java.
If you really want to turn arbitrary binary data (such as the result of encryption) into text, use base64 instead. Encode the result of encryption using base64 to get a string, and then later base64-decode it from the string to a byte array, then decrypt that byte array.
Oh, and I'd strongly recommend against using String(byte[])
or String.getBytes()
too - always use the overloads which take a character encoding instead.