AES/ECB/PKCS7Padding(AES加密 ECB模式 PKCS7Padding填充)

AES加密 ECB模式 PKCS7Padding填充

AES加密字符串

private static final String ENCODE = "UTF-8";
private static final String FORMAT = "AES";
private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";

**
 * 加密 ECB
 */
    public static String encryption(String content, String secretKey){
        if(secretKey == null) {
            return null;
        }
        if(secretKey.length() != 16) {
            return null;
        }
        try{
       	    Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            byte[] raw = secretKey.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(content.getBytes("utf-8"));
            String hexString =Hex.encodeHexString(encrypted);
            return hexString;
        } catch (Exception e) {
            log.error("解密异常 ",e);
            e.printStackTrace();
        }
        return null;
    }



 /**
 * 解密
 */
public static String decode(String str, String skey){
    try {
        if (str == null) {
            return null;
        }

        byte[] data = Hex.decodeHex(str.toCharArray());
        
		Security.addProvider(new BouncyCastleProvider());
		
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

        SecretKeySpec secretKeySpec = new SecretKeySpec(skey.getBytes(ENCODE), FORMAT);

        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

        return new String(cipher.doFinal(data), ENCODE);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}





***希望帮大家 解决问题 JDK的版本要高,如果帮到了您,希望能点个关注,谢谢***

你可能感兴趣的:(加密模式)