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 !!

  • possible duplicate http://stackoverflow.com/questions/6483181/javax-crypto-badpaddingexception-data-must-start-with-zero-exception – Luca May 03 '12 at 12:47
  • 2
    I'm pretty sure your encrypted text is _not_ `[B@d7eed7` (or at least not in the way you think). – mitchnull May 03 '12 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. –  May 03 '12 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. – President James K. Polk May 04 '12 at 00:57

3 Answers3

2

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

Community
  • 1
  • 1
João Fernandes
  • 1,101
  • 5
  • 11
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

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 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 –  May 03 '12 at 12:56
  • @coders_zone: He is saying "Use `new String(array, encoding)` and not `array.toString()`". – NPE May 03 '12 at 12:57
  • then, how can i decrypt the same array to the plain text. –  May 03 '12 at 13:00
  • i was trying `new String(encryptarr,"Base64")`, then it showing exception `UnsupportedEncodeingException: Base64` –  May 03 '12 at 13:16
  • Base64 is not a character encoding. Do it like this: encode your array into base64 and use ASCII character encoding for the output string. – João Fernandes May 03 '12 at 13:45
  • can you please give any example..?? –  May 03 '12 at 13:49
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!

Community
  • 1
  • 1
pelumi
  • 1,530
  • 12
  • 21