• ECB
import sun.misc.BASE64Decoder;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

public class encrypt {

    public static byte[] initSecretKey() throws NoSuchAlgorithmException {
        //指定算法秘钥生成器
        KeyGenerator kg = KeyGenerator.getInstance("des");
        //初始化秘钥生成器,使其具有确定到秘钥大小
        kg.init(56);
        //生成秘钥
        SecretKey secretkey = kg.generateKey();
        return secretkey.getEncoded();
    }

    public static byte[] encrypt(byte[] key, String src) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {
        //jdk初始化秘钥转化des秘钥
        DESKeySpec desKeySpec = new DESKeySpec(key);
        //创建秘钥工厂实例
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des");
        //生成秘钥
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);

        /**
         * 加密实际操作Cipher
         */
        //创建Cipher对象
        Cipher cipher = Cipher.getInstance("des/ecb/PKCS5Padding");
        //初始化Cipher
        cipher.init(Cipher.ENCRYPT_MODE,secretKey);
        byte[] data = src.getBytes();
        //加密
        byte[] encryptedData = cipher.doFinal(data);

    }

    public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, InvalidKeySpecException, InvalidKeyException, IOException {
        //byte[] secretKey = initSecretKey();
        //或者代码中约定key
        String key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";
        byte[] secretKey = new BASE64Decoder().decodeBuffer(key);
        String str = "abc";
        byte[] encryptedData = encrypt(secretKey, str);
        String decrypteData = decrypt(secretKey, encryptedData);
    }

    public static String decrypt(byte[] key,byte[] encryptedData) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {
        //jdk初始化秘钥转化des秘钥
        DESKeySpec desKeySpec = new DESKeySpec(key);
        //创建秘钥工厂实例
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des");
        //生成秘钥
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);

        /**
         * 解密实际操作Cipher
         */
        //创建Cipher对象
        Cipher cipher = Cipher.getInstance("des/ecb/PKCS5Padding");
        //初始化Cipher
        cipher.init(Cipher.DECRYPT_MODE,secretKey);
        //加密
        byte[] dencryptedData = cipher.doFinal(encryptedData);
        return new String(dencryptedData);
    }
}
  • CBC

与ECB差别,加解密都要使用初始化向量,且初始化向量一致

import sun.misc.BASE64Decoder;

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

public class encrypt {

    public static byte[] initSecretKey() throws NoSuchAlgorithmException {
        //指定算法秘钥生成器
        KeyGenerator kg = KeyGenerator.getInstance("des");
        //初始化秘钥生成器,使其具有确定到秘钥大小
        kg.init(56);
        //生成秘钥
        SecretKey secretkey = kg.generateKey();
        return secretkey.getEncoded();
    }

    public static byte[] encrypt(byte[] key, String src,byte[] keyIv) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {
        //jdk初始化秘钥转化des秘钥
        DESKeySpec desKeySpec = new DESKeySpec(key);
        //创建秘钥工厂实例
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des");
        //生成秘钥
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);

        /**
         * 加密实际操作Cipher
         */
        //创建Cipher对象
        Cipher cipher = Cipher.getInstance("des/cbc/PKCS5Padding");
        //创建初始化向量
        IvParameterSpec iv = new IvParameterSpec(keyIv);
        //初始化Cipher
        cipher.init(Cipher.ENCRYPT_MODE,secretKey,iv);
        byte[] data = src.getBytes();
        //加密
        byte[] encryptedData = cipher.doFinal(data);

    }

    public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, InvalidKeySpecException, InvalidKeyException, IOException, InvalidAlgorithmParameterException {
        //byte[] secretKey = initSecretKey();
        //或者代码中约定key
        String key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4";
        byte[] secretKey = new BASE64Decoder().decodeBuffer(key);
        byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8};
        String str = "abc";
        byte[] encryptedData = encrypt(secretKey, str,iv);
        String decrypteData = decrypt(secretKey, encryptedData,iv);
    }

    public static String decrypt(byte[] key,byte[] encryptedData,byte[] keyIv) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
        //jdk初始化秘钥转化des秘钥
        DESKeySpec desKeySpec = new DESKeySpec(key);
        //创建秘钥工厂实例
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des");
        //生成秘钥
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);

        /**
         * 解密实际操作Cipher
         */
        //创建Cipher对象
        Cipher cipher = Cipher.getInstance("des/cbc/PKCS5Padding");
        //创建初始化向量
        IvParameterSpec iv = new IvParameterSpec(keyIv);
        //初始化Cipher
        cipher.init(Cipher.DECRYPT_MODE,secretKey,iv);
        //加密
        byte[] dencryptedData = cipher.doFinal(encryptedData);
        return new String(dencryptedData);
    }
}

DES与3DES同样到工作模式和填充模式相同,它们加解密区别:

  • jdk初始化秘钥转化des秘钥

  DES:

DESKeySpec desKeySpec = new DESKeySpec(key);

  3DES:

DESedeKeySpec spec = new DESedeKeySpec(key);
  • 创建秘钥工厂实例  

  DES:

SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("des");

  3DES:

SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");

  

版权声明:本文为ivy-xu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/ivy-xu/p/12297194.html