AES128加密算法(java版)

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

public class AES128Test {

    public static void main(String[] args) {
        aes128("123456789ABCDEFG", "1234567891234567");
    }


    public static byte[] aes128(String text, String key) {
        if (key == null || text == null || key.getBytes().length != 16 || text.getBytes().length != 16) {
            System.out.println("encrypt: key or text error");
            return null;
        }
        try {
            //AES128(calculate with key and text)
            Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            encCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
            byte[] encrypted = encCipher.doFinal(text.getBytes());
            for (int i = 0; i < encrypted.length; i++) {
                System.out.println( "result: " + encrypted[i]);
            }
            System.out.println(bytesToHex(encrypted));
            return encrypted;
        } catch (Exception e) {
            System.out.println(e.toString());
            return null;
        }
    }

    final protected static char[] hexArray = "0123456789abcdef".toCharArray();
    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for ( int j = 0; j < bytes.length; j++ ) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java中可以使用javax.crypto包来实现AES-128加密算法。下面是一个简单的示例代码: ```java 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 plaintext, String key) throws Exception { SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encryptedBytes); } public static String decrypt(String ciphertext, String key) throws Exception { SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(ciphertext)); return new String(decryptedBytes, StandardCharsets.UTF_8); } } ``` 使用示例: ```java public class Main { public static void main(String[] args) { try { String plaintext = "Hello, World!"; String key = "0123456789abcdef"; // 16字节的密钥 String ciphertext = AESUtil.encrypt(plaintext, key); System.out.println("加密后的密文:" + ciphertext); String decryptedText = AESUtil.decrypt(ciphertext, key); System.out.println("解密后的明文:" + decryptedText); } catch (Exception e) { e.printStackTrace(); } } } ``` 注意:在实际使用中,应该使用更安全的方式来存储密钥,例如使用密钥管理系统(Key Management System)来保护密钥的安全性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yinhunzw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值