Example to explain Java Pure Digital encryption and decryption _java

Source: Internet
Author: User
Tags decrypt

We all know that when users add information, some of the more sensitive information, such as ID card number, mobile phone number, user login password and other information, is not directly clear to the database. Today we take a concrete example to illustrate the pure digital Java encryption and decryption technology.

After we get the information from the page to the user, we encrypt it and then save it to the database. Need to compare the information, encrypted after the user information we do not understand, so the corresponding we will use decryption technology. In fact, soft exam on the encryption and decryption technology has a very comprehensive description, Here we will use a relatively simple example to illustrate.

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

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

Application in specific code:

<span style= "White-space:pre" >  </span>/** 
   * <p>description: Save user basic information </p> 
   * param personbaseinfo User Profile entity 
   * @return Boolean, true to add success, false to add failure 
   * @throws Exception 
   * @date: July 27, 2015 
   * 
  /public boolean saveuserinformation (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 
      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 saved to the database is: The second line is the encrypted user password.

OK, the encryption process is described above, of course, the decryption process. You can't say we need it now. Yes, there is not much demand on the page, but encryption and decryption are a couple of symbiotic things. You're just encrypting, and if someone else takes over your project in the future, A look only encryption did not decrypt, is undoubtedly 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:gaoying * @update: * @date: 2015-7-27 */public string Secretdecrypt (string userpasswo      
    RD) throws exception{//using Cipher instance Cipher Cipher =cipher.getinstance ("AES");  
    Gets 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 ();  
      
    The initial dissolution of the secret operation, the transfer of the encryption 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); 
    //Execute decryption String result=cipher.dofinal (SRC). toString (); 
  return result; 

 }</span>

Well, to sum up, we put the encryption and decryption are finished, remember what I said above, encryption and decryption itself is a pair of symbiotic body, indispensable. So do not take a moment of ease, only to do encryption, and decryption to throw away.

The above is the entire content of this article, I hope to help you learn.

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.