Java实现AES和RSA加解密时,Mode动态支持ECB,CTR,GCM

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class AESAndRSAUtil {

    // 动态选择AES的加密模式
    public static String aesEncrypt(String plaintext, byte[] key, String mode) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/" + mode + "/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encrypted = cipher.doFinal(plaintext.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }

    // RSA加密
    public static String rsaEncrypt(String plaintext, Key rsaPublicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);
        byte[] encrypted = cipher.doFinal(plaintext.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }

    // RSA解密
    public static String rsaDecrypt(String encryptedText, Key rsaPrivateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey);
        byte[] original = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
        return new String(original);
    }

    // 动态选择AES的解密模式
    public static String aesDecrypt(String encryptedText, byte[] key, String mode) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/" + mode + "/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] original = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
        return new String(original);
    }

    public static void main(String[] args) throws Exception {
        // AES密钥生成
        KeyGenerator aesKeyGenerator = KeyGenerator.getInstance("AES");
        aesKeyGenerator.init(256);
        SecretKey aesSecretKey = aesKeyGenerator.generateKey();
        byte[] aesKey = aesSecretKey.getEncoded();

        // RSA密钥对生成
        KeyPairGenerator rsaKeyPairGenerator = KeyPairGenerator.getInstance("RSA");
        rsaKeyPairGenerator.initialize(2048);
        KeyPair rsaKeyPair = rsaKeyPairGenerator.generateKeyPair();
        PublicKey rsaPublicKey = rsaKeyPair.getPublic();
        PrivateKey rsaPrivateKey = rsaKeyPair.getPrivate();

        // 测试数据
        String data = "Hello World";

        // AES加密
        String aesEncryptedECB = aesEncrypt(data, aesKey, "ECB");
        System.out.println("AES Encrypted (ECB): " + aesEncryptedECB);

        String aesEncryptedCTR = aesEncrypt(data, aesKey, "CTR");
        System.out.println("AES Encrypted (CTR): " + aesEncryptedCTR);

        // AES解密
        String aesDecryptedECB = aesDecrypt(aesEncryptedECB, aesKey, "ECB");
        System.out.println("AES Decrypted (ECB): " + aesDecryptedECB);

        // RSA加密
        String rsaEncrypted = rsaEncrypt(data, rsaPublicKey);
        System.out.println("RSA Encrypted: " + rsaEncrypted);

        // RSA解密
        String rsaDecrypted = rsaDecrypt(rsaEncrypted, rsaPrivateKey);
        System.out.println("RSA Decrypted: " + rsaDecrypted);
    }
}

- `aesEncrypt`和`aesDecrypt`方法接受一个模式参数,允许动态选择AES的加密模式。
- RSA的加解密使用标准的`RSA`算法名称,不涉及模式选择。
- AESECBCTR模式使用`PKCS5Padding`填充方式,而GCM模式通常使用`NoPadding`。
- RSA的公钥和私钥是通过`KeyPairGenerator`生成的,可以用于加密和解密。
- 使用了`Base64`编码来处理加密后的字节数据,以便打印或存储。
  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值