Triple Des encryption algorithm

Introduction from Wikipedia:
Triple DES, also known as 3DES, is a mode of the DES encryption algorithm that uses three 56-bit key pairs
3DES 3DES
Data is encrypted three times. The Data Encryption Standard (DES) is a long-standing encryption standard in the United States that uses symmetric key cryptography and was standardized by the ANSI organization as ANSI X.3.92 in 1981 . DES uses a 56-bit key and cipher block method, and in the cipher block method, text is divided into 64-bit sized text blocks and then encrypted. 3DES is more secure than the original DES.
3DES (i.e. Triple DES) is an encryption (in 1999, NIST designated 3-DES as a transitional encryption standard). The specific implementation of the encryption algorithm is as follows: Let Ek() and Dk() represent the DES algorithm The encryption and decryption process of , K represents the key , M represents the plaintext, and C represents the ciphertext, as follows:
The 3DES encryption process is: C=Ek3(Dk2(Ek1(M)))
The 3DES decryption process is: M=Dk1(EK2(Dk3(C)))
Code:
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.lang3.StringUtils;
import jodd.util.Base64;
 public class TripleDES {
// algorithm name  
    public static final String KEY_ALGORITHM = "DESede";  
    // Algorithm name/encryption mode/padding method  
    public static final String CIPHER_ALGORITHM_CBC = "DESede/CBC/PKCS5Padding";
    public static byte[] DefaultIV = {0x12, 0x34, 0x56, 0x78, (byte) 0x90, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF};
    private SecretKey secretKey;
    private Cipher cipher;
    private byte[] encryptData;
    private AlgorithmParameterSpec iv;
    private static TripleDES tripleDES = null;
    public static TripleDES getInstance(String key) throws Exception {
    if (tripleDES == null) {
    synchronized (TripleDES.class) {
    tripleDES = new TripleDES(key,"CBC");
}
    }
    return tripleDES;
    }
    
    public TripleDES(String key,String mode) {        
        if("CBC".equals(mode)) {
        if (StringUtils.isEmpty(key)) {
        key = "GDAU8xpYaWivfJS1R5NGFotZwY8tt6KH";
        }
        byte[] keyData = Base64.decode(key);
            try {
cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace ();
} catch (NoSuchPaddingException e) {
e.printStackTrace ();
}
            secretKey = new SecretKeySpec(keyData, KEY_ALGORITHM);
            iv = new IvParameterSpec(DefaultIV);
        }  
    }
    
    /**
     * Initial vector 8 bits
     * @return
     */
    byte[] getIV() {  
    return DefaultIV;
    }
    
    /** 
     * 加密 
     * @param str 
     * @return byte[]
     * @throws Exception 
     */  
    public byte[] encrypt(String str) throws Exception {
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);  
        return encryptData = cipher.doFinal(str.getBytes("utf-8"));  
    }
    
    /** 
     * 解密 
     * @param encrypt 
     * @return byte[]
     * @throws Exception 
     */  
    public byte[] decrypt(byte[] encrypt) throws Exception {  
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);  
        return encryptData = cipher.doFinal(encrypt);  
    }
    
    /** 
     * TripleDes三层加密,并且base64编码
     * 默认utf-8
     * @param str 
     * @return String
     * @throws Exception 
     */  
    public String encryptStr(String str) throws Exception {  
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        encryptData = cipher.doFinal(str.getBytes("utf-8"));
        return Base64.encodeToString(encryptData);
    }
    /** 
     * 解密 返回字符串 ,,并且base64解码
     * 默认utf-8
     * @param encrypt 
     * @return String
     * @throws Exception 
     */  
    public String decryptStr(String data) throws Exception {
    byte[] encrypt = Base64.decode(data);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        encryptData = cipher.doFinal(encrypt);
        return new String(encryptData,"UTF-8");
    }
    public static void main(String[] args) throws Exception {  
        TripleDES tripleDES = TripleDES.getInstance("dgKU8xpYaRivfJS1R5NGFotZwY6lI6jI");
        Test1();
//        String data = "\"address\":\"北京市\",\"applyNo\":\"09ecd729d09e461bb9ad0329ecddf163\",\"applyReason\":\"测试\",\"creditAmount\":10,\"creditReason\":\"系统通过\",\"ext\":{},\"idCard\":\"101123\",\"jdPin\":\"washingtin\",\"name\":\"X_man\",\"phone\":\"15520059911\",\"reqDate\":\"20160823135900\",\"returnParams\":\"\",\"riskRemark\":\"无\"";
//        System.out.println(HashUtils.MD5(data));
//        tripleDES.testEncrypt();
        tripleDES.testDecrypt();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326151108&siteId=291194637
Recommended