【练习】基于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);
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值