我們都知道,在用戶添加信息時(shí),一些比較敏感的信息,如身份證號(hào),手機(jī)號(hào),用戶的登錄密碼等信息,是不能直接明文存進(jìn)數(shù)據(jù)庫(kù)的.今天我們就以一個(gè)具體的例子來說明一下純數(shù)字的java加密解密技術(shù).
一般我們從頁(yè)面獲取到用戶添加的信息之后,進(jìn)行加密然后存入到數(shù)據(jù)庫(kù).需要比對(duì)信息時(shí),加密之后的用戶信息我們看不懂,所以對(duì)應(yīng)的我們就要用解密技術(shù).其實(shí)軟考中對(duì)加密解密技術(shù)進(jìn)行了很全面的說明,這里我們就用一個(gè)比較簡(jiǎn)單的實(shí)例來說明一下.
我們可能會(huì)習(xí)慣在service層進(jìn)行加密,這個(gè)沒有太強(qiáng)制的要求.下面我們就具體來看一下加密的過程.先說明一下,因?yàn)槲业拿艽a是六位有效數(shù)字,所以我們需要把這六位有效數(shù)字進(jìn)行加密,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<span style= "white-space:pre" > </span> /** * <p>Description: 密碼加密</p> * @param Userpasword 傳過來的六位數(shù)字密碼 * @return 加密后的字符串 * @throws Exception * @date: 2015年7月27日 */ public String secretEncrypt(String Userpasword) throws Exception { //使用Cipher的實(shí)例 Cipher cipher =Cipher.getInstance( "AES" ); //得到加密的鑰匙 SecretKey key =KeyGenerator.getInstance( "AES" ).generateKey(); //初始化加密操作,傳遞加密的鑰匙 cipher.init(Cipher.ENCRYPT_MODE,key); //將加密的內(nèi)容傳遞進(jìn)去,返回加密后的二進(jìn)制數(shù)據(jù) String results =cipher.doFinal(Userpasword.getBytes()).toString(); //返回加密后的字符串 return results; } |
在具體代碼中的應(yīng)用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<span style= "white-space:pre" > </span> /** * <p>Description: 保存用戶基本信息</p> * @param personBaseInfo 用戶基本信息實(shí)體 * @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())); //給密碼加密,然后放在實(shí)體里進(jìn)行保存 userBaseInfo.setSUserPassword(secretEncrypt(userBaseInfo.getUserPassword())); //保存用戶信息 userBaseInfoService.save(userBaseInfo); result = true ; } catch (Exception e){ e.printStackTrace(); } return result; } |
存到數(shù)據(jù)庫(kù)中的用戶密碼為:第二行就是經(jīng)過加密后的用戶密碼.
好了,上面介紹了加密的過程,當(dāng)然少不了解密的過程.你可不能說我們現(xiàn)在需求只讓做加密,沒有解密.是,可能暫時(shí)頁(yè)面上沒有那么多需求,但是加密和解密本身就是一對(duì)共生體.你單單你做了加密,如果將來別人接手你的項(xiàng)目,一看只有加密沒有解密,無疑就是給別人挖了一個(gè)大坑,所以記住,做加密時(shí)一定要把解密一起做了,哪怕現(xiàn)在用不到.解密代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<span style= "font-size: 18px; white-space: pre;" > </span><span style= "font-size:14px;" > /** * <p>Description: 解密函數(shù)</p> * @param userPassword * @return * @throws Exception * @author : gaoying * @update : * @date : 2015-7-27 */ public String secretDecrypt(String userPassword) throws Exception{ //使用Cipher的實(shí)例 Cipher cipher =Cipher.getInstance( "AES" ); //獲取文件中的key進(jìn)行解密 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); //獲取文件中的二進(jìn)制數(shù)據(jù) FileInputStream fisDat= new FileInputStream( "secretContent.dat" ); //獲取數(shù)據(jù) 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); } //執(zhí)行解密 String result=cipher.doFinal(src).toString(); return result; }</span> |
好了,綜上所述,我們把加密和解密都講完了,記住我上面說的話,加密和解密本身就是一對(duì)共生體,缺一不可.所以不要圖一時(shí)輕松,只做加密,而把解密給扔掉。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。