views:

65

answers:

2

I'm encrypting a string in java,

encrypted data is only question marks

for eg .. plaintext : jjkkjlkljkj

      encrypted text: ???????????

While i'm decrypting i'm getting jjkkjlkljkj exact answer ...

+5  A: 

Yes, it's because you can't print the strings that are resulting from the encryption.

Note that saving the encrypted result in a string will possibly result in loss of the data, so don't do that. Take it as a byte array, and convert it to a displayable format, like Base64 or just simple Hex.

Noon Silk
A: 

The root cause of the problem is the way how you presented the encrtpyed data. The character encoding used doesn't recognize those characters as one of its charset, nor does have a suitable glyph (font) for those characters. Even then, when you used the "correct" character encoding (try to display it with UTF-8) it would not have been human readable. That's the whole point of encryption, else it would be easy for a human to decrypt them.

I suppose that you've it in flavor of a byte[] and are trying to convert it to String using new String(bytearray). If your purpose is to transfer it as a String instead of a byte[], then you should rather use either Apache Commons Codec Base64#encodeBase64String() or to convert the byte[] to a hexstring like follows:

StringBuilder hex = new StringBuilder(bytea.length * 2);
for (byte b : bytea) {
    if ((b & 0xff) < 0x10) hex.append("0");
    hex.append(Integer.toHexString(b & 0xff));
}
String hexString = hex.toString();
BalusC