Java Pure digital encryption and decryption instance

Source: Internet
Author: User
Tags decrypt

We all know that when the user joins the information, some sensitive information, such as the ID number, mobile phone number, the user's login password and other information, is not directly into the database. Today we will be a detailed example to illustrate the pure digital Java encryption and decryption technology.


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 required, the user information after the encryption we do not understand, so the corresponding we will use the decryption technology. In fact, soft examinations is a very comprehensive description of encryption and decryption technology. 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, since my password is a six-digit number, we need to encrypt the six-bit valid numbers, such as the following:

<span style= "White-space:pre" ></span>/** * <p>description:password Encryption </p> * @param Userpasword Six digits Password * @return Encrypted String * @throws Exception * @date: July 27, 2015 */public string Secretencrypt (Strin G Userpasword) throws 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 detailed code:

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

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

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

Well, the above describes the process of encryption, of course, the process of decryption. You can't say that we need just to encrypt, not decrypt. Yes, there may not be a lot of demand on the temporary page, but encryption and decryption itself is a pair of symbiosis. You're just encrypting, assuming that someone else takes over your project in the future, A look at the only encryption does not decrypt, is undoubtedly to others dug a big pit, so remember, do encryption must be decrypted together, even if it is not used today. Decrypt code such as the following:

<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);          Get 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);        }//Run decrypt 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. OK, next article let's talk about div nesting issues.



Java Pure digital encryption and decryption instance

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.