Java:使用javax.crypto.Cipher的AES算法实现数据加密解密

AES算法加密

String algorithm = "AES/ECB/PKCS5Padding"; // 定义加密算法
String key = "1234567890123456"; // 这是待加密的信息

String message = "Hello World."; // 这是待加密的信息

Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
String cipherText = Base64.getEncoder().encodeToString(cipher.doFinal(message.getBytes(Charset.forName("UTF-8"))));
System.out.println(cipherText);
// yjXK0enYB9AJify1rjMJMA==

AES算法解密

String algorithm = "AES/ECB/PKCS5Padding"; // 定义加密算法
String secretKey = "1234567890123456"; // 这是待加密的信息

String cipherText = "yjXK0enYB9AJify1rjMJMA==";

// 将Base64编码的密文解码
byte[] encrypted = Base64.getDecoder().decode(cipherText);

Cipher cipher = Cipher.getInstance(algorithm);
SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "AES");
cipher.init(Cipher.DECRYPT_MODE, key);

byte[] decryptBytes = cipher.doFinal(encrypted);

String message = new String(decryptBytes);
System.out.println(message);
// Hello World.

完整代码

package com.example.demo;

import org.junit.Test;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class CipherTest {
    
    
    @Test
    public void decrypt() throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException {
    
    

        String algorithm = "AES/ECB/PKCS5Padding"; // 定义加密算法
        String secretKey = "1234567890123456"; // 这是待加密的信息

        String cipherText = "yjXK0enYB9AJify1rjMJMA==";
        // 将Base64编码的密文解码
        byte[] encrypted = Base64.getDecoder().decode(cipherText);

        Cipher cipher = Cipher.getInstance(algorithm);
        SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "AES");
        cipher.init(Cipher.DECRYPT_MODE, key);

        byte[] decryptBytes = cipher.doFinal(encrypted);

        String message = new String(decryptBytes);
        System.out.println(message);
        // Hello World.
    }

    @Test
    public void encrypt() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    
    
        String algorithm = "AES/ECB/PKCS5Padding"; // 定义加密算法
        String key = "1234567890123456"; // 这是待加密的信息

        String message = "Hello World."; // 这是待加密的信息

        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
        String cipherText = Base64.getEncoder().encodeToString(cipher.doFinal(message.getBytes(Charset.forName("UTF-8"))));
        System.out.println(cipherText);
        // yjXK0enYB9AJify1rjMJMA==
    }
}

参考

猜你喜欢

转载自blog.csdn.net/mouday/article/details/132544163
今日推荐