页面提交数据的加密解密

查看页面详情的加密解密:

<script src="${webRoot}/js/crypto/crypto-js.js"></script>

function showFun(pkid){
        console.log(pkid);
        $.get('${webRoot}/mpi/mpiMall/prepare.shtml?'+pkid,function(k,status){
            console.log(status);
            if(status=='success'){
                k = eval(k);
                var u = encrypt(pkid,k);
                var dialog = jsutil.modalDialog({
                       title : '详情',
                       width: 700,    
                    height: 400,   
                       maximizable:true,
                       url : actionPath + '/view?u='+u
                   });
                
            }
            
        });
    }
    
    /**
     * 加密(需要先加载lib/aes/aes.min.js文件)
     * @param word
     * @returns {*}
     */
    function encrypt(word,keyStr){
        var key = CryptoJS.enc.Utf8.parse(keyStr);
        var srcs = CryptoJS.enc.Utf8.parse(word);
        var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
        return encrypted.toString();
    }

 

@PrivaligeAnnotation(code = CODE + VIEW, des = "查看详情")
    @RequestMapping(value ="/view",method = RequestMethod.GET)
    public String view(StUser stUser,HttpServletRequest request){
        logger.info("执行进入查看详情页面: MpiMallController.view():stUser==>" + stUser);
        try {
            //解密得到真实的pkid
            String usercode = stUser.getU();
            logger.info("得到的usercode:" + usercode);
            String cacheKey = key_prefix + usercode;
            CacheVo cache = cacheService.get(cacheKey);
            String key = (String) cache.getValue();
            //usercode就是解密后的pkid
            usercode = SecuryUtil.aesDecrypt(usercode, key);
            
            MpiMall mpiMall = this.mpiMallService.selectById(usercode);
            request.setAttribute("mpiMall", this.parseRenderJSONObject(mpiMall));
        } catch (Exception e) {
            logger.error("执行查看失败" , e);
        }
        logger.info("执行修改前: MpiMallController.view()完毕");
        return "/mall/mpiMall/view";
    }
    
    @PrivaligeAnnotation(code = CODE + VIEW, des = "预查询")
    @RequestMapping(value ="/prepare",method = RequestMethod.GET)
    @ResponseBody
    public String prepare(HttpServletRequest request){
        String key = UUIDTool.getUUID().substring(16);
        String pkid = request.getQueryString();
        try {
            pkid = SecuryUtil.aesEncrypt(pkid, key);
        } catch (Exception e) {
            logger.error("加密失败!");
            e.printStackTrace();
        }
        CacheVo cacheVo = new CacheVo();
        cacheVo.setKey(key_prefix + pkid);
        cacheVo.setValue(key);
        cacheService.add(cacheVo);
        return key;
    }

 

/**  
     * 自动生成32位的UUid,对应数据库的主键id进行插入用。  
     * @return  
     */  
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");  
    }

 

/**
     * AES加密为base 64 code
     * @param content 待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的base 64 code
     * @throws Exception
     */
    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return base64Encode(aesEncryptToBytes(content, encryptKey));
    }

    /**
     * base 64 encode
     * @param bytes 待编码的byte[]
     * @return 编码后的base 64 code
     */
    public static String base64Encode(byte[] bytes){
        return org.apache.commons.codec.binary.Base64.encodeBase64String(bytes);
    }

 

/**
     * AES加密
     * @param content 待加密的内容
     * @param encryptKey 加密密钥
     * @return 加密后的byte[]
     * @throws Exception
     */
    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));

        return cipher.doFinal(content.getBytes("utf-8"));
    }

 

/**
     * 将base 64 code AES解密
     * @param encryptStr 待解密的base 64 code
     * @param decryptKey 解密密钥
     * @return 解密后的string
     * @throws Exception
     */
    public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
        return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
    }

    /**
     * AES解密
     * @param encryptBytes 待解密的byte[]
     * @param decryptKey 解密密钥
     * @return 解密后的String
     * @throws Exception
     */
    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);

        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

 

 

在jsp页面加密,在controller.java解密参考

https://blog.csdn.net/fangdengfu123/article/details/70140162/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值