如何在 Java 中为 RSA 生成密钥



我得到了这个代码来加密/解密一些字符串通过RSA。此项目用于了解加密,并且仅用于学习目的。到目前为止,我总是使用固定密钥来使用我的应用程序,但现在我想构建一个基于输入的 bitsize 生成有效密钥的方法。

这是我目前的课程:

import java.math.BigInteger;
import java.util.Random;
public class RSAWorker {
    public static BigInteger gcdExtended(BigInteger a, BigInteger b) {
        BigInteger a_cur = BigInteger.ONE;
        BigInteger b_cur = BigInteger.ZERO;
        BigInteger a_next = BigInteger.ZERO;
        BigInteger b_next = BigInteger.ONE;
        BigInteger q;
        BigInteger r;
        do {
            q = a.divide(b);
            r = a.subtract(q.multiply(b));
            BigInteger tmpA = a_next;
            BigInteger tmpB = b_next;
            a_next = a_cur.subtract(q.multiply(a_next));
            b_next = b_cur.subtract(q.multiply(b_next));
            a_cur = tmpA;
            b_cur = tmpB;
            a = b;
            b = r;
        } while (r.intValue() > 0);
        return a_cur;
    }
    public static char separator = '_';
    private BigInteger e = BigInteger.ZERO;
    private BigInteger d = BigInteger.ZERO;
    private BigInteger n = BigInteger.ZERO;
    private ProgressInformant progressSupervisor;
    private void inform(long index, long max) {
        if (progressSupervisor != null) {
            progressSupervisor.prograssAt(Math.round((float) index / max * 100));
        }
    }
    public void generateKeys(int bitSize) {
       e = 65537;
       d = 1873890542004290369;
       n = 11495756297295397069;
    }
    public BigInteger encode(BigInteger c) {
        return c.modPow(e, n);
    }
    public BigInteger decode(BigInteger c) {
        return c.modPow(d, n);
    }
    public String encode(String str) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            result.append(encode(BigInteger.valueOf(c)));
            result.append(separator);
            inform(i + 1, str.length());
            if (Thread.interrupted()) {
                return null;
            }
        }
        return result.toString();
    }
    public String decode(String str) {
        StringBuilder result = new StringBuilder();
        String[] tokens = str.split(separator+"+");
        for (int i = 0; i < tokens.length; i++) {
            String s = tokens[i];
            result.append((char) decode(new BigInteger(s)).intValue());
            inform(i + 1, tokens.length);
            if (Thread.interrupted()) {
                return null;
            }
        }
        return result.toString();
    }
    public ProgressInformant getProgressSupervisor() {
        return progressSupervisor;
    }
    public void setProgressSupervisor(ProgressInformant progressSupervisor) {
        this.progressSupervisor = progressSupervisor;
    }
    public BigInteger getE() {
        return e;
    }
    public void setE(BigInteger e) {
        this.e = e;
    }
    public BigInteger getD() {
        return d;
    }
    public void setD(BigInteger d) {
        this.d = d;
    }
    public BigInteger getN() {
        return n;
    }
    public void setN(BigInteger n) {
        this.n = n;
    }
}

看看这个维基百科文章: RSA 密钥生成

它逐步向您展示如何生成有效密钥。

public void generateKeys(int bitSize) {
    Random rnd = new Random();
    //1
    BigInteger p = BigInteger.probablePrime(bitSize, rnd);
    BigInteger q = BigInteger.probablePrime(bitSize, rnd);
    //2
    n = p.multiply(q);
    //3
    BigInteger euler = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
    //4
    BigInteger x = BigInteger.valueOf(65537);
    do {
        if (euler.gcd(x).equals(BigInteger.ONE)) {
            e = x;
            //5
            d = gcdExtended(e, euler);
            if (d.signum() == 1) {
                break;
            }
        }
        x = x.add(BigInteger.ONE);
    } while (true);
}
  1. 选择两个不同的素数p和q(通过可能的素数)
  2. 计算 n = pq
  3. 欧拉函数(http://en.wikipedia.org/wiki/Euler%27s_totient_function)
  4. 选择 e(作为公钥指数)
  5. 通过您现有的 gcdExtended 方法(扩展欧几里得算法)确定 d。

相关内容

  • 没有找到相关文章

最新更新