java实现AES加密和解密算法

在日常接口对接中,不可避免的要与各种加密算法打交道,AES也是较为常用的一种加密算法,下面直接上代码
加密算法:


private static final String KEY_ALGORITHM = "AES";
private static final String CIPHER_ALGORITHM_CBC = "AES/CBC/NoPadding";

/**
 * 加密
 *
 * @param content  需要加密的内容
 * @param password 加密密码
 * @return
 */
public static String encrypt(String content, String password)
    {
        try
        {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = content.getBytes("UTF-8");
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0)
            {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
            SecretKeySpec keyspec = new SecretKeySpec(password.getBytes(), KEY_ALGORITHM);
            IvParameterSpec ivspec = new IvParameterSpec(password.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
            byte[] msgBytes = Base64.encodeBase64(encrypted);
            return new String(msgBytes);
        }
        catch (Exception ex)
        {
             ex.printStackTrace();
            return null;
        }

    }

解密算法:


private static final String KEY_ALGORITHM = "AES";
private static final String CIPHER_ALGORITHM_CBC = "AES/CBC/NoPadding";

/**
 * 解密
 *
 * @param content  待解密内容
 * @param password 解密密钥
 * @return
 */
public static String decrpyt(String content, String password)
    {
        try
        {
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC);
            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = Base64.decodeBase64(content);
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0)
            {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
            SecretKeySpec keyspec = new SecretKeySpec(password.getBytes(), KEY_ALGORITHM);
            IvParameterSpec ivspec = new IvParameterSpec(password.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
            return new String(encrypted, "UTF-8");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }

测试代码:

public static void main(String[] args) {
        String content = "abcdefg";
        String password ="1234567812345678";
        String result = encrypt(content, password);
        System.out.println("加密结果为:"+result);
        String decrpyt = decrpyt(result, password);
        System.out.println("解密结果为:"+decrpyt);   
    }

测试结果:
这里写图片描述
需要注意的一点是此处采用加密方式是CBC,加密的key要为16位

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 Java 实现 AES 加密算法的示例代码: ```java import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.util.Base64; public class AESUtil { public static String encrypt(String plaintext, String key, String iv) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] encrypted = cipher.doFinal(plaintext.getBytes()); return Base64.getEncoder().encodeToString(encrypted); } public static String decrypt(String ciphertext, String key, String iv) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); byte[] decoded = Base64.getDecoder().decode(ciphertext); byte[] decrypted = cipher.doFinal(decoded); return new String(decrypted, StandardCharsets.UTF_8); } public static void main(String[] args) throws Exception { String plaintext = "Hello, world!"; String key = "0123456789abcdef"; String iv = "fedcba9876543210"; String ciphertext = encrypt(plaintext, key, iv); System.out.println("Ciphertext: " + ciphertext); String decrypted = decrypt(ciphertext, key, iv); System.out.println("Decrypted: " + decrypted); } } ``` 注意:这只是一个简单的示例代码,实际使用中应该考虑更多的安全因素,比如密钥的生成和管理、IV 的随机性等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值