【练习】基于Cipher实现DES加密和解密

  Java中Cipher类主要提供加密和解密的功能,该类位于javax.crypto包下,声明为public class Cipher extends Object,它构成了Java Cryptographic Extension(JCE)框架的核心。使用Cipher类时,需构建Cipher对象,再调用Cipher的getInstance方法来实现。

package org.example;

import com.google.common.base.Strings;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;


public class DeEnCoderCipherUtil {
    private final static String CIPHER_MODE = "DES";
    public static String DEFAULT_DES_KEY =
            "区块链是分布式数据存储、点对点传输、共识机制、加密算法等计算机技术的新型应用模式。";

    /**
     * 加密通用方法
     * @param originalContent 明文
     * @param key 加密密钥
     * @return 密文
     */
    public static String encrypt(String originalContent, String key) {
        if (Strings.isNullOrEmpty(originalContent) || Strings.isNullOrEmpty(key)) {
            return null;
        }
        try {
            byte[] byteContent = encrypt(originalContent.getBytes(), key.getBytes());
            return new BASE64Encoder().encode(byteContent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }

    /**
     * 字节加密方法
     * @param oringianlContent 明文
     * @param key 加密密钥的byte数组
     * @return 密文的byte数组
     */
    private static byte[] encrypt(byte[] oringianlContent, byte[] key)
        throws Exception {
        //1.生成可信任的随机数源
        SecureRandom sercureRandom = new SecureRandom();
        //2.基于密钥数据创建DESKeySpec对象
        DESKeySpec desKeySpec = new DESKeySpec(key);
        //3.创建密钥工厂,将DESKeySpec转换成SecretKey对象来保存对称密钥
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(CIPHER_MODE);
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        //4.Cipher对象实际完成加密操作,指定其支持指定的加密和解密算法
        Cipher cipher = Cipher.getInstance(CIPHER_MODE);
        //5.用密钥初始化Cipher对象,ENCRYPT_MODE表示加密模式
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, sercureRandom);
        //返回密文
        return cipher.doFinal(oringianlContent);
    }


    public static String decrypt(String ciphertext, String key) {
        if (Strings.isNullOrEmpty(ciphertext) || Strings.isNullOrEmpty(key)) {
            return null;
        }
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] bufCiphertext = decoder.decodeBuffer(ciphertext);
            byte[] contentByte = decrypt(bufCiphertext, key.getBytes());
            return new String(contentByte);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }

    /**
     * 字节解密方法
     * @param ciphertextByte 字节密文
     * @param key 解密密钥byte数组
     * @return 明文byte数组
     */
    private static byte[] decrypt(byte[] ciphertextByte, byte[] key)
        throws Exception{
        //1.生成可信的随机数源
        SecureRandom sercureRandom = new SecureRandom();
        //2.基于密钥数据创建DESKeySpec对象
        DESKeySpec desKeySpec = new DESKeySpec(key);
        //3.创建密钥工厂,将DESKeySpec转换成SecretKey对象来保存对称密钥
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(CIPHER_MODE);
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        //4.Cipher对象实际完成解密操作,指定其支持指定的加密和解密算法
        Cipher cipher = Cipher.getInstance(CIPHER_MODE);
        //5.用密钥初始化Cipher对象,DECRYPT_MODE表示加密模式
        cipher.init(Cipher.DECRYPT_MODE, secretKey, sercureRandom);
        //返回密文
        return cipher.doFinal(ciphertextByte);
    }
}

你可能感兴趣的:(密码学)