字节到字符串,反之亦然 [英] byte to string and vice versa

查看:157
本文介绍了字节到字符串,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要转换随机生成的盐值并将其存储在数据库中。要将其存储在数据库中,我将其转换为字符串。然后为了检索原始值,我将其转换回字节。但是这两个值都不匹配。我试过UTF-8,UTF-16,BASE64Encoder。

  SecureRandom ranGen = new SecureRandom(); 
byte [] aesKey = new byte [16]; // 16 bytes = 128 bits
ranGen.nextBytes(aesKey);
System.out.println(aesKey);

String a = new String(aesKey,UTF-16);
byte [] b = new byte [16];
b = a.getBytes(UTF-16);
System.out.println(b);

输出以上代码(执行2次):

  [B @ 11563ff 
[B @ 1581593

  [B @ 170888e 
[B @ 11563ff


解决方案

如上面的代码所示,打印 aesKey 然后 b ,您实际打印的是数组对象的 toString 方法的输出,它只是默认的Object toString 方法。所以我看不到你可以期待他们是一样的。



如果你真的想检查它们是一样的,你应该逐个比较它们。 / p>

根据您在DB中将字节[]作为字符串存储的实际问题,最好的办法是将Base64编码。我建议为此使用Apache Commons Codec库。请参阅用户指南



编辑:



使用您引用的BASE64Encode和BASE64Decoder,代码将如下所示:

  SecureRandom ranGen = new SecureRandom(); 
byte [] aesKey = new byte [16]; // 16 bytes = 128 bits
ranGen.nextBytes(aesKey);
String a = new BASE64Encoder()。encode(aesKey);

System.out.println(a);

byte [] b = new BASE64Decoder()。decodeBuffer(a);
System.out.println(new BASE64Encoder()。encode(b)); (int i = 0; i< aesKey.length; i ++){
System.out.println(aesKey [i] ++ b [i]);


}

在这里,我也逐个循环了字节,以显示它们是确实相等。


I need to convert a salt value randomly generated and store it in the database. To store it in the database I converted it to a string. Then for retrieving the original value, I convert it back to byte. But both value are not matching. I have tried "UTF-8","UTF-16", BASE64Encoder.

SecureRandom ranGen = new SecureRandom();
byte[] aesKey = new byte[16]; // 16 bytes = 128 bits
ranGen.nextBytes(aesKey);   
System.out.println(aesKey);

String a=new String(aesKey,"UTF-16");
byte[] b=new byte[16];
b=a.getBytes("UTF-16");
System.out.println(b);

Outputs for the above code(Executed it 2 times):

[B@11563ff
[B@1581593

and

[B@170888e
[B@11563ff

解决方案

As your code is written above, printing aesKey and then b, what you are actually printing is the output of the toString method for an array object, which is just the default Object toString method. So I don't see how you can expect them to be the same.

If you really want to check they are the same you should compare them byte by byte.

In terms of your actual question regarding storing a byte[] as a String in the DB, your best bet is to Base64 encode it. I would suggest using the Apache Commons Codec library for this. See the user guide.

EDIT:

Using the BASE64Encode and BASE64Decoder you have referred to, the code would be like this:

    SecureRandom ranGen = new SecureRandom();
    byte[] aesKey = new byte[16]; // 16 bytes = 128 bits
    ranGen.nextBytes(aesKey);
    String a = new BASE64Encoder().encode(aesKey);

    System.out.println(a);

    byte[] b = new BASE64Decoder().decodeBuffer(a);
    System.out.println(new BASE64Encoder().encode(b));

    for (int i = 0; i < aesKey.length; i++) {
        System.out.println(aesKey[i] + " " + b[i]);
    }

Here, I have also looped through the bytes individually, to show that they are indeed equal.

这篇关于字节到字符串,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