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