使用des对称轴加密,JS前端加密,Java后端解密

DES加密 专栏收录该内容
1 篇文章 0 订阅

JS前端代码:

引入JS
 <script type="text/javascript" src="js/core.js" charset="utf-8"></script>
 <script type="text/javascript" src="js/cipher-core.js" charset="utf-8"></script>
 <script type="text/javascript" src="js/mode-ecb.js" charset="utf-8"></script>
 <script type="text/javascript" src="js/tripledes.js" charset="utf-8"></script>


//加密的私钥
var key = '12345678';//自定义

// DES加密
function encryptByDES(message) {//传入加密的内容
   //把私钥转换成16进制的字符串
   var keyHex = CryptoJS.enc.Utf8.parse(key);
   //模式为ECB padding为Pkcs7
   var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
       mode: CryptoJS.mode.ECB,
       padding: CryptoJS.pad.Pkcs7
   });
   //加密出来是一个16进制的字符串
   return encrypted.ciphertext.toString();
}

Java后台解密

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Locale;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;


public class DESUtils {

    /**
     * DES解密
     *
     * @param secretData 密码字符串
     * @param secretKey  解密密钥
     * @return 原始字符串
     * @throws Exception
     */
    private static final String DES_ALGORITHM = "DES";

    public static String decryption(String secretData, String secretKey) throws Exception {
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("DES/ECB/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, generateKey(secretKey));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new Exception("NoSuchAlgorithmException", e);
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
            throw new Exception("NoSuchPaddingException", e);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {

            byte[] buf = cipher.doFinal(hexStr2Bytes(secretData));
            int num = 0;
            for (byte b: buf) {
                String name = b+"";
                if (name.length() == 1) {
                    num++;
                }
            }

            byte[] bytes = new byte[buf.length-num];
            for (int i =0; i < buf.length; i++) {
                String name = buf[i]+"";
                if (name.length() != 1) {
                    bytes[i] = buf[i];
                }
            }

            return new String(bytes, "utf-8");

        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("IllegalBlockSizeException", e);
        }
    }

    public static byte[] hexStr2Bytes(String src) {
        /*对输入值进行规范化整理*/
        src = src.trim().replace(" ", "").toUpperCase(Locale.US);
        //处理值初始化
        int m = 0, n = 0;
        int iLen = src.length() / 2; //计算长度
        byte[] ret = new byte[iLen]; //分配存储空间

        for (int i = 0; i < iLen; i++) {
            m = i * 2 + 1;
            n = m + 1;
            ret[i] = (byte) (Integer.decode("0X" + src.substring(i * 2, m) + src.substring(m, n)) & 0xFF);
        }
        return ret;
    }

    /**
     * 获得秘密密钥
     *
     * @param secretKey
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     */
    private static SecretKey generateKey(String secretKey)
            throws Exception {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
        DESKeySpec keySpec = new DESKeySpec(secretKey.getBytes());
        keyFactory.generateSecret(keySpec);
        return keyFactory.generateSecret(keySpec);
    }
}

 

  • 1
    点赞
  • 3
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值