欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

实例讲解java的纯数字加密解密

程序员文章站 2024-03-05 23:39:37
我们都知道,在用户添加信息时,一些比较敏感的信息,如身份证号,手机号,用户的登录密码等信息,是不能直接明文存进数据库的.今天我们就以一个具体的例子来说明一下纯数字的java...

我们都知道,在用户添加信息时,一些比较敏感的信息,如身份证号,手机号,用户的登录密码等信息,是不能直接明文存进数据库的.今天我们就以一个具体的例子来说明一下纯数字的java加密解密技术.    

一般我们从页面获取到用户添加的信息之后,进行加密然后存入到数据库.需要比对信息时,加密之后的用户信息我们看不懂,所以对应的我们就要用解密技术.其实软考中对加密解密技术进行了很全面的说明,这里我们就用一个比较简单的实例来说明一下.

我们可能会习惯在service层进行加密,这个没有太强制的要求.下面我们就具体来看一下加密的过程.先说明一下,因为我的密码是六位有效数字,所以我们需要把这六位有效数字进行加密,代码如下:

<span style="white-space:pre">  </span>/** 
   * <p>description: 密码加密</p> 
   * @param userpasword 传过来的六位数字密码 
   * @return 加密后的字符串 
   * @throws exception 
   * @date: 2015年7月27日 
   */ 
  public string secretencrypt(string userpasword) throws exception {  
      //使用cipher的实例  
      cipher cipher =cipher.getinstance("aes");       
      //得到加密的钥匙  
      secretkey key =keygenerator.getinstance("aes").generatekey();      
      //初始化加密操作,传递加密的钥匙  
      cipher.init(cipher.encrypt_mode,key);           
      //将加密的内容传递进去,返回加密后的二进制数据  
      string results =cipher.dofinal(userpasword.getbytes()).tostring();  
 
    //返回加密后的字符串 
      return results; 
    } 

在具体代码中的应用:

<span style="white-space:pre">  </span>/** 
   * <p>description: 保存用户基本信息</p> 
   * @param personbaseinfo 用户基本信息实体 
   * @return 布尔型,true代表添加成功,false代表添加失败 
   * @throws exception 
   * @date: 2015年7月27日 
   */ 
  public boolean saveuserinformation(userbaseinfo userbaseinfo) throws exception{ 
    boolean result = false;  
    try{     
      //保存用户基本信息 
      system.out.println("用户密码:" + secretencrypt(userbaseinfo.getuserpassword())); 
      //给密码加密,然后放在实体里进行保存 
      userbaseinfo.setsuserpassword(secretencrypt(userbaseinfo.getuserpassword())); 
      //保存用户信息 
      userbaseinfoservice.save(userbaseinfo); 
      result = true;    
    }catch(exception e){ 
      e.printstacktrace(); 
    } 
    return result; 
  } 

存到数据库中的用户密码为:第二行就是经过加密后的用户密码. 

实例讲解java的纯数字加密解密

好了,上面介绍了加密的过程,当然少不了解密的过程.你可不能说我们现在需求只让做加密,没有解密.是,可能暂时页面上没有那么多需求,但是加密和解密本身就是一对共生体.你单单你做了加密,如果将来别人接手你的项目,一看只有加密没有解密,无疑就是给别人挖了一个大坑,所以记住,做加密时一定要把解密一起做了,哪怕现在用不到.解密代码如下:

<span style="font-size: 18px; white-space: pre;"> </span><span style="font-size:14px;">/** 
   * <p>description: 解密函数</p> 
   * @param userpassword 
   * @return 
   * @throws exception 
   * @author    : gaoying 
   * @update    : 
   * @date     : 2015-7-27 
   */ 
  public string secretdecrypt(string userpassword) throws exception{  
    //使用cipher的实例  
    cipher cipher =cipher.getinstance("aes");      
    //获取文件中的key进行解密  
    fileinputstream fiskey=new fileinputstream("secretkey.key");  
    objectinputstream oiskey =new objectinputstream(fiskey);  
    key key =(key)oiskey.readobject();  
    oiskey.close();  
    fiskey.close();  
      
    //初始化解密操作,传递加密的钥匙  
    cipher.init(cipher.decrypt_mode,key);  
      
    //获取文件中的二进制数据  
    fileinputstream fisdat=new fileinputstream("secretcontent.dat");  
    //获取数据 
    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);  
    }  
    //执行解密     
    string result=cipher.dofinal(src).tostring(); 
    return result; 
  }</span> 

好了,综上所述,我们把加密和解密都讲完了,记住我上面说的话,加密和解密本身就是一对共生体,缺一不可.所以不要图一时轻松,只做加密,而把解密给扔掉。

以上就是本文的全部内容,希望对大家的学习有所帮助。

上一篇: 数组工具类Arrays

下一篇: