I use encrypt(byte) code for input String and then save encrypt(String) in DB.
I get String encrypt from DB to decrypt it, but I need to cast String to byte without changing because decrypt get just byte.
I used s.getBytes(); but it changed it,
I need some code to cast string to byte without changing the String.
Thank you so much.
解决方案
getBytes() doesn't change the string, it encodes string into a sequence of bytes using the platform's default charset.
In order to print the array of bytes as a String value,
String s = new String(bytes);
Edit:
it seems as you want to print the string as bytes, for which you can use
Arrays.toString(bytes)
See this code,
String yourString = "This is an example text";
byte[] bytes = yourString.getBytes();
String decryptedString = new String(bytes);
System.out.println("Original String from bytes: " + decryptedString);
System.out.println("String represented as bytes : " + Arrays.toString(bytes));
Output,
Original String from bytes: This is an example text
String represented as bytes : [84, 104, 105, 115, 32, 105, 115, 32, 97, 110, 32, 101, 120, 97, 109, 112, 108, 101, 32, 116, 101, 120, 116]
本文探讨了在加密过程中将字符串转换为字节,然后存储到数据库中,以及如何从数据库获取加密字符串并进行解密。重点在于如何在不改变原始字符串的情况下,将字符串安全地转换回字节。示例代码展示了使用默认字符集进行转换,并通过Arrays.toString()方法打印字节数组。
1537

被折叠的 条评论
为什么被折叠?



