java实现aes/ecb/pkcspadding7

调用方式:

        // 去除JCE加密限制,只限于Java1.8
        try {
            Field field = Class.forName("javax.crypto.JceSecurity").getDeclaredField("isRestricted");
            field.setAccessible(true);
            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
            field.set(null, false);
        } catch (ClassNotFoundException | NoSuchFieldException | 
            SecurityException | IllegalArgumentException | IllegalAccessException ex) {

        }

            byte [] newkey = new byte[32];
            System.arraycopy(key.getBytes(), 0, newkey, 0, key.getBytes().length);
        	for(int i = key.length() ;i < 32;i ++){
        		newkey[i] = (byte) 0x20;
        	}
            
	          String decodeTest="";
		try {
			String text = "0qZc/yQqCkPwMnWkzht4t520pn9IIo9yKciYvuXFCt4=";
			decodeTest = decrypt( text,newkey);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	          System.out.println("解密结果: " + decodeTest);




函数定义:


    private static final String KEY_ALGORITHM = "AES";

    private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";

    public static final String ENCODING = "UTF-8";
    
	private  static String key = "Z016770e6f4f91.83Abez23*";
    private  static byte[] m_iv = 
    	{0x12,0x34,0x56,0x78,(byte) 0x90,(byte) 0xab,(byte) 0xcd,(byte) 0xef,
    	0x12,0x34,0x56,0x78,(byte) 0x90,(byte) 0xab,(byte) 0xcd,(byte) 0xef};

    static {
        // 是PKCS7Padding填充方式,则需要添加Bouncy Castle支持
        Security.addProvider(new BouncyCastleProvider());
    }

    public static String decrypt(String str, byte[] key) throws Exception {
        try {
            Field field = Class.forName("javax.crypto.JceSecurity").getDeclaredField("isRestricted");
            field.setAccessible(true);
            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
            field.set(null, false);
            
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"));
            byte[] doFinal = cipher.doFinal(Base64.getDecoder().decode(str));
            return new String(doFinal);
        } catch (ClassNotFoundException | NoSuchFieldException | 
            SecurityException | IllegalArgumentException | IllegalAccessException ex) {

        }
        return "";
    }

"AES/ECB/PKCS7Padding"加密方式需要Bouncy castle包的支持,安装Bouncy castle参考以下链接:https://blog.csdn.net/qq_41546054/article/details/120766852

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是Java实现AES/ECB/PKCS5Padding加密和解密的示例代码: ``` import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.util.Base64; public class AESUtil { private static final String ALGORITHM = "AES"; private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding"; // 加密 public static String encrypt(String data, String key) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] encrypted = cipher.doFinal(data.getBytes()); return Base64.getEncoder().encodeToString(encrypted); } // 解密 public static String decrypt(String encryptedData, String key) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encryptedData)); return new String(decrypted); } } ``` 使用示例: ``` public static void main(String[] args) throws Exception { String data = "Hello, world!"; String key = "0123456789abcdef"; String encryptedData = AESUtil.encrypt(data, key); String decryptedData = AESUtil.decrypt(encryptedData, key); System.out.println("加密前:" + data); System.out.println("加密后:" + encryptedData); System.out.println("解密后:" + decryptedData); } ``` 输出: ``` 加密前:Hello, world! 加密后:qVeqhVSSSmYZQgz6H+JYUA== 解密后:Hello, world! ``` 注意事项: 1. key 长度必须为16位。 2. 为了方便起见,这里使用了 Base64 编码。如果需要更高的安全性,可以使用 Hex 编码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值