Java中的数据加密与解密技术

Java中的数据加密与解密技术

大家好,我是微赚淘客系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!今天我们来探讨Java中的数据加密与解密技术。

一、数据加密与解密的基本概念

数据加密与解密是保护敏感信息的重要手段,通过将明文数据转换为密文,防止未经授权的访问。在Java中,我们常用的加密算法包括对称加密算法(如AES)和非对称加密算法(如RSA)。对称加密使用相同的密钥进行加密和解密,而非对称加密使用一对密钥(公钥和私钥)进行加密和解密。

二、对称加密算法:AES

AES(Advanced Encryption Standard)是一种常见的对称加密算法。以下是使用AES进行加密和解密的示例代码:

package cn.juwatech.encryption;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AesEncryption {

    private static final String ALGORITHM = "AES";

    // 生成AES密钥
    public static SecretKey generateKey() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
        keyGen.init(256); // 可以选择128, 192, 256位的密钥
        return keyGen.generateKey();
    }

    // 加密
    public static String encrypt(String data, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedData = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    // 解密
    public static String decrypt(String encryptedData, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decodedData = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedData = cipher.doFinal(decodedData);
        return new String(decryptedData);
    }

    public static void main(String[] args) {
        try {
            String data = "Hello, World!";
            SecretKey key = generateKey();
            
            String encryptedData = encrypt(data, key);
            System.out.println("Encrypted Data: " + encryptedData);
            
            String decryptedData = decrypt(encryptedData, key);
            System.out.println("Decrypted Data: " + decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

三、非对称加密算法:RSA

RSA(Rivest–Shamir–Adleman)是一种常见的非对称加密算法。以下是使用RSA进行加密和解密的示例代码:

package cn.juwatech.encryption;

import javax.crypto.Cipher;
import java.security.*;
import java.util.Base64;

public class RsaEncryption {

    private static final String ALGORITHM = "RSA";

    // 生成RSA密钥对
    public static KeyPair generateKeyPair() throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
        keyGen.initialize(2048);
        return keyGen.generateKeyPair();
    }

    // 加密
    public static String encrypt(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedData = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    // 解密
    public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decodedData = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedData = cipher.doFinal(decodedData);
        return new String(decryptedData);
    }

    public static void main(String[] args) {
        try {
            String data = "Hello, World!";
            KeyPair keyPair = generateKeyPair();
            
            String encryptedData = encrypt(data, keyPair.getPublicKey());
            System.out.println("Encrypted Data: " + encryptedData);
            
            String decryptedData = decrypt(encryptedData, keyPair.getPrivateKey());
            System.out.println("Decrypted Data: " + decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

四、结合使用AES和RSA

为了同时利用对称加密的高效性和非对称加密的安全性,通常会将两者结合使用,即使用RSA加密AES密钥,使用AES加密实际数据。以下是结合使用AES和RSA的示例代码:

package cn.juwatech.encryption;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import java.util.Base64;

public class HybridEncryption {

    private static final String AES_ALGORITHM = "AES";
    private static final String RSA_ALGORITHM = "RSA";

    // 生成AES密钥
    public static SecretKey generateAesKey() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance(AES_ALGORITHM);
        keyGen.init(256);
        return keyGen.generateKey();
    }

    // 生成RSA密钥对
    public static KeyPair generateRsaKeyPair() throws Exception {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(RSA_ALGORITHM);
        keyGen.initialize(2048);
        return keyGen.generateKeyPair();
    }

    // 使用AES加密数据
    public static String encryptData(String data, SecretKey aesKey) throws Exception {
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);
        byte[] encryptedData = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    // 使用RSA加密AES密钥
    public static String encryptAesKey(SecretKey aesKey, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedKey = cipher.doFinal(aesKey.getEncoded());
        return Base64.getEncoder().encodeToString(encryptedKey);
    }

    // 使用RSA解密AES密钥
    public static SecretKey decryptAesKey(String encryptedAesKey, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decodedKey = Base64.getDecoder().decode(encryptedAesKey);
        byte[] decryptedKey = cipher.doFinal(decodedKey);
        return new SecretKeySpec(decryptedKey, AES_ALGORITHM);
    }

    // 使用AES解密数据
    public static String decryptData(String encryptedData, SecretKey aesKey) throws Exception {
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, aesKey);
        byte[] decodedData = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedData = cipher.doFinal(decodedData);
        return new String(decryptedData);
    }

    public static void main(String[] args) {
        try {
            String data = "Hello, World!";
            KeyPair rsaKeyPair = generateRsaKeyPair();
            SecretKey aesKey = generateAesKey();

            String encryptedData = encryptData(data, aesKey);
            String encryptedAesKey = encryptAesKey(aesKey, rsaKeyPair.getPublicKey());

            System.out.println("Encrypted Data: " + encryptedData);
            System.out.println("Encrypted AES Key: " + encryptedAesKey);

            SecretKey decryptedAesKey = decryptAesKey(encryptedAesKey, rsaKeyPair.getPrivateKey());
            String decryptedData = decryptData(encryptedData, decryptedAesKey);

            System.out.println("Decrypted Data: " + decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

五、总结

本文详细介绍了在Java中实现数据加密与解密的几种常见技术,包括对称加密算法(AES)、非对称加密算法(RSA)以及结合使用AES和RSA的混合加密技术。这些技术可以有效地保护数据的安全性,防止未经授权的访问。

本文著作权归聚娃科技微赚淘客系统开发者团队,转载请注明出处!

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值