java fortify弱密码漏洞使用AES的GCM解决

java_fortify Scan:

问题:
  1.Weak Encryption
  2.Weak Encryption: Insecure Mode of Operation

问题解释:
  问题1.程序使用了弱加密算法,无法保证敏感数据的保密性。
  问题2.使用某加密方式的某不安全加密模式,例如:AES的ECB模式不安全
  注:本人使用了DES加密导致的问题1,使用了AES的ECB模式导致出现问题2

解决方案:
  使用某安全加密方式的某种安全加密模式,例如:AES的GCM模式

使用AES的GCM模式工具类(JDK1.8):

 

package com.test.common.util;

import javax.crypto.*;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import java.util.Base64;

/**
 * 
 * AES加密工具类(GCM模式)
 */
public class AesUtils {
    private static final String ALGORITHM = "AES";
    private static final String AES_GCM_PADDING = "AES/GCM/NoPadding";

    /**
     * Base64 加密
     *
     * @param plainBytes
     * @return
     */
    public static byte[] encodeByBase64(byte[] plainBytes) {
        if (plainBytes == null) {
            return null;
        }
        return Base64.getEncoder(
package cn.axa.ams.util; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.util.Base64; /** * AES算法工具类 * * @author - ex_liuxiang * @date - 2018-10-29 17:46 */ public class AESUtil { private static final String ENCODE_CHARSET = "GBK"; private static final String ENCODE_ALGORITHM = "AES"; private static final String ENCODE_TRANSFORMATION = "AES/ECB/PKCS5Padding"; /** * AES加密转16进制(只包含字母和数字) * * @param rule(长度仅支持16bytes) * @param content 明文 * @return String * @author ex_liuxiang */ @Deprecated public static String AESEncodeWithHex(String rule, String content) throws Exception { byte[] result = cipher(buildSecretKey(rule), content.getBytes(ENCODE_CHARSET), Cipher.ENCRYPT_MODE); return bytes2Hex(result); } /** * 16进制字符AES解密 * * @param rule 加密规则(长度仅支持16bytes) * @param content 密文 * @return String * @author ex_liuxiang */ @Deprecated public static String AESDecodeWithHex(String rule, String content) throws Exception { byte[] result = cipher(buildSecretKey(rule), hex2Bytes(content), Cipher.DECRYPT_MODE); return new String(result, ENCODE_CHARSET); } /** * AES加密 - 推荐使用AESEncodeWithHex() * * @param rule(长度仅支持16bytes) * @param content 明文 * @return String * @author ex_liuxiang */ public static String AESEncode(String rule, String content) throws Exception { byte[] result = cipher(buildSecretKey(rule), content.getBytes(ENCODE_CHARSET), Cipher.ENCRYPT_MODE); return Base64.getEncoder().encodeToString(result); } /** * AES解密 * * @param rule 加密规则(长度仅支持16bytes) * @param content 密文 * @return String * @author ex_liuxiang */ public static String AESDecode(String rule, String content) throws Exception { byte[] contentBytes = Base64.getDecoder().decode(content); byte[] result = cipher(buildSecretKey(rule), contentBytes, Cipher.DECRYPT_MODE); return new String(result, ENCODE_CHARSET); } /** * byte数组转16进制字符串 * * @param bytes byte数组 * @return String * @author ex_liuxiang */ @Deprecated private static String bytes2Hex(byte[] bytes) { StringBuffer sb = new StringBuffer(bytes.length); String hex; for (byte b : bytes) { hex = Integer.toHexString(0xFF & b); if (hex.length() < 2) { sb.append(0); } sb.append(hex.toUpperCase()); } return sb.toString(); } /** * 16进制字符串转byte数组 * * @param hex 16进制字符串 * @return byte[] * @author ex_liuxiang */ @Deprecated private static byte[] hex2Bytes(String hex) { String str = "0123456789ABCDEF"; char[] hexs = hex.toCharArray(); byte[] bytes = new byte[hex.length() / 2]; int n; for (int i = 0; i < bytes.length; i++) { n = str.indexOf(hexs[2 * i]) * 16; n += str.indexOf(hexs[2 * i + 1]); bytes[i] = (byte) (n & 0xff); } return bytes; } /** * 构建密钥 * * @param rule 加密规则(长度仅支持16bytes) * @return SecretKeySpec * @author ex_liuxiang */ private static SecretKeySpec buildSecretKey(String rule) throws UnsupportedEncodingException { return new SecretKeySpec(rule.getBytes(ENCODE_CHARSET), ENCODE_ALGORITHM); } /** * 加密明文(解密密文) * * @param secretKey 密钥 * @param input 明文(密文) * @param mode 加密:Cipher.ENCRYPT_MODE,解密:Cipher.DECRYPT_MODE * @return byte[] * @author ex_liuxiang */ public static byte[] cipher(SecretKeySpec secretKey, byte[] input, int mode) throws Exception { Cipher cipher = Cipher.getInstance(ENCODE_TRANSFORMATION); cipher.init(mode, secretKey); return cipher.doFinal(input); } } 该java工具类使用的是AES的ECB模式进行加解密处理,现在被fortify扫描出来Weak Encryption: Insecure Mode of Operation漏洞,推荐使用AESGCM模式进行加密。现在要将该工具类调整为使用AESGCM模式加密的应该怎么调整该类
最新发布
12-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值