BouncyCastle JCE實踐(五)

來源:互聯網
上載者:User
?

對稱解密的實現

對稱式加密/解密演算法在電子商務交易過程中存在幾個問題:

(1)?????? 要求提供一條安全的渠道使通訊雙方在首次通訊時協商一個共同的密鑰。直接的面對面協商可能是不現實而且難於實施的,所以雙方可能需要藉助於郵件和電話等其它相對不夠安全的手段來進行協商;

(2)?????? 密鑰的數目難於管理。因為對於每一個合作者都需要使用不同的密鑰,很難適應開放社會中大量的資訊交流;

(3)?????? 對稱式加密演算法一般不能提供資訊完整性的鑒別。它無法驗證寄件者和接受者的身份;

對稱金鑰的管理和分發工作是一件具有潛在危險的和煩瑣的過程。對稱式加密是基於共同保守秘密來實現的,採用對稱式加密技術的貿易雙方必須保證採用的是相同的密鑰,保證彼此密鑰的交換是安全可靠的,同時還要設定防止密鑰泄密和更改密鑰的程式。

對稱解密的代碼實現如下:

//從密鑰檔案中讀密鑰

?? SecretKey key=null;

?? try

?? {ObjectInputStream keyFile=new ObjectInputStream(

???? new FileInputStream("c://安全檔案//"+misClass.username+"//對稱//對稱金鑰//yhb.des"));

??? key=(SecretKey)keyFile.readObject();

??? keyFile.close();

??? }

??? catch(FileNotFoundException ey1)

??? {

??? System.out.println("Error when read keyFile");

??? System.exit(0);

??? }

??? catch(Exception ey2)

??? {

??? System.out.println("error when read the keyFile");

??? System.exit(0);

??? }

?? //用key產生Cipher

??? Cipher cipher=null;

??? try

{

//設定演算法,應該與加密時的設定一樣

cipher=Cipher.getInstance("DES");

//設定解密模式

???? cipher.init(Cipher.DECRYPT_MODE,key);

???? }catch(Exception ey3)

???? {

???? System.out.println("Error when create the cipher");

???? System.exit(0);

???? }

???? //從對話方塊中取得要解密的檔案並解密

???? File file=new File(dirstring,string1);

???? String filename=file.getName();

??? try

{

//輸出資料流,請注意檔案名稱的擷取

BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(

????????? "c://安全檔案//檔案//"+filename.substring(0,filename.length()-4)));

???? //輸入資料流

????? CipherInputStream in=new CipherInputStream(new BufferedInputStream(

??????????? new FileInputStream(file)),cipher);

??? int thebyte=0;

??? while((thebyte=in.read())!=-1)

??? {

??? out.write(thebyte);

??? }

????? in.close();

????? out.close();

????? }

????? catch(Exception ey5)

????? {

????? System.out.println("Error when encrypt the file");

????? System.exit(0);

????? }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.