Java對稱與非對稱加密解密,AES與RSA


加密技術可以分為對稱與非對稱兩種.

對稱加密,解密,即加密與解密用的是同一把秘鑰,常用的對稱加密技術有DES,AES等

而非對稱技術,加密與解密用的是不同的秘鑰,常用的非對稱加密技術有RSA等

 

為什么要有非對稱加密,解密技術呢

假設這樣一種場景A要發送一段消息給B,但是又不想以明文發送,所以就需要對消息進行加密.如果采用對稱加密技術,那么加密與解密用的是同一把秘鑰.除非B事先就知道A的秘鑰,並且保存好.這樣才可以解密A發來的消息.

由於對稱技術只有一把秘鑰,所以秘鑰的管理是一個很麻煩的問題.而非對稱技術的誕生就解決了這個問題.非對稱加密與解密使用的是不同的秘鑰,並且秘鑰對是一一對應的,即用A的私鑰加密的密文只有用A的公鑰才能解密.

這樣的話,每個人都有兩把秘鑰,私鑰和公鑰,私鑰是只有自己才知道的,不能告訴別人,而公鑰是公開的,大家都可以知道.這樣,當A想要發送消息給B的時候,只需要用B的公鑰對消息進行加密就可以了,由於B的私鑰只有B才擁有,所以A用B的公鑰加密的消息只有B才能解開.而B想更換自己的秘要時也很方便,只須把公鑰告訴大家就可以了.

那么,既然非對稱加密如此之好,對稱加密就沒有存在的必要了啊,其實不然,由於非對稱加密算法的開銷很大,所以如果直接以非對稱技術來加密發送的消息效率會很差.那么怎么辦呢?解決的辦法也很簡單,就是把對稱加密技術與非對稱加密技術結合起來使用.

還是這個例子:A要發送一個消息給B.

一,A先生成一個對稱秘鑰,這個秘鑰可以是隨機生成的,

二,A用B的公鑰加密第一步生成的這個對稱秘鑰

三,A把加密過的對稱秘鑰發給B

四,A用第一步生成的這個對稱秘鑰加密實際要發的消息

五,A把用對稱秘鑰加密的消息發給B

對於B

他先收到A發來的對稱秘鑰,這個秘鑰是用B的公鑰加密過的,所以B需要用自己的私鑰來解密這個秘鑰

然后B又收到A發來的密文,這時候用剛才解密出來的秘鑰來解密密文

 

這樣子的整個過程既保證了安全,又保證了效率.

 

接下來是Java實現:

我這個Java實現使用的是AES的對稱加密和RSA的非對稱加密(DES的對稱加密實現方法和AES的是一樣的,但是由於DES算法本身有缺陷,容易被破解,所以現在多用其升級版AES對稱加密)

 

AES對稱加密,解密

