3DES双倍长加密

Posted bchange

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3DES双倍长加密相关的知识,希望对你有一定的参考价值。

结果与DES算法工具一致
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class ceshi2 {
     public static void main(String[] args) {
           String key = "F2C04AD9F598EE61C424C9C7D39BA75F";
           String data =  "06321643FF8B67EB";
           String des = encryptECB3Des(key,data);
           System.out.println(des);
     }
     
    public static String encryptECB3Des(String key, String src) { 
        System.out.println("encryptECB3Des->" + "key:" + key); 
        System.out.println("encryptECB3Des->" + "src:" + src); 
        int len = key.length(); 
        if (key == null || src == null) { 
            return null
        } 
        if (src.length() % 16 != 0) { 
            return null
        } 
        if (len == 32) { 
            String outData = ""
            String str = ""
            for (int i = 0; i < src.length() / 16; i++) { 
                str = src.substring(i * 16, (i + 1) * 16); 
                outData += encECB3Des(key, str); 
            } 
            return outData; 
        } 
        return null
    } 
 
    public static String encECB3Des(String key, String src) { 
        byte[] temp = null
        byte[] temp1 = null
        temp1 = encryptDes(hexStringToBytes(key.substring(0, 16)), hexStringToBytes(src)); 
        temp = decryptDes(hexStringToBytes(key.substring(16, 32)), temp1); 
        temp1 = encryptDes(hexStringToBytes(key.substring(0, 16)), temp); 
        return bytesToHexString(temp1); 
    } 
 
    public static String decECB3Des(String key, String src) { 
        byte[] temp2 = decryptDes(hexStringToBytes(key.substring(0, 16)), hexStringToBytes(src)); 
        byte[] temp1 = encryptDes(hexStringToBytes(key.substring(16, 32)), temp2); 
        byte[] dest = decryptDes(hexStringToBytes(key.substring(0, 16)), temp1); 
        return bytesToHexString(dest); 
    } 
   
    public static String bytesToHexString(byte[] src) {
           StringBuilder stringBuilder = new StringBuilder("");
           if (src == null || src.length <= 0) {
                return null;
           }
           for (int i = 0; i < src.length; i++) {
                int v = src[i] & 0xFF;
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                     stringBuilder.append(0);
                }
                stringBuilder.append(hv);
           }
           return stringBuilder.toString();
     }
   
 
    public static byte[] hexStringToBytes(String hexString) {
           if (hexString == null || hexString.equals("")) {
                return null;
           }
           hexString = hexString.toUpperCase();
           int length = hexString.length() / 2;
           char[] hexChars = hexString.toCharArray();
           byte[] d = new byte[length];
           for (int i = 0; i < length; i++) {
                int pos = i * 2;
                d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
           }
           return d;
     }
     private static byte charToByte(char c) {
           return (byte) "0123456789ABCDEF".indexOf(c);
     }
   
   
    /**
     * 3DES(双倍长) 解密
     * 
     * @param keybyte
     * @param src
     * @return
     */ 
    public static String decryptECB3Des(String key, String src) { 
        if (key == null || src == null) { 
            return null
        } 
        if (src.length() % 16 != 0) { 
            return null
        } 
        if (key.length() == 32) { 
            String outData = ""
            String str = ""
            for (int i = 0; i < src.length() / 16; i++) { 
                str = src.substring(i * 16, (i + 1) * 16); 
                outData += decECB3Des(key, str); 
            } 
            return outData; 
        } 
        return null
    } 
   
    /**
    * DES加密
    * 
    */ 
   public static byte[] encryptDes(byte[] key, byte[] src) { 
       try
           // 创建一个DESKeySpec对象 
           DESKeySpec desKey = new DESKeySpec(key); 
           // 创建一个密匙工厂 
           SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 
           // 将DESKeySpec对象转换成SecretKey对象 
           SecretKey secretKey = keyFactory.generateSecret(desKey); 
           // Cipher对象实际完成解密操作 
           Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); 
           // 用密匙初始化Cipher对象 
           cipher.init(Cipher.ENCRYPT_MODE, secretKey); 
           // 现在,获取数据并加密 
           // 正式执行加密操作 
           return cipher.doFinal(src); 
       } catch (Exception e) { 
           e.printStackTrace(); 
       } 
       return null
   } 
 
   /**
    * des解密
    * 
    * @param key
    * @param src
    * @return
    */ 
   public static byte[] decryptDes(byte[] key, byte[] src) { 
       try
           // DES算法要求有一个可信任的随机数源 
           SecureRandom random = new SecureRandom(); 
           // 创建一个DESKeySpec对象 
           DESKeySpec desKey = new DESKeySpec(key); 
           // 创建一个密匙工厂 
           SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 
           // 将DESKeySpec对象转换成SecretKey对象 
           SecretKey secretKey = keyFactory.generateSecret(desKey); 
           // Cipher对象实际完成解密操作 
           Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); 
           // 用密匙初始化Cipher对象 
           cipher.init(Cipher.DECRYPT_MODE, secretKey, random); 
           // 现在,获取数据并加密 
           // 正式执行加密操作 
           return cipher.doFinal(src); 
       } catch (Exception e) { 
           e.printStackTrace(); 
       } 
       return null
   } 
}

以上是关于3DES双倍长加密的主要内容,如果未能解决你的问题,请参考以下文章

php 3des 加密问题

3DES加密算法原理及实现

加密算法系列之:des加密aes加密3des加密对称加密非对称加密Hash 算法

对称加密算法对比

[android]DES/3DES/AES加密方式

java 3des加密问题记录