成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

強大加密!SpringBoot 實現(xiàn) RSA+AES 自動解密,保障接口安全

開發(fā) 前端
本文展示了如何在 Spring Boot 應用中實現(xiàn) RSA + AES 混合加密方案,從而保障接口數(shù)據(jù)傳輸?shù)陌踩浴Mㄟ^結(jié)合這兩種加密算法,能夠在確保安全的同時,不影響系統(tǒng)性能。

在當今的應用開發(fā)中,保障接口的安全性變得尤為關鍵。尤其是當敏感數(shù)據(jù)通過網(wǎng)絡傳輸時,如何避免數(shù)據(jù)泄露或篡改,成為了開發(fā)者必須考慮的問題。本篇文章將詳細探討如何在 SpringBoot 3.4 框架下,利用 RSA 和 AES 混合加密方案,確保接口通信的安全性。

為什么需要接口加密?

在沒有加密的情況下,通過網(wǎng)絡傳輸?shù)臄?shù)據(jù)很容易被中間人或抓包工具截獲。尤其是當數(shù)據(jù)中包含用戶隱私信息或支付數(shù)據(jù)時,缺乏加密的傳輸方式會帶來巨大的安全隱患。通過對接口數(shù)據(jù)進行加密,即使數(shù)據(jù)在傳輸過程中被截獲,黑客也無法解密和理解數(shù)據(jù)內(nèi)容,從而有效避免了數(shù)據(jù)泄漏的風險。

RSA+AES 混合加密方案的優(yōu)勢

選擇 RSA 和 AES 混合加密方案,主要是因為這兩種加密算法的結(jié)合,能夠平衡加密的安全性和性能:

  • RSA 是一種非對稱加密算法,雖然加密安全性高,但加密速度較慢,適合用來加密較小的數(shù)據(jù),比如加密 AES 密鑰。
  • AES 是一種對稱加密算法,速度較快,適用于大量數(shù)據(jù)的加密,但密鑰的分發(fā)和管理是一個挑戰(zhàn)。

通過結(jié)合這兩種算法,我們利用 RSA 加密 AES 的密鑰,再使用 AES 加密實際的數(shù)據(jù),從而實現(xiàn)了高安全性和高性能的平衡。

實現(xiàn)原理

  1. 客戶端和服務端預先約定好 RSA 公鑰和私鑰。
  2. 客戶端生成一個隨機的 AES 密鑰,并使用 RSA 公鑰加密這個 AES 密鑰。
  3. 客戶端使用 AES 密鑰加密實際的數(shù)據(jù)。
  4. 客戶端將加密后的 AES 密鑰和加密的數(shù)據(jù)一并發(fā)送給服務端。
  5. 服務端使用 RSA 私鑰解密得到 AES 密鑰,然后用 AES 密鑰解密數(shù)據(jù)。

項目依賴

在 pom.xml 中,我們需要添加以下依賴,來支持加密解密功能:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.78</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.68</version>
    </dependency>
</dependencies>

加密工具類

接下來,我們將實現(xiàn)一個加密工具類,用于處理 RSA 和 AES 加密解密邏輯。以下是該類的實現(xiàn)代碼:

package com.icoderoad.secureapi.utils;


import org.bouncycastle.jce.provider.BouncyCastleProvider;


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;


public class EncryptionUtils {


    static {
        Security.addProvider(new BouncyCastleProvider());
    }


    private static final String AES_ALGORITHM = "AES/CBC/PKCS7Padding";
    private static final int AES_KEY_SIZE = 256;
    private static final String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";
    private static final int RSA_KEY_SIZE = 2048;


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


    public static String keyToString(Key key) {
        return Base64.getEncoder().encodeToString(key.getEncoded());
    }


    public static PublicKey stringToRSAPublicKey(String keyStr) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(keyStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    }


    public static PrivateKey stringToRSAPrivateKey(String keyStr) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(keyStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(keySpec);
    }


    public static SecretKey generateAESKey() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(AES_KEY_SIZE);
        return keyGen.generateKey();
    }


    public static SecretKey stringToAESKey(String keyStr) {
        byte[] keyBytes = Base64.getDecoder().decode(keyStr);
        return new SecretKeySpec(keyBytes, "AES");
    }


    public static String encryptWithRSA(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }


    public static String decryptWithRSA(String encryptedData, PrivateKey privateKey) throws Exception {
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }


    public static String encryptWithAES(String data, SecretKey secretKey, byte[] iv) throws Exception {
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM, "BC");
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }


    public static String decryptWithAES(String encryptedData, SecretKey secretKey, byte[] iv) throws Exception {
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM, "BC");
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }


    public static byte[] generateIV() {
        SecureRandom random = new SecureRandom();
        byte[] iv = new byte[16];
        random.nextBytes(iv);
        return iv;
    }
}

請求包裝類與解密攔截器

為了便于加密請求的自動解密,我們將創(chuàng)建一個 EncryptedRequest 請求包裝類:

package com.icoderoad.secureapi.model;


import lombok.Data;


@Data
public class EncryptedRequest {
    private String encryptedKey;
    private String iv;
    private String encryptedData;
    private Long timestamp;
    private String signature;
}

