Java加密算法Triple DES

  1. package com.stone.security;  
  2.   
  3. import javax.crypto.Cipher;  
  4. import javax.crypto.KeyGenerator;  
  5. import javax.crypto.SecretKey;  
  6. import javax.crypto.SecretKeyFactory;  
  7. import javax.crypto.spec.DESedeKeySpec;  
  8. import javax.crypto.spec.IvParameterSpec;  
  9.   
  10. /** 
  11.  * 三重加密 3DES也作 Triple DES, 
  12.  *  
  13.  * @author stone 
  14.  * @date 2014-03-10 02:14:37 
  15.  */  
  16. public class TripleDES {  
  17.     // 算法名称  
  18.     public static final String KEY_ALGORITHM = "DESede";  
  19.     // 算法名称/加密模式/填充方式  
  20.     public static final String CIPHER_ALGORITHM_ECB = "DESede/ECB/PKCS5Padding";  
  21.     public static final String CIPHER_ALGORITHM_CBC = "DESede/CBC/PKCS5Padding";  
  22.       
  23.     private KeyGenerator keyGen;  
  24.     private SecretKey secretKey;  
  25.     private SecretKey secretKey2;  
  26.     private Cipher cipher;  
  27.     private static byte[] encryptData;  
  28.       
  29.     public static void main(String[] args) throws Exception {  
  30.         TripleDES tripleDES = new TripleDES("ECB");  
  31.         tripleDES.encrypt("sau8jzxlcvm,'123`98(*^&%^^JCB ZX>>A<S<}}{");  
  32.         System.out.println("加密后:" + new String(encryptData));  
  33.         System.out.println("解密后:"new String(tripleDES.decrypt(encryptData)));  
  34.           
  35.         tripleDES = new TripleDES("CBC");  
  36.         tripleDES.encrypt2("sau8jzxlc DQV#><«|vm,'123`98(*^&%^^JCB ZX>>A<S<}}{");  
  37.         System.out.println("加密后:" + new String(encryptData));  
  38.         System.out.println("解密后:"new String(tripleDES.decrypt2(encryptData)));  
  39.     }  
  40.       
  41.     public TripleDES(String mode) throws Exception {  
  42.         if ("ECB".equals(mode)) {  
  43. //          cipher = Cipher.getInstance(KEY_ALGORITHM);  
  44.             cipher = Cipher.getInstance(CIPHER_ALGORITHM_ECB);  
  45.             keyGen = KeyGenerator.getInstance(KEY_ALGORITHM);  
  46.             secretKey = keyGen.generateKey();  
  47.         } else if("CBC".equals(mode)) {  
  48.             cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);  
  49.             keyGen = KeyGenerator.getInstance(KEY_ALGORITHM);  
  50.             DESedeKeySpec spec = new DESedeKeySpec(keyGen.generateKey().getEncoded());  
  51.             secretKey2 = SecretKeyFactory.getInstance(KEY_ALGORITHM).generateSecret(spec);  
  52.         }  
  53.     }  
  54.     /** 
  55.      * 加密 
  56.      * @param str 
  57.      * @return 
  58.      * @throws Exception 
  59.      */  
  60.     public byte[] encrypt(String str) throws Exception {  
  61.         cipher.init(Cipher.ENCRYPT_MODE, secretKey);  
  62.         return encryptData = cipher.doFinal(str.getBytes());  
  63.     }  
  64.     /** 
  65.      * 解密 
  66.      * @param encrypt 
  67.      * @return 
  68.      * @throws Exception 
  69.      */  
  70.     public byte[] decrypt(byte[] encrypt) throws Exception {  
  71.         cipher.init(Cipher.DECRYPT_MODE, secretKey);  
  72.         return encryptData = cipher.doFinal(encrypt);  
  73.     }  
  74.     byte[] getIV() {  
  75.         return "administ".getBytes();  
  76.     }  
  77.     /** 
  78.      * 加密 
  79.      * @param str 
  80.      * @return 
  81.      * @throws Exception 
  82.      */  
  83.     public byte[] encrypt2(String str) throws Exception {  
  84.         cipher.init(Cipher.ENCRYPT_MODE, secretKey2, new IvParameterSpec(getIV()));  
  85.         return encryptData = cipher.doFinal(str.getBytes());  
  86.     }  
  87.     /** 
  88.      * 解密 
  89.      * @param encrypt 
  90.      * @return 
  91.      * @throws Exception 
  92.      */  
  93.     public byte[] decrypt2(byte[] encrypt) throws Exception {  
  94.         cipher.init(Cipher.DECRYPT_MODE, secretKey2, new IvParameterSpec(getIV()));  
  95.         return encryptData = cipher.doFinal(encrypt);  
  96.     }  
  97. }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值