首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 實作 RSA 加密演算法完整教學

     編輯:Java 相關知識

RSA 演算法原理與 Java 實作

RSA 是非對稱加密演算法的經典實現,廣泛應用於資料加密、數位簽章與金鑰交換。本文將詳細解析 RSA 核心數學原理,並提供完整的 Java 程式碼實作,幫助開發者深入理解公開金鑰加密體系的運作機制。

核心數學基礎

RSA 安全性建立在大整數分解困難性之上。演算法流程包含三個關鍵步驟:

  1. 金鑰生成:選擇兩個大質數 p、q,計算 n = p×q 與 φ(n) = (p-1)(q-1),選取與 φ(n) 互質的 e 作為公開指數,再透過擴展歐幾里得演算法求得私鑰指數 d,使 ed ≡ 1 (mod φ(n))
  2. 加密運算:密文 C = M^e mod n,其中 M 為明文資料
  3. 解密運算:明文 M = C^d mod n

Java 完整實作程式碼

package rsa;
import java.math.BigInteger;

public class RSA {
    private long p, q, e, d, n;

    public RSA() {
        // 為了示範方便,這裡使用固定質數
        // 實際應用應使用 SecureRandom 生成大質數
        p = 1033;
        q = 2017;
        e = 29437;
        n = p * q;
        d = calculateD();
    }

    private long calculateD() {
        long t0 = 0, t1 = 1, t2 = -1;
        long r0 = (p - 1) * (q - 1), m = r0, r1 = e, r2 = -1;
        
        do {
            long q = r0 / r1;
            r2 = r0 - r1 * q;
            if (r2 == 0) break;
            t2 = t0 - t1 * q;
            while (t2 < 0) t2 += m;
            if (t2 >= m) t2 %= m;
            r0 = r1; r1 = r2;
            t0 = t1; t1 = t2;
        } while (r2 != 0);
        
        if (r1 != 1) return 0;
        else return t2;
    }

    public BigInteger encode(BigInteger data) {
        return pow(data, d).mod(BigInteger.valueOf(n));
    }

    public BigInteger decode(BigInteger code) {
        return pow(code, e).mod(BigInteger.valueOf(n));
    }

    public BigInteger pow(BigInteger data, long p) {
        return data.pow((int) p);
    }

    // Getter 方法省略...
    
    public static void main(String args[]) {
        RSA rsa = new RSA();
        BigInteger data = new BigInteger("222222");
        
        long oldtime = System.currentTimeMillis();
        BigInteger code = rsa.encode(data);
        long newtime = System.currentTimeMillis();
        double codetime = ((double)(newtime - oldtime)) / 1000;
        
        oldtime = System.currentTimeMillis();
        BigInteger decode = rsa.decode(code);
        newtime = System.currentTimeMillis();
        double decodetime = ((double)(newtime - oldtime)) / 1000;
        
        System.out.println("私鑰 d: " + rsa.d);
        System.out.println("公開指數 e: " + rsa.e);
        System.out.println("模數 n: " + rsa.n);
        System.out.println("原文: " + data);
        System.out.println("密文: " + code + " 耗時: " + codetime + "秒");
        System.out.println("解密: " + decode + " 耗時: " + decodetime + "秒");
    }
}

關鍵技術細節解析

  • BigInteger 運算:Java 原生 long 型別無法支撐實際 RSA 所需的大整數運算,必須使用 java.math.BigInteger 進行模冪運算
  • 模逆運算:calculateD() 實作擴展歐幾里得演算法求模逆,這是 RSA 金鑰生成的核心步驟
  • 效能考量:範例使用簡單的 pow() 方法,生產環境應採用平方乘演算法或 BigInteger.modPow() 優化大指數運算

安全性與最佳實踐建議

  1. 質數生成:實際部署必須使用密碼學安全的隨機數生成器(如 SecureRandom)產生 2048 位元以上的大質數
  2. 填充機制:教科書式 RSA 易受選定明文攻擊,必須採用 OAEP 或 PKCS#1 v1.5 填充標準
  3. 金鑰管理:私鑰應儲存於硬體安全模組(HSM)或加密金鑰庫中,嚴禁硬編碼於原始碼
  4. 效能優化:利用中國剩餘定理(CRT)可將解密速度提升 4 倍

常見應用場景

  • HTTPS/TLS 協定中的金鑰交換與伺服器身分驗證
  • 電子郵件加密(PGP/GPG)與數位簽章
  • SSH 遠端登入認證
  • 區塊鏈交易簽名驗證

透過本文的完整實作與原理解析,開發者可建立扎實的 RSA 演算法認知基礎。實際專案開發中,強烈建議直接使用成熟的密碼學函式庫(如 Bouncy Castle 或 Java 內建 JCA),避免自行實作帶來的安全風險。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved