Java实现AES和RSA加密算法:代码示例与分析

本文将通过Java代码示例,展示如何实现两种常用的加密算法:AES和RSA。这些示例代码旨在帮助初学者理解加密算法的基本原理和实现细节,并提供一些安全方面的思考。

注意: 以下代码示例仅供学习参考,实际使用时需遵循安全最佳实践,例如使用更强的密钥长度、安全的随机数生成器,以及避免使用默认的加密模式和填充方式等。

1. AES加密算法实现

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class AESExample {

    public static void main(String[] args) throws Exception {
        String plainText = 'Hello, World!';
        String key = '0123456789abcdef';
        
        // 加密
        String encryptedText = encryptAES(plainText, key);
        System.out.println('Encrypted Text: ' + encryptedText);
        
        // 解密
        String decryptedText = decryptAES(encryptedText, key);
        System.out.println('Decrypted Text: ' + decryptedText);
    }

    public static String encryptAES(String plainText, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), 'AES');
        Cipher cipher = Cipher.getInstance('AES/ECB/PKCS5Padding');
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decryptAES(String encryptedText, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), 'AES');
        Cipher cipher = Cipher.getInstance('AES/ECB/PKCS5Padding');
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }
}

代码分析

  • 密钥: AES算法使用对称密钥,即加密和解密使用相同的密钥。
  • 加密模式: 代码中使用的是ECB模式,它是一种简单但不太安全的模式。实际应用中应该使用更安全的模式,例如CBC、CTR或GCM。
  • 填充: 代码中使用的是PKCS5Padding,它是一种常见的填充方式,用于确保明文长度满足加密算法的要求。

2. RSA加密算法实现

import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;

public class RSAExample {

    public static void main(String[] args) throws Exception {
        String plainText = 'Hello, World!';
        
        // 生成RSA密钥对
        KeyPair keyPair = generateRSAKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        
        // 加密
        byte[] encryptedBytes = encryptRSA(plainText.getBytes(), publicKey);
        System.out.println('Encrypted Text: ' + new String(encryptedBytes));
        
        // 解密
        byte[] decryptedBytes = decryptRSA(encryptedBytes, privateKey);
        System.out.println('Decrypted Text: ' + new String(decryptedBytes));
    }

    public static KeyPair generateRSAKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance('RSA');
        keyPairGenerator.initialize(2048);
        return keyPairGenerator.generateKeyPair();
    }

    public static byte[] encryptRSA(byte[] input, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance('RSA');
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(input);
    }

    public static byte[] decryptRSA(byte[] input, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance('RSA');
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(input);
    }
}

代码分析

  • 密钥: RSA算法使用非对称密钥,即加密和解密使用不同的密钥。公钥用于加密,私钥用于解密。
  • 密钥生成: 代码中使用KeyPairGenerator类生成RSA密钥对,并使用KeyFactory类获取公钥和私钥的具体信息。

总结:

本文提供了Java代码示例,展示了如何实现AES和RSA两种常见的加密算法。这些示例代码旨在帮助初学者理解加密算法的基本原理和实现细节,并提供一些安全方面的思考。在实际应用中,需要注意选择合适的加密模式、填充方式,以及使用更强的密钥长度等安全措施,以确保数据安全。

Java实现AES和RSA加密算法:代码示例与分析

原文地址: http://www.cveoy.top/t/topic/CEk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录