Java Security--base64

Source: Internet
Author: User

This topic is mainly about Java security, it should be considered a hodgepodge, but not lack of practicality, as a summary, when used can be used as a reference.

1, using BASE64 plus decryption

In Java encryption technology, BASE64 is the simplest and most basic encryption technology. See the following example:

Importjava.io.IOException;ImportDecoder.base64decoder;ImportDecoder.base64encoder; Public classBase64test { Public Static voidMain (string[] args)throwsioexception{String Data= "I am a data that needs to be encrypted"; System.out.println ("Before encryption:" +data); String data1=encryptBASE64 (Data.getbytes ()); System.out.println ("After encryption:" +data1); byte[] Data2 =decryptBASE64 (DATA1); System.out.println ("After decryption:" +NewString (data2)); }    Private StaticString encryptBASE64 (byte[] data) {      return(NewBase64encoder ()). Encodebuffer (data); }    Private Static byte[] decryptBASE64 (String data)throwsioexception{return(NewBase64decoder ()). Decodebuffer (data); }}

Results:

before encryption: I am a need to encrypt the data after encryption: Ztlkx9k7upbq6nkqvnpd3lxeyv2+3q== after decryption: I am a data that needs to be encrypted

The number of strings encrypted with BASE64 is a multiple of 4, a character of two bytes, that is, the string after BASE64 encryption must be a multiple of 8 bytes, if not enough, you need to fill in the back "=" number, so many times you can see the use of BASE64 encrypted characters with "=" end.

Strings that are encrypted with BASE64 will be inserted with a carriage return every 76 characters, so even if the plaintext is long, the printed ciphertext will not be displayed on one line. However, when decrypting, these carriage returns appear insignificant, even if the ciphertext does not contain a carriage return character can also be decrypted normally.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.