简体   繁体   中英

Converting string to byte array

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

我只是查看了屏幕右侧的“相关”部分,然后将Java字符串转换为字节数组。

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM