如何用java实现AES加密和解密

什么是AES算法?

AES算法是一种对称加密算法,也就是说,加密和解密使用的是同一个密钥。它是美国国家标准技术研究所(NIST)在2001年选定的一种高级加密标准(Advanced Encryption Standard),用来替代之前的DES算法。AES算法的优点是安全性高、效率高、易于实现、不需要专利授权等。

AES算法的原理是什么?

AES算法的基本思想是将明文分成固定长度的分组(128位),然后对每个分组进行多轮的变换,最终得到密文。每一轮的变换包括四个步骤:字节代换(SubBytes)、行移位(ShiftRows)、列混合(MixColumns)和轮密钥加(AddRoundKey)。其中,轮密钥是从初始密钥(128位、192位或256位)通过一系列的运算生成的。不同长度的初始密钥决定了不同的轮数(10轮、12轮或14轮)。
字节代换
字节代换是一种非线性的变换,它将每个字节替换为另一个字节,根据一个固定的查找表,称为S盒。S盒是一个16×16的矩阵,每个元素是一个8位的字节。S盒的构造基于有限域上的逆运算和仿射变换,具有良好的抗线性攻击和差分攻击的能力。

行移位

行移位是一种线性的变换,它将每个分组看作一个4×4的矩阵,并对每一行进行循环左移。第一行不移动,第二行左移一个字节,第三行左移两个字节,第四行左移三个字节。这样可以打破每列之间的相关性,增加混乱度。

列混合

列混合也是一种线性的变换,它将每个分组看作一个4×4的矩阵,并对每一列进行矩阵乘法。乘法运算在有限域GF(2 8 )上进行,使用一个固定的4×4矩阵作为乘数。这样可以打破每行之间的相关性,增加扩散度。

轮密钥加

轮密钥加是一种简单的变换,它将每个分组与当前轮密钥进行按位异或运算。轮密钥是从初始密钥通过一个称为密钥扩展算法生成的。该算法利用了S盒、循环左移和常量异或等操作,产生了足够数量和长度的轮密钥。

如何用java实现AES加密和解密?

java提供了javax.crypto包,其中包含了一些用于加密和解密的类和接口。要使用AES算法,我们需要以下几个步骤:

导入javax.crypto包中的相关类,如Cipher、SecretKey、SecretKeySpec等。
创建一个Cipher对象,并指定使用AES算法和加密模式(如ECB、CBC、CFB等)。
创建一个SecretKey对象,并指定使用AES算法和初始密钥。
使用Cipher对象的init方法,初始化为加密或解密模式,并传入SecretKey对象。
使用Cipher对象的doFinal方法,对明文或密文进行加密或解密,并返回字节数组。
如果需要,可以将字节数组转换为字符串或其他格式。
下面是一个简单的示例代码,演示了如何用java实现AES加密和解密:

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

public class AESDemo {

    public static void main(String[] args) throws Exception {
        // 明文
        String plainText = "Hello, world!";
        // 初始密钥
        String key = "1234567890abcdef";
        // 加密模式
        String mode = "AES/ECB/PKCS5Padding";

        // 创建Cipher对象
        Cipher cipher = Cipher.getInstance(mode);
        // 创建SecretKey对象
        SecretKey secretKey = new SecretKeySpec(key.getBytes(), "AES");
        // 初始化为加密模式
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        // 加密明文
        byte[] encrypted = cipher.doFinal(plainText.getBytes());
        // 输出加密后的字节数组
        System.out.println("Encrypted: " + bytesToHex(encrypted));

        // 初始化为解密模式
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        // 解密密文
        byte[] decrypted = cipher.doFinal(encrypted);
        // 输出解密后的字符串
        System.out.println("Decrypted: " + new String(decrypted));
    }

    // 将字节数组转换为十六进制字符串
    public static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
}

输出结果:

Encrypted: 8b0a493c06f9
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用javax.crypto包的类来进行AES加密解密。具体实现步骤如下: 1. 生成一个AES密钥 ``` KeyGenerator keygen = KeyGenerator.getInstance("AES"); keygen.init(128); SecretKey key = keygen.generateKey(); ``` 2. 创建Cipher对象,并初始化为加密模式或解密模式 ``` Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); ``` 3. 加密/解密数据 ``` byte[] ciphertext = cipher.doFinal(plaintext); byte[] plaintext = cipher.doFinal(ciphertext); ``` 完整代码示例: ``` import javax.crypto.*; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; public class AESUtil { private static final String CHARSET_NAME = "UTF-8"; private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding"; /** * 随机生成AES密钥 * * @param keySize 密钥长度,单位:位 * @return 生成的密钥 * @throws NoSuchAlgorithmException 异常 */ public static byte[] generateKey(int keySize) throws NoSuchAlgorithmException { KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(keySize); SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } /** * AES加密 * * @param plaintext 明文 * @param key 密钥 * @param iv 初始化向量 * @return 密文 * @throws NoSuchPaddingException 异常 * @throws NoSuchAlgorithmException 异常 * @throws InvalidAlgorithmParameterException 异常 * @throws InvalidKeyException 异常 * @throws BadPaddingException 异常 * @throws IllegalBlockSizeException 异常 */ public static byte[] encrypt(byte[] plaintext, byte[] key, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance(AES_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(iv)); return cipher.doFinal(plaintext); } /** * AES解密 * * @param ciphertext 密文 * @param key 密钥 * @param iv 初始化向量 * @return 明文 * @throws NoSuchPaddingException 异常 * @throws NoSuchAlgorithmException 异常 * @throws InvalidAlgorithmParameterException 异常 * @throws InvalidKeyException 异常 * @throws BadPaddingException 异常 * @throws IllegalBlockSizeException 异常 */ public static byte[] decrypt(byte[] ciphertext, byte[] key, byte[] iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance(AES_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(iv)); return cipher.doFinal(ciphertext); } /** * 生成随机的初始化向量 * * @param ivSize 初始化向量长度,单位:位 * @return 生成的初始化向量 */ public static byte[] generateIv(int ivSize) { byte[] iv = new byte[ivSize / 8]; new SecureRandom().nextBytes(iv); return iv; } /** * 将字节数组转换为十六进制字符串 * * @param bytes 字节数组 * @return 十六进制字符串 */ public static String toHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(bytes.length * 2); for (byte b : bytes) { sb.append(String.format("%02x", b & 0xff)); } return sb.toString(); } /** * 将十六进制字符串转换为字节数组 * * @param hexString 十六进制字符串 * @return 字节数组 */ public static byte[] toByteArray(String hexString) { int len = hexString.length() / 2; byte[] bytes = new byte[len]; for (int i = 0; i < len; i++) { int index = i * 2; bytes[i] = (byte) Integer.parseInt(hexString.substring(index, index + 2), 16); } return bytes; } public static void main(String[] args) throws Exception { // 随机生成16字节的AES密钥和16字节的初始化向量 byte[] key = generateKey(128); byte[] iv = generateIv(128); String plaintextStr = "hello, AES!"; byte[] plaintext = plaintextStr.getBytes(CHARSET_NAME); // AES加密 byte[] ciphertext = encrypt(plaintext, key, iv); String ciphertextHex = toHexString(ciphertext); System.out.println("密文:" + ciphertextHex); // AES解密 byte[] ciphertextBytes = toByteArray(ciphertextHex); byte[] decrypted = decrypt(ciphertextBytes, key, iv); String decryptedStr = new String(decrypted, CHARSET_NAME); System.out.println("明文:" + decryptedStr); } } 相关问题:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值