Java Pure digital encryption and decryption instance

Source: Internet
Author: User
Tags decrypt

We all know that when users add information, some more sensitive information, such as the identity card number, mobile phone number, user login password and other information, is not directly in the text into the database. Today, we will illustrate the pure digital Java encryption and decryption technology with a concrete example.


In general, after we get the information from the page to the user, we encrypt it and then save it to the database. When information is needed, the user information after encryption we do not understand, so the corresponding we will use the decryption technology. In fact, the soft examinations encryption and decryption technology has a very comprehensive description, Here we use a relatively simple example to illustrate.


We may get used to encryption at the service layer, which is not too mandatory. Let's take a look at the encryption process here. First, because my password is six digits, so we need to encrypt the six-bit valid number, the code is as follows:

<span style= "White-space:pre" ></span>/** * <p>description: Password encryption </p> * @param Userpasword Pass over the six-digit password * @return The encrypted String * @throws Exception * @date: July 27, 2015 */public string Secretencrypt (string userpasword) thr oWS Exception {          //Use Cipher instance          Cipher Cipher =cipher.getinstance ("AES");                   Get the encrypted key          Secretkey key =keygenerator.getinstance ("AES"). GenerateKey ();                  Initializes the cryptographic operation, passing the encrypted key          Cipher.init (Cipher.encrypt_mode,key);                            The encrypted content is passed in, returning the encrypted binary data          string results =cipher.dofinal (Userpasword.getbytes ()). ToString ();//Returns the encrypted string       return results;    }

Application in specific code:

<span style= "White-space:pre" ></span>/** * <p>description: Save user Basic Info </p> * @param Personbaseinfo User Basic Information entity * @return Boolean, True for add success, FALSE for add failure * @throws Exception * @date: July 27, 2015 */public Boolean Save UserInformation (Userbaseinfo userbaseinfo) throws Exception{boolean result = false;try{//Save user basic information System.out.println ( "User password:" + secretencrypt (Userbaseinfo.getuserpassword ()));//Encrypt the password and place it in the entity to save the Userbaseinfo.setsuserpassword ( Secretencrypt (Userbaseinfo.getuserpassword ()));//Save user Information Userbaseinfoservice.save (userbaseinfo); result = true;} catch (Exception e) {e.printstacktrace ();} return result;}

The user password that is stored in the database is: The second line is the encrypted user password.


Well, the above describes the process of encryption, of course, the process of decryption. You can't say that we now need only to make encryption, no decryption. Yes, there may not be a lot of demand on the page, but encryption and decryption itself is a pair of symbiosis. You just encrypted, and if someone else takes over your project in the future, A look only encryption does not decrypt, no doubt is to others dug a big pit, so remember, do encryption must be decrypted together, even if not now. The decryption code is as follows:

<span style= "FONT-SIZE:18PX; White-space:pre; " ></span><span style= "FONT-SIZE:14PX;" >/** * <p>description: Decryption function </p> * @param userpassword * @return * @throws Exception * @author: Gaoyi NG * @update: * @date: 2015-7-27 */public string Secretdecrypt (String userpassword) throws exception{//Make                 Use Cipher example Cipher Cipher =cipher.getinstance ("AES");          Get the key in the file to decrypt FileInputStream fiskey=new fileinputstream ("Secretkey.key");          ObjectInputStream oiskey =new ObjectInputStream (Fiskey);          Key key = (key) oiskey.readobject ();          Oiskey.close ();                    Fiskey.close ();                    Initial dissolve secret operation, pass the encrypted key cipher.init (Cipher.decrypt_mode,key);          Gets the binary data in the file FileInputStream fisdat=new fileinputstream ("SecretContent.dat");          Get data byte [] src=new byte [fisdat.available ()];          int len =fisdat.read (src);          int total = 0; while (total<Src.length) {Total +=len;          Len=fisdat.read (src,total,src.length-total);        }//performs decryption of String result=cipher.dofinal (SRC). toString (); return result;} </span>

OK, in summary, we put the encryption and decryption are finished, remember what I said above, encryption and decryption itself is a pair of symbiosis, indispensable. So don't take it easy, just encrypt, and get rid of the decryption. Okay, next article we'll cover the div nesting problem.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Pure digital encryption and decryption instance

Related Article

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.