We all know that when users add information, some sensitive information, such as ID card numbers, mobile phone numbers, user login passwords and other information. Take a pure digital Java encryption and decryption technology.
Generally, after obtaining the information added by the user from the page, the encryption is encrypted and then stored in the database. When the comparison information needs to be compared, we cannot understand the user information after the encryption, so we must use the decryption technology. In fact The encryption decryption technology has been comprehensively explained. Here we use a relatively simple example to explain it.
We may be accustomed to encryption on the Service layer. This is not too compulsory. Let's take a closer look at the process of encryption. Let's explain it first, because my password is six effective numbers, so we need to effective these six places to be effective The number is encrypted, the code is as follows:
<span style = "white-spice: pre"> </span>/*** <p> description: Code encryption </p>* @param userpaSword* @Retant-encrypted string after encrypted string * @throws Exception * @Date: July 27, 2015 */Public String SecretonCrypt (String UserPasword) Throws Exception {// Use Cipher Cipher.get Instance ("AES"); // Get the encrypted key SecretKey key =KeyGenerator.getInstance("AES").generateKey(); //初始化加密操作,传递加密的钥匙cipher.init(Cipher.ENCRYPT_MODE,key); //将加密的内容传递进去,返回加密后的Binary data string results = cipher.dofinal (userpaSword.getBytes ()). Tostring (); // Return the encrypted strings Return Results;}Application in specific code:
<span style = "white-point: pre"> </span>/*** <p> Description: Save the user's basic information </p>* @param PersonbaseInfo user basic information entity* @Return Boolean, TRUE represents adds to add Success, False representative adds failure* @throws exception* @date: July 27, 2015*/ Public Boolean SaveuseuserinFormation (UserBaseInfo UserBaseinfo) Throws Exception {Boolean ult = false; try {// save user basic information system.out.println ("User Password:" + SecretonCrypt (UserBaseInfo.getUserPassword ())); // encrypted the password, and then placed in the entity to save userBaseInfo.SetSuserpassword (userBaseinfo.getu.getu serpassword ())); // Save user information userBaseInfoservice .save (userBaseinfo); Result = true;} Catch (Exception E) {e.printstacktrace ();} Return Result;}The user password stored in the database is: the second line is the encrypted user password.
Well, the process of encryption is introduced above. Of course, the process of tightness is not understood. Can't you say that our needs are only encrypted now and there is no decryption. Yes, there may not be so many needs on the page, but the encryption and decryption itself are a pair of pairs itself Symbiosis. You just made an encryption. If someone takes over your project in the future, only the encryption is not decrypted, and it is undoubtedly digging a large pit for others, so remember that when you are encrypted, you must do it together. Even if it is not used now. The decryption code is as follows:
<span style = "font-siZe: 18px; white-spie:"> </span> <span style = "font-size: 14px;">/*** <p> description: decryption function </p > * @param UserPassword * @Return * @throws Exception * @AnceHor: Gaoying * @UPDATE: * @Date: 2015-7-27 */ Public StringCretDeCrypt (String Password) Throws Exception {// Use CIPHER instance CIPHER CIPHER = Cipher.getInstance ("AES"); // Get the key in the file to decrypt FileInputStream Fiskey = New FileInputStream ("Secretkey.key"); UTSTREAM (Fiskey); key key = (key) oiskey.readObject (); oiskey.close (); Fiskey.close (); // Initially decompose secret operations, pass the encrypted key cipher.init (cipher.DeCrypt_mode, key); // Get the binary data in the file FileInputStream Fisdat = NEW F ILEINPUTSTREAM ( "SecretContent.dat"); // Get the data byte [] src = new byte [fisdat.available ()]; int len = fisdat.read (src); int Total = 0; about (total <src.Length) { Total += Len; LEN = FISDAT.READ (src, Total, SRC.Length- Total);} // execute decryption string res) an>Well, in summary, we finished the encryption and decryption. Remember what I said above. The encryption and decryption itself is a pair of symbiosis. Decryage and throw away.
The above is all the contents of this article. I hope it will be helpful to everyone's learning.