JAVA加密解密DES對稱式加密演算法

來源:互聯網
上載者:User

標籤:exception   private   catch   簡單   decrypt   get   檔案中   ges   nal   

  1 下面用DES對稱式加密演算法(設定一個密鑰,然後對所有的資料進行加密)來簡單舉個例子。  2   3 首先,產生一個密鑰KEY。  4 我把它儲存到key.txt中。這個檔案就象是一把鑰匙。誰擁有它,誰就能解開我們的類檔案。代碼參考如下:  5 package com.neusoft.jiami;  6 import Java.io.File;  7 import java.io.FileOutputStream;  8 import java.security.SecureRandom;  9 import javax.crypto.KeyGenerator; 10 import javax.crypto.SecretKey; 11 class Key { 12     private String keyName; 13     public Key(String keyName) { 14         this.keyName = keyName; 15      } 16     public void createKey(String keyName) throws Exception { 17         // 建立一個可信任的隨機數源,DES演算法需要 18          SecureRandom sr = new SecureRandom(); 19         // 用DES演算法建立一個KeyGenerator對象 20          KeyGenerator kg = KeyGenerator.getInstance("DES"); 21         // 初始化此金鑰產生器,使其具有確定的密鑰長度 22          kg.init(sr); 23         // 產生密匙 24          SecretKey key = kg.generateKey(); 25         // 擷取密鑰資料 26         byte rawKeyData[] = key.getEncoded(); 27         // 將擷取到密鑰資料儲存到檔案中,待解密時使用 28          FileOutputStream fo = new FileOutputStream(new File(keyName)); 29          fo.write(rawKeyData); 30      } 31     public static void main(String args[]) { 32         try { 33             new Key("key.txt"); 34          } catch (Exception e) { 35              e.printStackTrace(); 36          } 37      } 38 } 39 第二步,對我們所要進行加密的類檔案進行加密。 40     比如我有一個DigestPass類,已經被正常編譯好產生DigestPass.class檔案。此時,這個類檔案是任何人都可以用的。因為系統的類載入器可以自動的載入它。那麼下一步,我們要做的就是把這個類檔案加密。使系統的類載入器無法讀取到正確的位元組碼檔案。參考代碼如下: 41 package com.neusoft.jiami; 42 import java.io.File; 43 import java.io.FileInputStream; 44 import java.io.FileOutputStream; 45 import java.security.SecureRandom; 46 import javax.crypto.Cipher; 47 import javax.crypto.SecretKey; 48 import javax.crypto.SecretKeyFactory; 49 import javax.crypto.spec.DESKeySpec; 50 public class JiaMi { 51     public static void main(String[] args) throws Exception { 52         // DES演算法要求有一個可信任的隨機數源 53          SecureRandom sr = new SecureRandom(); 54         // 獲得密匙資料 55          FileInputStream fi = new FileInputStream(new File("key.txt")); 56         byte rawKeyData[] = new byte[fi.available()]; 57          fi.read(rawKeyData); 58          fi.close(); 59         // 從原始密匙資料建立DESKeySpec對象 60          DESKeySpec dks = new DESKeySpec(rawKeyData); 61         // 建立一個密匙工廠,然後用它把DESKeySpec轉換成一個SecretKey對象 62          SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(dks); 63         // Cipher對象實際完成加密操作 64          Cipher cipher = Cipher.getInstance("DES"); 65         // 用密匙初始化Cipher對象 66          cipher.init(Cipher.ENCRYPT_MODE, key, sr); 67         // 現在,擷取要加密的檔案資料 68          FileInputStream fi2 = new FileInputStream(new File("DigestPass.class")); 69         byte data[] = new byte[fi2.available()]; 70          fi2.read(data); 71          fi2.close(); 72         // 正式執行加密操作 73         byte encryptedData[] = cipher.doFinal(data); 74         // 用加密後的資料覆蓋原檔案 75          FileOutputStream fo = new FileOutputStream(new File("DigestPass.class")); 76          fo.write(encryptedData); 77          fo.close(); 78      } 79 } 80 第三步,用自訂的CLASSLOADER進行載入。參考代碼如下: 81 package com.neusoft.jiami; 82 import java.io.File; 83 import java.io.FileInputStream; 84 import java.io.FileOutputStream; 85 import java.security.SecureRandom; 86 import javax.crypto.Cipher; 87 import javax.crypto.SecretKey; 88 import javax.crypto.SecretKeyFactory; 89 import javax.crypto.spec.DESKeySpec; 90 import com.neusoft.classloader.MyClassLoader; 91 public class JieMi { 92     public static void main(String[] args) throws Exception { 93         // DES演算法要求有一個可信任的隨機數源 94          SecureRandom sr = new SecureRandom(); 95         // 獲得密匙資料 96          FileInputStream fi = new FileInputStream(new File("key.txt")); 97         byte rawKeyData[] = new byte[fi.available()];// = new byte[5]; 98          fi.read(rawKeyData); 99          fi.close();100         // 從原始密匙資料建立一個DESKeySpec對象101          DESKeySpec dks = new DESKeySpec(rawKeyData);102         // 建立一個密匙工廠,然後用它把DESKeySpec對象轉換成一個SecretKey對象103          SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(dks);104         // Cipher對象實際完成解密操作105          Cipher cipher = Cipher.getInstance("DES");106         // 用密匙初始化Cipher對象107          cipher.init(Cipher.DECRYPT_MODE, key, sr);108         // 現在,擷取資料並解密109          FileInputStream fi2 = new FileInputStream(new File("DigestPass.class"));110         byte encryptedData[] = new byte[fi2.available()];111          fi2.read(encryptedData);112          fi2.close();113         // 正式執行解密操作114         byte decryptedData[] = cipher.doFinal(encryptedData);115         // 這時把資料還原成原有的類檔案116         // FileOutputStream fo = new FileOutputStream(new117         // File("DigestPass.class"));118         // fo.write(decryptedData);119         // 用解密後的資料載入類並應用120          MyClassloader mcl = new MyClassloader("E:/");121          Class cl = mcl.loadClass(decryptedData, "com.neusoft.jiami.DigestPass");122          DigestPass dp = cl.newInstance();123      }124 }

 

JAVA加密解密DES對稱式加密演算法

聯繫我們

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