2

I'm using RSA encryption for converting simpletext to encrypted form.

my plain text is : hello

encrypted text : [B@d7eed7

Now, how to convert encrypted text into simple plain text i'm using following code

KeyPairGenerator keygenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keygenerator.initialize(1024, random);

KeyPair keypair = keygenerator.generateKeyPair();
PrivateKey privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();
Cipher cipher = Cipher.getInstance("RSA");

String arrayStr = "[b@d7eed7";
byte ciphertext = arrayStr.getBytes();
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cleartext1 = cipher.doFinal(ciphertext);
System.out.println("the decrypted cleartext is: " + new String(cleartext1));

i'm getting javax.crypto.BadPaddingException: Data must start with zero

need help !!

4
  • possible duplicate stackoverflow.com/questions/6483181/… Commented May 3, 2012 at 12:47
  • 2
    I'm pretty sure your encrypted text is not [B@d7eed7 (or at least not in the way you think). Commented May 3, 2012 at 12:52
  • it is not for encryption, encryption part have been already done. Now, i've the encrypted text [b@77eed7, which is in string format, now i want to return it back to the original text. Commented May 3, 2012 at 12:54
  • There is a lot of wrongness potential in this question, even before the wrong assumption that byte [].toString() is meaningful. You should not even be trying to take the byte [] result of encryption and converting it to character string unless you need to transfer it through a character-only channel. In that case, you should using something like base64 encoding. Commented May 4, 2012 at 0:57

3 Answers 3

2

The problem is that [B@d7eed7 is not the encrypted text. It simply shows the type and the address of the byte array, not its contents.

For more information, see https://stackoverflow.com/a/5500020/367273

Sign up to request clarification or add additional context in comments.

6 Comments

i've already visited that link, but it hard for me to understand. I' mean i didn't get what he/she is saying for the solution
@coders_zone: He is saying "Use new String(array, encoding) and not array.toString()".
then, how can i decrypt the same array to the plain text.
i was trying new String(encryptarr,"Base64"), then it showing exception UnsupportedEncodeingException: Base64
Base64 is not a character encoding. Do it like this: encode your array into base64 and use ASCII character encoding for the output string.
|
2

I just looked to the "Related" part on the right side of the screen and... Convert Java string to byte array

Comments

1

to convert string to byte array you can use the following:

String source = "0123456789";
byte[] byteArray = source.getBytes("specify encoding alongside endianess");// e.g "UTF-16LE", "UTF-16"..

For more info you can check here, here and here.

Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.