java rsa 私钥加密,Java-公私钥加密-如何在RSA中计算私钥

I worked on a code for an RSA algorithm and it returns the incorrect number, which happens to be huge. I am sure I coded everything right except for one line I was not sure about. I did not know how to solve for the private key in the RSA, and just winged it (I saw someone code

d = e.modInverse(m);

where d is the private key, e is the public key, and m is (p-1)*(q-1). I dont understand how the modInverse method works though. long story short, how do you actually solve for the 'd' without having 2 unknowns in the same equation (I saw some equations given that said:

d = 1/(e % m);

I refrained from posting results just because the number returned is about as big as the encrypted message.

package encryptionalgorithms;

import java.math.BigInteger;

import java.util.*;

/**

*

* @author YAZAN Sources:

* http://introcs.cs.princeton.edu/java/78crypto/RSA.java.html

* http://www.math.rutgers.edu/~greenfie/gs2004/euclid.html

* http://www.youtube.com/watch?v=ejppVhOSUmA

*/

public class EncryptionAlgorithms {

private static BigInteger p, q, n, m, e, r, a, b, d, encrypt, decrypt, message, userN, userE, userD;

private static BigInteger one = new BigInteger("1");

private static BigInteger badData = new BigInteger("-1");

private static BigInteger zero = new BigInteger("0");

public static void main(String[] args) {

PKE();

}

public static void PKE() { //Private Key Encryption

Scanner input = new Scanner(System.in);

Random rand1 = new Random(System.nanoTime());

Random rand2 = new Random(System.nanoTime() * 16); //to create a second obscure random number

p = BigInteger.probablePrime(1024, rand1);

q = BigInteger.probablePrime(1024, rand2);

n = p.multiply(q); // n = p * q

m = (p.subtract(one)).multiply(q.subtract(one)); // m = (p-1) * (q-1)

e = new BigInteger("65537"); //must be a prime. GCD(e,m)=1 //65537 = 2^16 + 1 // will have to make an algorith for this later

d = e.modInverse(m); //weakest link <============

// System.out.println("Public Keys:");

// System.out.println("e = " + e + " and n = " + n);

// System.out.println("Private Keys:");

// System.out.println("d = " + d + " and n = " + n);

System.out.println("please enther the message to be encrypted");

BigInteger mes = new BigInteger(input.next());

BigInteger ans = encrypt(mes, n, e);

decrypt(ans, n, d);

}

public static BigInteger encrypt(BigInteger num, BigInteger n, BigInteger e) {

encrypt = num.modPow(e, n);

System.out.println("encrypted: " + encrypt);

return encrypt;

}

public static BigInteger decrypt(BigInteger enc, BigInteger n, BigInteger d) {

decrypt = enc.modPow(d, n);

System.out.println("decrypted: " + decrypt);

return decrypt;

}

}

and as a variant to the line in question, I tried:

d = one.divide(e.mod(m));

and I still got incorrect results.

解决方案

haha, you are going to kick yourself. You did everything correct, except for this teeny-weeny bug:

decrypt(ans, n, e);

should be

decrypt(ans, n, d);

In general, you could probably do a better job with variable names and class concepts such as instance variables. Kudos to you for posting a complete working example.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值