DES3 CBC加解密

DES3 CBC加解密

package com.qubingwei.test;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;



public class Demo {
    public static void main(String[] args)throws Exception {

        //密钥
        byte[] key= Base64.decode("12345678901234567890123456789000");
        byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8 };
        //原始数据
        String mainStr = "达达ABCacb123";
        //原始数据转byte
        byte[] data = mainStr.getBytes("UTF-8");
        System.out.println("原始数据:" + mainStr);

        System.out.println("----CBC加密解密开始----");
        //加密后的byte[]
        byte[] encodeByte_CBC = des3EncodeCBC(key, keyiv,data);
        //加密后的String   需把加密的byte[]转base64
        String encodeString_CBC =Base64.encode(encodeByte_CBC);
        System.out.println("加密的数据是:" + encodeString_CBC);
        //解密后的原始byte[] 需把加密后的byte[]转bass64
        byte[] decodeByteMain_CBC=Base64.decode(encodeString_CBC);
        //解密后的byte[]
        byte[] decodeByte_CBC = des3DecodeCBC(key, keyiv, decodeByteMain_CBC);
        //解密后的String
        String decodeString_CBC = new String(decodeByte_CBC, "UTF-8");
        System.out.println("解密后的数据是:" + decodeString_CBC);
    }


    public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] bOut = cipher.doFinal(data);

        return bOut;
    }


    public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);

        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);

        byte[] bOut = cipher.doFinal(data);

        return bOut;

    }
}

JS實現

//秘钥key
var key = "12345677654321";
//key不足24位自动以0(最小位数是0)补齐,如果多余24位,则截取前24位,后面多余则舍弃掉
var base64 = CryptoJS.enc.Utf8.parse(key)
//加密使用的是3DES中的ECB,解密对应的使用ECB
function encrypt() {
    var text = document.getElementById('content').value+'';
    var encrypt = CryptoJS.TripleDES.encrypt(text, base64, {
            iv: CryptoJS.enc.Utf8.parse('0123456789'),//iv偏移量
            mode: CryptoJS.mode.CBC,  //CBC模式
            // mode: CryptoJS.mode.ECB,  //ECB模式
            padding: CryptoJS.pad.Pkcs7//padding处理
        }
    );
    var encryptData = encrypt.toString();//加密完成后,转换成字符串
    document.getElementById("encrypt").innerText=encryptData;
}
//解密
function decrypt() {
    var text = document.getElementById("encrypt").innerText;
    var decrypt = CryptoJS.TripleDES.decrypt(text, base64, {
            iv: CryptoJS.enc.Utf8.parse('0123456789'),
            mode: CryptoJS.mode.CBC,
            // mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        }
    );
    //解析数据后转为UTF-8
    var parseData = decrypt.toString(CryptoJS.enc.Utf8);
    document.getElementById("decrypt").innerText=parseData;
}

https://www.jb51.net/article/140418.htm

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页