dongyou8087 2016-01-05 02:48
浏览 37
已采纳

Java Byte to String给出了奇怪的结果?

I have this code:

 private static c e;
  private static byte[] f = { 55, -86, -102, 55, -23, 26, -83, 103, 125, -57, -110, -34, 70, 102, 48, -103 };
  private String a;
  private SecureRandom b;
  private int c;
  private byte[] d;

  public c(String paramString, SecureRandom paramSecureRandom)
  {
    this.a = paramString;
    this.b = paramSecureRandom;
  }

  public static c a()
  {
    if (e == null)
    {
      e = new c("AES/CBC/PKCS7Padding", new SecureRandom());
      e.a(f, 16);
    }
    return e;
  }

With f being the array of bytes and 16 to do with reading 16 bytes of the IV generated with SecureRandom(). (atleast I assume that's what it is doing?) However when I use this:

byte[] byteArray = { 55, -86, -102, 55, -23, 26, -83, 103, 125, -57, -110, -34, 70, 102, 48, -103 };

String value = new String(byteArray, "ISO-8859-1");
    System.out.println(value);

I get this output: 7ª7é­g}ÇÞFf0

I'm attempting to work out how this app i've got generates the encryptionkey used for encrypting/decrypting... that result above surely can't be anything right? Am I completely on the wrong track here?

I've included the full class code here incase it helps: http://pastie.org/private/5fhp9yqknzoansd1vc0xfg

Would really love to know what the code above is actually doing so I can port it to PHP, not too good @ Java.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doucong3048 2016-01-05 03:09
    关注

    Your output 7ª7é­g}ÇÞFf0 makes sense to me.

    You are using the character set: ISO-8859-1, and thus the bytes will be decoded to the characters they are mapped to in that character set.

    Your byte array is created using base 10, and java bytes are signed. This means your byte array has the following hexadecimal values (in order):

    37, AA, 9A, 37, E9, 1A, AD, 67, 7D, C7, 92, DE, 46, 66, 30, 99
    

    According to the ISO-8859-1 character set, these values map to the following:

    7, ª, (nil), 7, é, (nil), SHY, g, }, Ç, (nil), Þ, F, f, 0, (nil)
    

    Which is pretty close to what your string is actually. The (nil) characters are not rendered in your string because the character set does not have glyphs for the corresponding values. And for the character SHY, I will assume again there is no glyph (while the standard indicates there actually should be).

    Your output seems correct to me! :)

    Remember, your encryption key is just a sequence of bytes. You shouldn't expect the data to be human-readable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示