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

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

開發 前端
本文展示了如何在 Spring Boot 應用中實現 RSA + AES 混合加密方案,從而保障接口數據傳輸的安全性。通過結合這兩種加密算法,能夠在確保安全的同時,不影響系統性能。

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

為什么需要接口加密?

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

RSA+AES 混合加密方案的優勢

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

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

通過結合這兩種算法,我們利用 RSA 加密 AES 的密鑰,再使用 AES 加密實際的數據,從而實現了高安全性和高性能的平衡。

實現原理

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

項目依賴

在 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>

加密工具類

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

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;
    }
}

請求包裝類與解密攔截器

為了便于加密請求的自動解密,我們將創建一個 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;
}

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

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;
    }
}

完整的實現

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

總結

本文展示了如何在 Spring Boot 應用中實現 RSA + AES 混合加密方案,從而保障接口數據傳輸的安全性。通過結合這兩種加密算法,能夠在確保安全的同時,不影響系統性能。

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

2023-03-06 08:49:02

加密和解密SpringBoot

2021-01-07 14:17:31

Springboot數據安全加密

2023-09-26 08:25:37

CobaltStri模式Agent

2024-07-09 10:13:15

2025-03-26 08:43:17

2020-12-13 09:40:11

物聯網物聯網安全加密方法

2024-10-15 10:38:32

2015-03-26 11:25:10

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

2019-03-19 15:25:47

toplip加密工具開源

2013-11-15 13:06:52

透明加解密hook技術數據安全

2022-06-04 12:25:10

解密加密過濾器

2021-03-09 13:18:53

加密解密參數

2009-09-09 18:50:23

C# 加密RSA

2022-01-26 07:25:09

PythonRSA加解密

2024-04-29 07:50:52

C#AES加密

2010-04-13 15:13:04

2024-04-15 10:32:14

2023-12-13 12:27:46

2024-01-02 10:46:14

2024-08-26 08:34:47

AES加密算法
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 五月婷婷婷 | 欧美精品在欧美一区二区 | 91视频精选 | 欧美日韩专区 | 亚洲国产精品一区二区第一页 | 亚洲一二三区在线观看 | 久久噜噜噜精品国产亚洲综合 | 久久99精品久久久久久 | 久久久www成人免费无遮挡大片 | 国产精品久久久久一区二区三区 | 91资源在线观看 | 欧美一级特黄aaa大片在线观看 | 最新中文字幕在线 | 久久精品视频在线免费观看 | 国产网站在线播放 | 欧美一级免费观看 | 亚洲一区二区在线视频 | 波多野结衣电影一区 | 久久大| 一区二区三区高清 | 国产精品96久久久久久 | 欧美一区二区三区 | 日韩欧美亚洲一区 | 国产激情视频在线观看 | 欧美久久一级 | 国产精品视频免费 | 在线免费亚洲视频 | 鸳鸯谱在线观看高清 | 国产毛片av | 国产福利91精品 | 国产精品国产三级国产播12软件 | 国产综合久久久久久鬼色 | 欧美一级三级 | 欧美 视频 | 逼逼视频 | 91久久精品国产 | 国产精品美女久久久久aⅴ国产馆 | 日本亚洲欧美 | 欧美精品综合 | 久久国产精品久久国产精品 | 国产精品成人一区二区三区 |