DES加密模板,和C#des加密

来源:互联网 发布:全球第一大社交网络 编辑:程序博客网 时间:2024/05/18 03:02

第一种

packagecom.afreon.util;
 
importjava.io.IOException;
importjava.security.SecureRandom;
 
importjavax.crypto.Cipher;
importjavax.crypto.SecretKey;
importjavax.crypto.SecretKeyFactory;
importjavax.crypto.spec.DESKeySpec;
 
importsun.misc.BASE64Decoder;
importsun.misc.BASE64Encoder;
 
publicclass DesUtil {
 
    privatefinal static String DES = "DES";
 
    publicstatic void main(String[] args) throwsException {
        String data = "极限挑战";
        String key = "sdfdsdfdsfs";
        System.err.println(encrypt(data, key));
        System.err.println(decrypt(encrypt(data, key), key));
 
    }
     
    /**
     * Description 根据键值进行加密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    publicstatic String encrypt(String data, String key) throwsException {
        byte[] bt = encrypt(data.getBytes(), key.getBytes());
        String strs = newBASE64Encoder().encode(bt);
        returnstrs;
    }
 
    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws IOException
     * @throws Exception
     */
    publicstatic String decrypt(String data, String key) throwsIOException,
            Exception {
        if(data == null)
            returnnull;
        BASE64Decoder decoder = newBASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf,key.getBytes());
        returnnew String(bt);
    }
 
    /**
     * Description 根据键值进行加密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    privatestatic byte[] encrypt(byte[] data, byte[] key) throwsException {
        // 生成一个可信任的随机数源
        SecureRandom sr = newSecureRandom();
 
        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = newDESKeySpec(key);
 
        // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
 
        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);
 
        // 用密钥初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
 
        returncipher.doFinal(data);
    }
     
     
    /**
     * Description 根据键值进行解密
     * @param data
     * @param key  加密键byte数组
     * @return
     * @throws Exception
     */
    privatestatic byte[] decrypt(byte[] data, byte[] key) throwsException {
        // 生成一个可信任的随机数源
        SecureRandom sr = newSecureRandom();
 
        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = newDESKeySpec(key);
 
        // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
 
        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(DES);
 
        // 用密钥初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
 
        returncipher.doFinal(data);
    }
}
http://www.yoodb.com/article/display/266
第二种
  1. import java.security.Security;  
  2. import java.util.Arrays;  
  3.   
  4. import javax.crypto.Cipher;  
  5. import javax.crypto.SecretKey;  
  6. import javax.crypto.spec.IvParameterSpec;  
  7. import javax.crypto.spec.SecretKeySpec;  
  8.   
  9. import org.bouncycastle.jce.provider.BouncyCastleProvider;  
  10. import org.bouncycastle.util.encoders.Hex;  
  11.   
  12. /*** 
  13.  * 可以和c#加密结果相同     需要到两个jar包: 
  14.  * bcprov-jdk15on-148.jar 
  15.  * bcprov-ext-jdk15on-148.jar   在D:/jar/ 
  16.  *  
  17.  * @ClassName Test3DES_2 
  18.  * @author 张月 
  19.  * @date 2014年4月16日 
  20.  */  
  21. public class Test3DES_2 {  
  22.       
  23.     public static void main(String[] args) throws Exception {  
  24.         String key = "30313233343536373938373635343332";  
  25.         String shuju = "0088757365722020202020202020202020202020202020202020202020202013111816000001123456bc445951f5f60762f951146ece3bed0fbde2c4198f7dc43018f1d5f2c807c47f26FFD30AAF8E3823";  
  26.         String jiamiResult = encryptStr(shuju,key);  
  27.         System.out.println("加密后:"+jiamiResult);//【与C# CBC 零填充模式 互相加解密】  
  28.           
  29.         System.out.println("解密后:"+decryptStr(jiamiResult,key));  
  30.     }  
  31.       
  32.     /** 
  33.      * 3DES加密 (亦称为:DESede加密)   
  34.      *  
  35.      * CBC模式 
  36.      * 填充模式:零字节填充  ZeroBytePadding 
  37.      *  
  38.      * @Method: encrypt3Str  
  39.      * @param @param shuju 
  40.      * @param @param key 
  41.      * @param @return 
  42.      * @param @throws Exception 
  43.      * @return String 
  44.      * @throws 
  45.      */  
  46.     public static String encryptStr(String shuju,String key) throws Exception {  
  47.         String result = "";  
  48.         try {  
  49.             Security.addProvider(new BouncyCastleProvider());  
  50.             byte[] bKey = Hex.decode(key);//十六进制转换成字节数据  
  51.             byte[] bMsg = Hex.decode(shuju);  
  52.   
  53.             byte[] keyBytes = Arrays.copyOf(bKey, 24);  
  54.             int j = 0, k = 16;  
  55.             while (j < 8) {  
  56.                 keyBytes[k++] = keyBytes[j++];  
  57.             }  
  58.   
  59.             SecretKey key3 = new SecretKeySpec(keyBytes, "DESede");  
  60.             IvParameterSpec iv3 = new IvParameterSpec(new byte[8]);//初始向量默认 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00  
  61.             Cipher cipher3 = Cipher.getInstance("DESede/CBC/ZeroBytePadding");  
  62.             cipher3.init(Cipher.ENCRYPT_MODE, key3, iv3);  
  63.   
  64.             byte[] bMac = cipher3.doFinal(bMsg);  
  65.             result=  new String(Hex.encode(bMac));//encode方法字节数组转换成十六进制  
  66.         } catch (Exception e) {  
  67.             e.printStackTrace();  
  68.             throw e;  
  69.         }  
  70.         return result;  
  71.     }  
  72.       
  73.     /** 
  74.      * 3DES 解密 
  75.      * @Method: decryptStr  
  76.      * @param @param shuju 
  77.      * @param @param key 
  78.      * @param @return 
  79.      * @param @throws Exception 
  80.      * @return String 
  81.      * @throws 
  82.      */  
  83.     public static String decryptStr(String shuju,String key) throws Exception{  
  84.         String result = "";  
  85.         try {  
  86.             Security.addProvider(new BouncyCastleProvider());  
  87.             byte[] bKey = Hex.decode(key);//十六进制转换成字节数据  
  88.             byte[] bMsg = Hex.decode(shuju);  
  89.   
  90.             byte[] keyBytes = Arrays.copyOf(bKey, 24);  
  91.             int j = 0, k = 16;  
  92.             while (j < 8) {  
  93.                 keyBytes[k++] = keyBytes[j++];  
  94.             }  
  95.   
  96.             SecretKey key3 = new SecretKeySpec(keyBytes, "DESede");  
  97.             IvParameterSpec iv3 = new IvParameterSpec(new byte[8]);//初始向量默认 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00  
  98.             Cipher cipher3 = Cipher.getInstance("DESede/CBC/ZeroBytePadding");  
  99.             cipher3.init(Cipher.DECRYPT_MODE, key3, iv3);  
  100.   
  101.             byte[] bMac = cipher3.doFinal(bMsg);  
  102.             result=  new String(Hex.encode(bMac));//encode方法字节数组转换成十六进制  
  103.         } catch (Exception e) {  
  104.             e.printStackTrace();  
  105.             throw e;  
  106.         }  
  107.         return result;  
  108.     }   
  109. }  
借鉴
http://blog.csdn.net/zhuni_xingfu/article/details/23864643

0 0
原创粉丝点击