search

Home  >  Q&A  >  body text

java - 为什么des加密后输出乱码?

大家讲道理大家讲道理2770 days ago1102

reply all(8)I'll reply

  • PHPz

    PHPz2017-04-18 10:11:45

    Because it is no longer UTF-8 string data, dear. You encrypted it. DES generates unreadable binary data. If you force new String to interpret it according to UTF-8 encoding, of course it will be garbled. If it can be read directly, what's the point of encrypting it?
    Because most data encrypted based on mathematical algorithms generate binary blocks or stream data. Only after decryption can the original text be read.
    If you want the encrypted data to be stored as text or HTTP transmission, you can encode the data with BASE64. After encoding, it will be an ASCII string and will not be garbled (of course, humans still can't understand it).

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:11:45

    After encryption, it is a byte array. Not all byte arrays can be converted into strings through new String()

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:11:45

    This may be because you do not understand the difference between memory byte data stream and string.
    If you have used a text editor to display the data in hexadecimal, then you will not have this question.
    Why can String display a string? , that’s because the memory data conforms to certain encoding rules, such as UTF-8 encoding. But generally the data after des encryption is a complete memory data stream, which does not comply with the string encoding rules, so you force When you use string encoding rules to parse it, it will become "garbled code".

    But if the purpose of encryption is not correct, then people can’t understand it?

    It’s just gibberish

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:11:45

    After encryption, it should be unreadable.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:11:45

    There is a similar article here https://segmentfault.com/q/10...

    reply
    0
  • PHPz

    PHPz2017-04-18 10:11:45

    Why do you think it’s not gibberish?

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:11:45

    After encryption, it is usually a byte array, which can be converted into a hexadecimal string.

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:11:45

    After encryption, you can use Base64,

    reply
    0
  • Cancelreply