【MVP】Liuxiang Chen

作者是一名技术顾问,专注于微软金质合作伙伴的Windows基础设施设计。热衷于微软服务器系统,特别是Exchange Server和MOM。当前正参与多项微软Beta计划,包括Exchange 12、Office 12、Windows Vista和Longhorn Server等。
主要简历:
大家好I'm a technical consultant in a Microsoft Golden Partner, and responsible for Windows-base infrastructure designing for VIP customers. I'm interesting Microsoft Server System, especially, Exchange Server, MOM are my favoriters. Currently, I'm focus on many Microsoft Beta Program, such as Exchange 12, Office 12, Windows Vista and Longhor Server.
    Since I abhor being idle, so, I always read some whitepaper on MSDN or Technet when I have spare time, include weekend, from there, I get rich information that needed for my job.
     Welcome anyone talk with me, I think I would be your best friend on technical.

Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=769681


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漏洞,推荐使用AES的GCM模式进行加密。现在要将该工具类调整为使用AES的GCM模式加密的应该怎么调整该类
12-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值