Java代碼   收藏代碼
  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import java.io.OutputStream;  
  4. import java.security.InvalidKeyException;  
  5. import java.security.Key;  
  6. import java.security.NoSuchAlgorithmException;  
  7. import java.security.SecureRandom;  
  8.   
  9. import javax.crypto.BadPaddingException;  
  10. import javax.crypto.Cipher;  
  11. import javax.crypto.IllegalBlockSizeException;  
  12. import javax.crypto.KeyGenerator;  
  13. import javax.crypto.NoSuchPaddingException;  
  14. import javax.crypto.ShortBufferException;  
  15.   
  16. public class AES {  
  17.       
  18.     private Key key;  
  19.       
  20.     /** 
  21.      * 生成AES對稱秘鑰 
  22.      * @throws NoSuchAlgorithmException 
  23.      */  
  24.     public void generateKey() throws NoSuchAlgorithmException {  
  25.         KeyGenerator keygen = KeyGenerator.getInstance("AES");  
  26.         SecureRandom random = new SecureRandom();  
  27.         keygen.init(random);  
  28.         this.key = keygen.generateKey();  
  29.     }  
  30.       
  31.       
  32.     /** 
  33.      * 加密 
  34.      * @param in 
  35.      * @param out 
  36.      * @throws InvalidKeyException 
  37.      * @throws ShortBufferException 
  38.      * @throws IllegalBlockSizeException 
  39.      * @throws BadPaddingException 
  40.      * @throws NoSuchAlgorithmException 
  41.      * @throws NoSuchPaddingException 
  42.      * @throws IOException 
  43.      */  
  44.     public void encrypt(InputStream in, OutputStream out) throws InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IOException {  
  45.         this.crypt(in, out, Cipher.ENCRYPT_MODE);  
  46.     }  
  47.       
  48.     /** 
  49.      * 解密 
  50.      * @param in 
  51.      * @param out 
  52.      * @throws InvalidKeyException 
  53.      * @throws ShortBufferException 
  54.      * @throws IllegalBlockSizeException 
  55.      * @throws BadPaddingException 
  56.      * @throws NoSuchAlgorithmException 
  57.      * @throws NoSuchPaddingException 
  58.      * @throws IOException 
  59.      */  
  60.     public void decrypt(InputStream in, OutputStream out) throws InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IOException {  
  61.         this.crypt(in, out, Cipher.DECRYPT_MODE);  
  62.     }  
  63.   
  64.     /** 
  65.      * 實際的加密解密過程 
  66.      * @param in 
  67.      * @param out 
  68.      * @param mode 
  69.      * @throws IOException 
  70.      * @throws ShortBufferException 
  71.      * @throws IllegalBlockSizeException 
  72.      * @throws BadPaddingException 
  73.      * @throws NoSuchAlgorithmException 
  74.      * @throws NoSuchPaddingException 
  75.      * @throws InvalidKeyException 
  76.      */  
  77.     public void crypt(InputStream in, OutputStream out, int mode) throws IOException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {  
  78.         Cipher cipher = Cipher.getInstance("AES");  
  79.         cipher.init(mode, this.key);  
  80.           
  81.         int blockSize = cipher.getBlockSize();  
  82.         int outputSize = cipher.getOutputSize(blockSize);  
  83.         byte[] inBytes = new byte[blockSize];  
  84.         byte[] outBytes = new byte[outputSize];  
  85.           
  86.         int inLength = 0;  
  87.         boolean more = true;  
  88.         while (more) {  
  89.             inLength = in.read(inBytes);  
  90.             if (inLength == blockSize) {  
  91.                 int outLength = cipher.update(inBytes, 0, blockSize, outBytes);  
  92.                 out.write(outBytes, 0, outLength);  
  93.             } else {  
  94.                 more = false;  
  95.             }  
  96.         }  
  97.         if (inLength > 0)  
  98.             outBytes = cipher.doFinal(inBytes, 0, inLength);  
  99.         else  
  100.             outBytes = cipher.doFinal();  
  101.         out.write(outBytes);  
  102.         out.flush();  
  103.     }  
  104.   
  105.     public void setKey(Key key) {  
  106.         this.key = key;  
  107.     }  
  108.   
  109.     public Key getKey() {  
  110.         return key;  
  111.     }  
  112.       
  113. }  

 

RSA非對稱加密,解密對稱秘鑰

Java代碼   收藏代碼
  1. public class RSA {  
  2.   
  3.     public static final int KEYSIZE = 512;  
  4.       
  5.     private KeyPair keyPair;  
  6.     private Key publicKey;  
  7.     private Key privateKey;  
  8.       
  9.     /** 
  10.      * 生成秘鑰對 
  11.      * @return 
  12.      * @throws NoSuchAlgorithmException 
  13.      */  
  14.     public KeyPair generateKeyPair() throws NoSuchAlgorithmException {  
  15.         KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");  
  16.         SecureRandom random = new SecureRandom();  
  17.         pairgen.initialize(RSA.KEYSIZE, random);  
  18.         this.keyPair = pairgen.generateKeyPair();  
  19.         return this.keyPair;  
  20.     }  
  21.   
  22.     /** 
  23.      * 加密秘鑰 
  24.      * @param key 
  25.      * @return 
  26.      * @throws NoSuchAlgorithmException 
  27.      * @throws NoSuchPaddingException 
  28.      * @throws InvalidKeyException 
  29.      * @throws IllegalBlockSizeException 
  30.      */  
  31.     public byte[] wrapKey(Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException {  
  32.         Cipher cipher = Cipher.getInstance("RSA");  
  33.         cipher.init(Cipher.WRAP_MODE, this.privateKey);  
  34.         byte[] wrappedKey = cipher.wrap(key);  
  35.         return wrappedKey;  
  36.     }  
  37.       
  38.     /** 
  39.      * 解密秘鑰 
  40.      * @param wrapedKeyBytes 
  41.      * @return 
  42.      * @throws NoSuchAlgorithmException 
  43.      * @throws NoSuchPaddingException 
  44.      * @throws InvalidKeyException 
  45.      */  
  46.     public Key unwrapKey(byte[] wrapedKeyBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {  
  47.         Cipher cipher = Cipher.getInstance("RSA");  
  48.         cipher.init(Cipher.UNWRAP_MODE, this.publicKey);  
  49.         Key key = cipher.unwrap(wrapedKeyBytes, "AES", Cipher.SECRET_KEY);  
  50.         return key;  
  51.     }  
  52.   
  53.     public Key getPublicKey() {  
  54.         return publicKey;  
  55.     }  
  56.   
  57.     public void setPublicKey(Key publicKey) {  
  58.         this.publicKey = publicKey;  
  59.     }  
  60.   
  61.     public Key getPrivateKey() {  
  62.         return privateKey;  
  63.     }  
  64.   
  65.     public void setPrivateKey(Key privateKey) {  
  66.         this.privateKey = privateKey;  
  67.     }  
  68.       
  69. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM