Java中的对称加密算法

在Java中,对于加密密钥和解密密钥相同的加密我们称之为对称加密,其中主要有DES,3DES和AES加密。

DES加密
[java]  view plain  copy
  1. package com.example.asiatravel.learndes.util;  
  2.   
  3. import android.util.Log;  
  4.   
  5. import javax.crypto.Cipher;  
  6. import javax.crypto.KeyGenerator;  
  7. import javax.crypto.SecretKey;  
  8. import javax.crypto.spec.SecretKeySpec;  
  9.   
  10. /** 
  11.  * Created by kuangxiaoguo on 16/9/11. 
  12.  * 
  13.  * DES加密工具类 
  14.  */  
  15. public class DESUtil {  
  16.   
  17.     private static final String TAG = "TAG";  
  18.   
  19.     /** 
  20.      * 生成密钥 
  21.      */  
  22.     public static byte[] initKey() {  
  23.         try {  
  24.             //KeyGenerator 密钥生成器  
  25.             KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");  
  26.             //初始化密钥生成器  
  27.             keyGenerator.init(56);  
  28.             //生成密钥  
  29.             SecretKey secretKey = keyGenerator.generateKey();  
  30.             return secretKey.getEncoded();  
  31.         } catch (Exception e) {  
  32.             Log.e(TAG, "initKey: " + e.getMessage());  
  33.         }  
  34.         return null;  
  35.     }  
  36.   
  37.     /** 
  38.      * DES加密 
  39.      * 
  40.      * @param data 需要加密的数据 
  41.      * @param key  加密使用的密钥 
  42.      * @return 加密后获取的字节数组 
  43.      */  
  44.     public static byte[] encrypt(byte[] data, byte[] key) {  
  45.         //恢复密钥  
  46.         SecretKey secretKey = new SecretKeySpec(key, "DES");  
  47.         try {  
  48.             //Cipher完成加密或解密工作  
  49.             Cipher cipher = Cipher.getInstance("DES");  
  50.             //根据密钥对Cipher进行初始化 ENCRYPT_MODE, DECRYPT_MODE  
  51.             cipher.init(Cipher.ENCRYPT_MODE, secretKey);  
  52.             //加密  
  53.             return cipher.doFinal(data);  
  54.         } catch (Exception e) {  
  55.             Log.e(TAG, "encrypt: " + e.getMessage());  
  56.         }  
  57.         return null;  
  58.     }  
  59.   
  60.     /** 
  61.      * DES解密 
  62.      */  
  63.     /** 
  64.      * @param data 密文对应的字节数组 
  65.      * @param key  算法名字 
  66.      * @return 解密后的字节数组 
  67.      */  
  68.     public static byte[] decrypt(byte[] data, byte[] key) {  
  69.         SecretKey secretKey = new SecretKeySpec(key, "DES");  
  70.         try {  
  71.             Cipher cipher = Cipher.getInstance("DES");  
  72.             cipher.init(Cipher.DECRYPT_MODE, secretKey);  
  73.             return cipher.doFinal(data);  
  74.         } catch (Exception e) {  
  75.             Log.e(TAG, "decrypt: " + e.getMessage());  
  76.         }  
  77.         return null;  
  78.     }  
  79. }  
3DES加密:
[java]  view plain  copy
  1. package com.example.asiatravel.learndes.util;  
  2.   
  3.   
  4. import javax.crypto.Cipher;  
  5. import javax.crypto.KeyGenerator;  
  6. import javax.crypto.SecretKey;  
  7. import javax.crypto.spec.SecretKeySpec;  
  8.   
  9. /** 
  10.  * Created by kuangxiaoguo on 16/9/13. 
  11.  * 
  12.  * 3DES加密工具类 
  13.  */  
  14. public class TripleDESUtil {  
  15.   
  16.     /** 
  17.      * 生成密钥 
  18.      * 
  19.      * @return 密钥 
  20.      * @throws Exception 
  21.      */  
  22.     public static byte[] initKey() throws Exception {  
  23.         KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede");  
  24.         keyGenerator.init(168);//密钥长度 112 168  
  25.         SecretKey secretKey = keyGenerator.generateKey();  
  26.         return secretKey.getEncoded();  
  27.     }  
  28.   
  29.     /** 
  30.      * 3DES加密 
  31.      * 
  32.      * @param data 要加密的数据 
  33.      * @param key  加密所使用的密钥 
  34.      * @return 加密后的数据 
  35.      * @throws Exception 
  36.      */  
  37.     public static byte[] encrypt(byte[] data, byte[] key) throws Exception {  
  38.         SecretKey secretKey = new SecretKeySpec(key, "DESede");  
  39.         Cipher cipher = Cipher.getInstance("DESede");  
  40.         cipher.init(Cipher.ENCRYPT_MODE, secretKey);  
  41.         return cipher.doFinal(data);  
  42.     }  
  43.   
  44.     /** 
  45.      * 3DES解密 
  46.      * 
  47.      * @param data 加密后的数据 
  48.      * @param key  解密所需要的key 
  49.      * @return 解密后的数据 
  50.      * @throws Exception 
  51.      */  
  52.     public static byte[] decrypt(byte[] data, byte[] key) throws Exception {  
  53.         SecretKey secretKey = new SecretKeySpec(key, "DESede");  
  54.         Cipher cipher = Cipher.getInstance("DESede");  
  55.         cipher.init(Cipher.DECRYPT_MODE, secretKey);  
  56.         return cipher.doFinal(data);  
  57.     }  
  58. }  
AES加密:
[java]  view plain  copy
  1. package com.example.asiatravel.learndes.util;  
  2.   
  3. import java.security.NoSuchAlgorithmException;  
  4.   
  5. import javax.crypto.Cipher;  
  6. import javax.crypto.KeyGenerator;  
  7. import javax.crypto.SecretKey;  
  8. import javax.crypto.spec.SecretKeySpec;  
  9.   
  10. /** 
  11.  * Created by kuangxiaoguo on 16/9/13. 
  12.  * 
  13.  * AES加密工具类 
  14.  */  
  15. public class AESUtil {  
  16.   
  17.     /** 
  18.      * 获取密钥 
  19.      * 
  20.      * @return 密钥 
  21.      * @throws NoSuchAlgorithmException 
  22.      */  
  23.     public static byte[] initKey() throws NoSuchAlgorithmException {  
  24.         KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");  
  25.         keyGenerator.init(128); //192,256  
  26.         SecretKey secretKey = keyGenerator.generateKey();  
  27.         return secretKey.getEncoded();  
  28.     }  
  29.   
  30.     /** 
  31.      * AES加密 
  32.      * 
  33.      * @param data 要加密的数据 
  34.      * @param key  加密所使用的密钥 
  35.      * @return 加密后的数据 
  36.      * @throws Exception 
  37.      */  
  38.     public static byte[] encrypt(byte[] data, byte[] key) throws Exception {  
  39.         SecretKey secretKey = new SecretKeySpec(key, "AES");  
  40.         Cipher cipher = Cipher.getInstance("AES");  
  41.         cipher.init(Cipher.ENCRYPT_MODE, secretKey);  
  42.         return cipher.doFinal(data);  
  43.     }  
  44.   
  45.     /** 
  46.      * AES解密 
  47.      * 
  48.      * @param data 要解密的数据 
  49.      * @param key  解密所使用的密钥 
  50.      * @return 解密后的数据, 即源数据 
  51.      * @throws Exception 
  52.      */  
  53.     public static byte[] decrypt(byte[] data, byte[] key) throws Exception {  
  54.         SecretKey secretKey = new SecretKeySpec(key, "AES");  
  55.         Cipher cipher = Cipher.getInstance("AES");  
  56.         cipher.init(Cipher.DECRYPT_MODE, secretKey);  
  57.         return cipher.doFinal(data);  
  58.     }  
  59. }  
测试三种加密方式的类:
[java]  view plain  copy
  1. package com.example.asiatravel.learndes;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.util.Log;  
  6.   
  7. import com.example.asiatravel.learndes.util.AESUtil;  
  8. import com.example.asiatravel.learndes.util.ByteToHexUtil;  
  9. import com.example.asiatravel.learndes.util.DESUtil;  
  10. import com.example.asiatravel.learndes.util.TripleDESUtil;  
  11.   
  12. public class MainActivity extends AppCompatActivity {  
  13.   
  14.     private static final String DATA = "asiatravel";  
  15.     private static final String TAG = "TAG";  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.   
  22.         testDES();  
  23.         test3DES();  
  24.         testAES();  
  25.     }  
  26.   
  27.     /** 
  28.      * 测试AES加密-->对称加密 
  29.      */  
  30.     private void testAES() {  
  31.         try {  
  32.             /** 
  33.              * 密钥 
  34.              */  
  35.             byte[] aesKey = AESUtil.initKey();  
  36.             System.out.println(DATA + "AES key: " + ByteToHexUtil.fromByteToHex(aesKey));  
  37.             /** 
  38.              * 加密后的数据 
  39.              */  
  40.             byte[] encryptResult = AESUtil.encrypt(DATA.getBytes(), aesKey);  
  41.             System.out.println(DATA + "AES 加密: " + ByteToHexUtil.fromByteToHex(encryptResult));  
  42.             /** 
  43.              * 解密后的数据 
  44.              */  
  45.             byte[] decryptResult = AESUtil.decrypt(encryptResult, aesKey);  
  46.             System.out.println(DATA + "AES 解密: " + new String(decryptResult));  
  47.         } catch (Exception e) {  
  48.             Log.e(TAG, "testAES: " + e.getMessage());  
  49.         }  
  50.     }  
  51.   
  52.     /** 
  53.      * 测试3DES加密-->对称加密 
  54.      */  
  55.     private void test3DES() {  
  56.         try {  
  57.             /** 
  58.              * 密钥 
  59.              */  
  60.             byte[] tripleKey = TripleDESUtil.initKey();  
  61.             System.out.println(DATA + "3DES key: " + ByteToHexUtil.fromByteToHex(tripleKey));  
  62.             /** 
  63.              * 加密后的数据 
  64.              */  
  65.             byte[] encryptResult = TripleDESUtil.encrypt(DATA.getBytes(), tripleKey);  
  66.             System.out.println(DATA + "3DES 加密: " + ByteToHexUtil.fromByteToHex(encryptResult));  
  67.             /** 
  68.              * 解密后的数据 
  69.              */  
  70.             byte[] decryptResult = TripleDESUtil.decrypt(encryptResult, tripleKey);  
  71.             System.out.println(DATA + "3DES 解密: " + new String(decryptResult));  
  72.         } catch (Exception e) {  
  73.             Log.e(TAG, "test3DES: " + e.getMessage());  
  74.         }  
  75.     }  
  76.   
  77.     /** 
  78.      * 测试DES加密-->对称加密 
  79.      */  
  80.     private void testDES() {  
  81.         /** 
  82.          * DES 加密 
  83.          */  
  84.         byte[] desKey = DESUtil.initKey();  
  85.         System.out.println("DES key: " + ByteToHexUtil.fromByteToHex(desKey));  
  86.         /** 
  87.          * 加密后的数据 
  88.          */  
  89.         byte[] desResult = DESUtil.encrypt(DATA.getBytes(), desKey);  
  90.         System.out.println(DATA + "DES 加密>>>" + ByteToHexUtil.fromByteToHex(desResult));  
  91.   
  92.         /** 
  93.          * DES 解密 
  94.          */  
  95.         byte[] decryptResult = DESUtil.decrypt(desResult, desKey);  
  96.         System.out.println(DATA + "DES 解密" + new String(decryptResult));  
  97.     }  
  98. }  
总结:这就是关于Java中对称加密的相关代码,相关注释都写在代码里面了
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值