Java AES Encrypt

Source: Internet
Author: User
Tags decrypt

This time using AES symmetric encryption algorithm.

The reason for using AES is that you can restore an encrypted string. The procedure is as follows:

     Public Staticstring Encode (string content) {Keygenerator keyGen; Try{KeyGen= Keygenerator.getinstance ("AES"); Keygen.init (128,NewSecureRandom (SALT)); Cipher Cipher= Cipher.getinstance ("aes/ecb/pkcs5padding");
Pit is here key key=NewSecretkeyspec (Keygen.getkey (). getencoded (), "AES"); Cipher.init (Cipher.encrypt_mode, key); byte[] bytes = cipher.dofinal (content.getbytes ("Utf-8")); StringBuilder SB=NewStringBuilder (); for(inti=0;i<bytes.length;i++) {sb.append (Bytes[i]+","); } sb.setlength (Sb.length ()-1); returnsb.tostring (); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); } Catch(nosuchpaddingexception e) {e.printstacktrace (); } Catch(InvalidKeyException e) {e.printstacktrace (); } Catch(illegalblocksizeexception e) {e.printstacktrace (); } Catch(badpaddingexception e) {e.printstacktrace (); } Catch(unsupportedencodingexception e) {e.printstacktrace (); } return NULL; }

This allows for encryption.

The problem is in this line of the pit.

In general, we encrypt the user name and password of DB connection, then decrypt it on other machines and then connect.

The encrypted string I have used locally to operate the program cannot be decode on the server.

After troubleshooting, the problem occurs in

Keygen.getkey (). getencoded ()  type is byte[]
Because the server and my native generated expressions are completely different. Causes the server to be unable to decrypt the locally generated string.
This is a bit of a sore egg. Then, switch to a fixed byte[]
The server can parse the locally generated string.

Java AES Encrypt

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.