java純數字加密解密執行個體

來源:互聯網
上載者:User

標籤:項目   string   try   遞加   詳細   java   trace   取資料   word   

     我們都知道,在使用者加入資訊時,一些比較敏感的資訊,如社會安全號碼,手機號,使用者的登入password等資訊,是不能直接明文存進資料庫的.今天我們就以一個詳細的範例來說明一下純數位java加密解密技術.     


     一般我們從頁面擷取到使用者加入的資訊之後,進行加密然後存入到資料庫.須要比對資訊時,加密之後的使用者資訊我們看不懂,所以相應的我們就要用解密技術.事實上軟考中對加密解密技術進行了非常全面的說明,這裡我們就用一個比較簡單的執行個體來說明一下.


     我們可能會習慣在service層進行加密,這個沒有太強制的要求.以下我們就詳細來看一下加密的過程.先說明一下,由於我的password是六位有效數字,所以我們須要把這六位有效數字進行加密,代碼例如以下:

<span style="white-space:pre"></span>/** * <p>Description: password加密</p> * @param Userpasword 傳過來的六位元字password * @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("使用者password:" + secretEncrypt(userBaseInfo.getUserPassword()));//給password加密。然後放在實體裡進行儲存userBaseInfo.setSUserPassword(secretEncrypt(userBaseInfo.getUserPassword()));//儲存使用者資訊userBaseInfoService.save(userBaseInfo);result = true;}catch(Exception e){e.printStackTrace();}return result;}

     存到資料庫中的使用者password為:第二行就是經過加密後的使用者password.

     

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >

     好了,上面介紹了加密的過程,當然少不瞭解密的過程.你可不能說我們如今需求僅僅讓做加密,沒有解密.是,可能臨時頁面上沒有那麼多需求,可是加密和解密本身就是一對共生體.你單單你做了加密,假設將來別人接手你的項目,一看僅僅有加密沒有解密,無疑就是給別人挖了一個大坑,所以記住,做加密時一定要把解密一起做了,哪怕如今用不到.解密代碼例如以下:

<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>

     好了,綜上所述,我們把加密和解密都講完了,記住我上面說的話,加密和解密本身就是一對共生體,缺一不可.所以不要圖一時輕鬆,僅僅做加密,而把解密給扔掉了.好了,下篇文章我們來說一下div嵌套的問題.



java純數字加密解密執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.