AES加密java实现

CTR模式



import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class CryptoAes {
    public static final String KEY_ALGORITHM = "AES";
    public static final String CIPHER_ALGORITHM = "AES/CTR/PKCS5Padding";
    public static final String ivParameter = "8888888888888888";

    public static String Encrypt(String key, String text) {
        try {
            byte[] keyBytes = key.getBytes();
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            SecretKeySpec sKeySpec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv);
            byte[] encrypted = cipher.doFinal(text.getBytes("utf-8"));
            return new BASE64Encoder().encode(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String Decrypt(String Key, String text) {
        try {
            byte[] raw = Key.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(text);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "utf-8");
            return originalString;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    public static void main(String[] args) {
        String encrypt = Encrypt("88888888888888888888888888888888", "zhangsan");
        System.out.println(encrypt);
        System.out.println(Decrypt("88888888888888888888888888888888",encrypt));
    }
}
ECB模式


import java.math.BigInteger;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

public class CryptoAes {
    private static final String KEY = "8888888888888888";
    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";

    public CryptoAes() {
    }

    public static String aesDecrypt(String encrypt) {
        try {
            return aesDecrypt(encrypt, "8888888888888888");
        } catch (Exception var2) {
            var2.printStackTrace();
            return "";
        }
    }

    public static String aesEncrypt(String content) {
        try {
            return aesEncrypt(content, "8888888888888888");
        } catch (Exception var2) {
            var2.printStackTrace();
            return "";
        }
    }

    public static String binary(byte[] bytes, int radix) {
        return (new BigInteger(1, bytes)).toString(radix);
    }

    public static String base64Encode(byte[] bytes) {
        return Base64.encodeBase64String(bytes);
    }

    public static byte[] base64Decode(String base64Code) throws Exception {
        return StringUtils.isEmpty(base64Code) ? null : Base64.decodeBase64(base64Code);
    }

    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(1, new SecretKeySpec(encryptKey.getBytes(), "AES"));
        return cipher.doFinal(content.getBytes("utf-8"));
    }

    public static String aesEncrypt(String content, String encryptKey) {
        try {
            if (!StringUtils.isEmpty(encryptKey) && !StringUtils.isEmpty(content)) {
                if (encryptKey.length() < 16) {
                    encryptKey = encryptKey + "8888888888888888";
                    encryptKey = encryptKey.substring(0, 16);
                }

                return base64Encode(aesEncryptToBytes(content, encryptKey));
            } else {
                return null;
            }
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(2, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        byte[] decryptBytes = cipher.doFinal(encryptBytes);
        return new String(decryptBytes);
    }

    public static String aesDecrypt(String encryptStr, String decryptKey) {
        try {
            if (!StringUtils.isEmpty(decryptKey) && !StringUtils.isEmpty(encryptStr)) {
                if (decryptKey.length() < 16) {
                    decryptKey = decryptKey + "8888888888888888";
                    decryptKey = decryptKey.substring(0, 16);
                }

                return aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
            } else {
                return null;
            }
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        String content = "888888";
        System.out.println(aesEncrypt(content, "1"));
        System.out.println(aesDecrypt(aesEncrypt(content, "1"), "1"));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现AES加密的代码示例: 1. 导入相关包 ```java import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; ``` 2. 定义加密方法 ```java public static String encrypt(String input, String key) throws Exception { byte[] inputBytes = input.getBytes("UTF-8"); // 16字节密钥 byte[] keyBytes = key.getBytes("UTF-8"); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); // ECB模式 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] encrypted = cipher.doFinal(inputBytes); // 转换为Base64编码 return Base64.getEncoder().encodeToString(encrypted); } ``` 3. 定义解密方法 ```java public static String decrypt(String encryptedInput, String key) throws Exception { // 解码Base64 byte[] encryptedBytes = Base64.getDecoder().decode(encryptedInput); // 16字节密钥 byte[] keyBytes = key.getBytes("UTF-8"); SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); // ECB模式 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] decrypted = cipher.doFinal(encryptedBytes); return new String(decrypted, "UTF-8"); } ``` 4. 示例使用 ```java public static void main(String[] args) throws Exception { String input = "Hello World"; String key = "0123456789abcdef"; // 加密 String encrypted = encrypt(input, key); System.out.println("Encrypted: " + encrypted); // 解密 String decrypted = decrypt(encrypted, key); System.out.println("Decrypted: " + decrypted); } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值