3DES加解密源码实践


import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.*;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 *
 * 3DES加解密简介
 * DES使用分块加密,加密密钥为56位,块大小为64位。由于56位版本的DES加密非常容易破解(一般机器一天以内),就有了和DES兼容性较好的3DES算法。
 *
 * 原理
 * 3DES加密算法表示为C=EncryptK3(DecryptK2(EncryptK1(message),如果K1、K2、K3为密钥,如果各不相同,则相当于加密密钥长度为112(由于中途相遇攻击);解密算法表示为message=DecryptK1((EncryptK2(DecryptK3(C)))
 *
 * 以加密算法举例:
 *
 * 使用了3次DES算法,有3个密钥
 * 加密时第一个密钥K1用来加密消息(P),输出C1密文
 * 第二个密钥K2用来解密C1,输出C2密文
 * 第三个密钥K3用来加密C2,输出C3密文
 *
 * */
@Component
public class DES3Util {


    private final static String secretKey ="njxtqgjyptfrom0123456789";
    private final static String iv ="87654321";
    private final static String encoding ="UTF-8";



    public static String encrypt(String text) {
        try {
            DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("desede");
            Key destKey = keyFactory.generateSecret(spec);
            Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, destKey,ips);
            byte[] cipherText = cipher.doFinal(text.getBytes(encoding));
            BASE64Encoder b64encoder = new BASE64Encoder();
            return b64encoder.encode(cipherText);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }

        return null;
    }
    public static String decrypt(String decryptedText ){

        try {

            BASE64Decoder b64decoder = new BASE64Decoder();
            byte[] textBs = b64decoder.decodeBuffer(decryptedText);
            DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("desede");
            Key destKey = keyFactory.generateSecret(spec);
            Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");

            IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, destKey,ips);
            byte[] cipherText = cipher.doFinal(textBs);
            return new String(cipherText,encoding);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }

        return null;
    }


    public static void main(String arg[]) {
        String wen = "orcle";

        System.out.println(encrypt(wen));

        System.out.println(decrypt(encrypt(wen)));

    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值