简体   繁体   English

Java字节到字符串给出奇怪的结果?

[英]Java Byte to String giving weird result?

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(). f是字节数组,而16是读取由SecureRandom()生成的IV的16个字节。 (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 我得到以下输出: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 如果有帮助,我在此处包括了完整的类代码: 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. 我真的很想知道上面的代码在做什么,所以我可以将其移植到PHP,而不是@ Java。

Thanks in advance. 提前致谢。

Your output 7ª7ég}ÇÞFf0 makes sense to me. 您的输出7ª7ég}ÇÞFf0对我来说很有意义。

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. 您正在使用字符集: ISO-8859-1 ,因此字节将被解码为它们在该字符集中映射到的字符。

Your byte array is created using base 10, and java bytes are signed. 您的字节数组使用10为基数创建,并且对Java字节进行了签名。 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: 根据ISO-8859-1字符集,这些值映射到以下内容:

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. (nil)字符未在您的字符串中呈现,因为该字符集没有对应值的字形。 And for the character SHY , I will assume again there is no glyph (while the standard indicates there actually should be). 对于字符SHY ,我将再次假设没有字形(而标准指示实际上应该有)。

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. 您不应该期望数据是人类可读的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM