在java中,将SecureRandom字节转换为字符串不会出现编码问题
我使用以下代码snippnet在Java 8中将字节转换为字符串:在java中,将SecureRandom字节转换为字符串不会出现编码问题,java,Java,我使用以下代码snippnet在Java 8中将字节转换为字符串: SecureRandom random = new SecureRandom(); byte bytes[] = new byte[20]; random.nextBytes(bytes); String salt = new String(bytes, StandardCharsets.UTF_8); 但是输出盐是这样的: 0����8���t��.1�A 是否可以将其设置为平面字符串或从url参数传递?您可以将其
SecureRandom random = new SecureRandom();
byte bytes[] = new byte[20];
random.nextBytes(bytes);
String salt = new String(bytes, StandardCharsets.UTF_8);
但是输出盐是这样的:
0����8���t��.1�A
是否可以将其设置为平面字符串或从url参数传递?您可以将其编码为Base 64:
SecureRandom random = new SecureRandom();
byte bytes[] = new byte[20];
random.nextBytes(bytes);
byte[] encoded = Base64.getEncoder().encode(bytes);
System.out.println(new String(encoded, StandardCharsets.UTF_8));
输出:
DBL53Wdublx4iN7gU/+5jNy7mes=
您可以将其编码为64进制:
SecureRandom random = new SecureRandom();
byte bytes[] = new byte[20];
random.nextBytes(bytes);
byte[] encoded = Base64.getEncoder().encode(bytes);
System.out.println(new String(encoded, StandardCharsets.UTF_8));
输出:
DBL53Wdublx4iN7gU/+5jNy7mes=
把它编码成64进制或十六进制怎么样?八进制?十进制?按照你的提示,我现在使用base64@SweeperHow将其编码为base64?或十六进制?八进制?Decimal?按照您的提示,我现在使用base64@SweeperNote有一个在rfc4648中定义的base64的“URL安全”变体,JWS/JWE/JWT显著使用,并且在
java.util.base64
中实现,这会更改字母表以避免URL或x-www-form-urlencoded中的百分比编码。注意,rfc4648中定义了base64的“URL安全”变体,JWS/JWE/JWT使用该变体,并在java.util.base64
中实现,它会更改字母表以避免URL或x-www-form-urlencoded中的百分比编码。