[英]Converting string to byte array
I'm using RSA encryption for converting simpletext to encrypted form. 我正在使用RSA加密将简单文本转换为加密形式。
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 我正在获取javax.crypto.BadPaddingException:数据必须以零开头
need help !! 需要帮忙 !!
我只是查看了屏幕右侧的“相关”部分,然后将Java字符串转换为字节数组。
The problem is that [B@d7eed7
is not the encrypted text. 问题是[B@d7eed7
不是加密的文本。 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 有关更多信息,请参见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! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.