接著,創(chuàng)建一個解密攔截器,自動在控制器處理請求前進行解密:

package com.icoderoad.secureapi.utils;


import org.bouncycastle.jce.provider.BouncyCastleProvider;


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;


public class EncryptionUtils {


    static {
        Security.addProvider(new BouncyCastleProvider());
    }


    private static final String AES_ALGORITHM = "AES/CBC/PKCS7Padding";
    private static final int AES_KEY_SIZE = 256;
    private static final String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";
    private static final int RSA_KEY_SIZE = 2048;


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


    public static String keyToString(Key key) {
        return Base64.getEncoder().encodeToString(key.getEncoded());
    }


    public static PublicKey stringToRSAPublicKey(String keyStr) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(keyStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    }


    public static PrivateKey stringToRSAPrivateKey(String keyStr) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(keyStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(keySpec);
    }


    public static SecretKey generateAESKey() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(AES_KEY_SIZE);
        return keyGen.generateKey();
    }


    public static SecretKey stringToAESKey(String keyStr) {
        byte[] keyBytes = Base64.getDecoder().decode(keyStr);
        return new SecretKeySpec(keyBytes, "AES");
    }


    public static String encryptWithRSA(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }


    public static String decryptWithRSA(String encryptedData, PrivateKey privateKey) throws Exception {
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }


    public static String encryptWithAES(String data, SecretKey secretKey, byte[] iv) throws Exception {
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM, "BC");
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }


    public static String decryptWithAES(String encryptedData, SecretKey secretKey, byte[] iv) throws Exception {
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM, "BC");
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }


    public static byte[] generateIV() {
        SecureRandom random = new SecureRandom();
        byte[] iv = new byte[16];
        random.nextBytes(iv);
        return iv;
    }
}

完整的實現(xiàn)

  • RSA 密鑰生成在應用啟動時,生成并存儲公鑰和私鑰。
  • AES 密鑰生成與加密客戶端生成一個隨機 AES 密鑰,并通過 RSA 公鑰加密。
  • 數(shù)據(jù)加密與解密使用 AES 加密數(shù)據(jù),服務端使用 RSA 解密 AES 密鑰后,再用 AES 解密數(shù)據(jù)。

總結(jié)

本文展示了如何在 Spring Boot 應用中實現(xiàn) RSA + AES 混合加密方案,從而保障接口數(shù)據(jù)傳輸?shù)陌踩浴Mㄟ^結(jié)合這兩種加密算法,能夠在確保安全的同時,不影響系統(tǒng)性能。

責任編輯:武曉燕 來源: 路條編程
相關推薦

2023-03-06 08:49:02

加密和解密SpringBoot

2023-09-26 08:25:37

CobaltStri模式Agent

2021-01-07 14:17:31

Springboot數(shù)據(jù)安全加密

2024-07-09 10:13:15

2025-03-26 08:43:17

2024-10-15 10:38:32

2020-12-13 09:40:11

物聯(lián)網(wǎng)物聯(lián)網(wǎng)安全加密方法

2015-03-26 11:25:10

對稱加密加密壓縮加密解密解壓

2019-03-19 15:25:47

toplip加密工具開源

2013-11-15 13:06:52

透明加解密hook技術數(shù)據(jù)安全

2022-06-04 12:25:10

解密加密過濾器

2021-03-09 13:18:53

加密解密參數(shù)

2009-09-09 18:50:23

C# 加密RSA

2022-01-26 07:25:09

PythonRSA加解密

2024-04-29 07:50:52

C#AES加密

2023-12-13 12:27:46

2024-04-15 10:32:14

2010-04-13 15:13:04

2024-01-02 10:46:14

2024-04-22 09:02:06

LicenseC#軟件開發(fā)RSA加密
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧洲高清转码区一二区 | 久久久av| 夜夜久久 | 国产精品一区二区欧美黑人喷潮水 | 97色在线观看免费视频 | 精品日韩 | 欧美激情a∨在线视频播放 成人免费共享视频 | 欧美在线a | 国产精品国产三级国产aⅴ入口 | 亚洲国产一区在线 | 国产高清视频在线观看 | 国产精品区二区三区日本 | 久久久久国产 | 国产专区在线 | 国产成人一区二区三区电影 | 91亚洲视频在线 | 亚洲精品视频在线播放 | 日韩一级一区 | 成人国产在线观看 | 在线播放一区二区三区 | 亚洲一区二区在线 | 性欧美精品一区二区三区在线播放 | 日韩一区二区在线观看 | 久久精品久久久久久 | 二区中文 | 日韩精品一区二区三区中文在线 | 亚洲国产精品日韩av不卡在线 | 日韩中文字幕一区二区 | 亚洲成人精品 | 日韩电影免费在线观看中文字幕 | 羞羞的视频免费观看 | 2021天天躁夜夜看 | 亚洲精品一区国语对白 | 一区二区三区四区不卡 | 成人欧美一区二区三区白人 | 亚洲一区影院 | 超碰精品在线观看 | 亚洲国产成人精品女人 | 亚洲一区二区久久 | 午夜成人在线视频 | 欧美成人h版在线观看 |