DES加密解密

    DES加密生成的是byte[]数组,我们直接new String()转换为String,然后传输到后台,后台解密是将该String字符串使用getBytes()方法转化为byte[],再DES解密,这个过程会使解密失败,工程紧遇到的坑。

    为此,我们在加密过程中采用base64编码生成密文String,然后后端base64解码再解密,即可正确的获得明文。


import sun.misc.BASE64Encoder;

import org.junit.Test;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;
/**
 * @author zhoujin
 * @date 2018-07-04
 */
public class DESEncryptUtil {
    /**
     * 加密以及解密(对称性加密算法)
     * @param datasource byte[]   加密内容
     * @param password   String   加密钥匙
     * @return String  返回加密后结果
     */
    public static String encrypt(byte[] datasource, String password) {
        try {
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(password.getBytes());
            //创建一个密匙工厂,然后用它把DESKeySpec转换成
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            //Cipher对象实际完成加密操作
            Cipher cipher = Cipher.getInstance("DES");
            //用密匙初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            //现在,获取数据并加密
            //正式执行加密操作
            byte[] bytes=cipher.doFinal(datasource);
            //base64编码
            return new BASE64Encoder().encode(bytes);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 解密
     * @param src byte[]       密文
     * @param password String  解密钥匙
     * @return String          解密后结果
     * @throws Exception
     */
    public static String decrypt(byte[] src, String password){
        try {
            // DES算法要求有一个可信任的随机数源
            SecureRandom random = new SecureRandom();
            // 创建一个DESKeySpec对象
            DESKeySpec desKey = new DESKeySpec(password.getBytes());
            // 创建一个密匙工厂
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            // 将DESKeySpec对象转换成SecretKey对象
            SecretKey securekey = keyFactory.generateSecret(desKey);
            // Cipher对象实际完成解密操作
            Cipher cipher = Cipher.getInstance("DES");
            // 用密匙初始化Cipher对象
            cipher.init(Cipher.DECRYPT_MODE, securekey, random);
            // 真正开始解密操作
            byte[] bytes= cipher.doFinal(src);
            return new String(bytes);
        } catch (Throwable e){
            e.getStackTrace();
        }
        return null;

    }

 @Test
    public void test() throws IOException {
        Corporation corporation=new Corporation();
        corporation.setOrganizationCodeURL("http://www.baidu.com");
        String s1=DESEncryptUtil.encrypt(corporation.getOrganizationCodeURL().getBytes(),"9588888888880288");
        String s2=DESEncryptUtil.decrypt(new BASE64Decoder().decodeBuffer(s1),"9588888888880288");
    }

}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值