[英]Java RSA Input encrypted Text and decrypt it.(BigINteger)
我花了一天的時間檢查代碼最后幾行的很多組合,但我絕對不會理解如何獲取加密輸入(如BigInteger並將其解密為String)……這是最后一塊的代碼:
package rsa;
import java.io.IOException;
import static java.lang.System.exit;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class RSA {
private BigInteger n, d, e;
private int bitlen = 1024;
/** Create an instance that can encrypt using someone elses public key. */
public RSA(BigInteger newn, BigInteger newe) {
n = newn;
e = newe;
}
/** Create an instance that can both encrypt and decrypt. */
public RSA(int bits) {
bitlen = bits;
SecureRandom r = new SecureRandom();
BigInteger p = new BigInteger(bitlen / 2, 100, r);
BigInteger q = new BigInteger(bitlen / 2, 100, r);
n = p.multiply(q);
BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q
.subtract(BigInteger.ONE));
e = new BigInteger("3");
while (m.gcd(e).intValue() > 1) {
e = e.add(new BigInteger("2"));
}
d = e.modInverse(m);
}
/** Encrypt the given plaintext message. */
public synchronized String encrypt(String message) {
return (new BigInteger(message.getBytes())).modPow(e, n).toString();
}
/** Encrypt the given plaintext message. */
public synchronized BigInteger encrypt(BigInteger message) {
return message.modPow(e, n);
}
/** Decrypt the given ciphertext message. */
public synchronized String decrypt(String message) {
return new String((new BigInteger(message)).modPow(d, n).toByteArray());
}
/** Decrypt the given ciphertext message. */
public synchronized BigInteger decrypt(BigInteger message) {
return message.modPow(d, n);
}
/** Generate a new public and private key set. */
public synchronized void generateKeys() {
SecureRandom r = new SecureRandom();
BigInteger p = new BigInteger(bitlen / 2, 100, r);
BigInteger q = new BigInteger(bitlen / 2, 100, r);
n = p.multiply(q);
BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q
.subtract(BigInteger.ONE));
e = new BigInteger("3");
while (m.gcd(e).intValue() > 1) {
e = e.add(new BigInteger("2"));
}
d = e.modInverse(m);
}
/** Return the modulus. */
public synchronized BigInteger getN() {
return n;
}
/** Return the public key. */
public synchronized BigInteger getE() {
return e;
}
/** Trivial test program. */
public static void main(String[] args) throws InterruptedException, IOException {
RSA rsa = new RSA(1024);
loop:
/** Input Scanner switch*/
Scanner first = new Scanner(System.in);
int a = first.nextInt();
switch (a) {
case 1:
/** Output Text zum verschlüsseln */
System.out.println();
System.out.println();
System.out.println();
System.out.println("### Text zum verschlüsseln eingeben: ###");
/** Input Text zum verschlüsseln*/
Scanner scanner = new Scanner(System.in);
String plaintext = scanner.nextLine();
/** Output eingegebenen Text*/
System.out.println("Eingegebener Text: ");
System.out.println("------------------------");
System.out.println(plaintext);
BigInteger bigplaintext = new BigInteger(plaintext.getBytes());
/** Output */
System.out.println();
System.out.println();
System.out.println();
/** Output verschlüsselten Text*/
BigInteger vbigplaintext = rsa.encrypt(bigplaintext);
System.out.println("Veschlüsselter Text: ");
System.out.println("------------------------");
System.out.println(vbigplaintext);
/** ---------------------------------------------------- */
break;
case 2:
/** Output Text zum Entschlüsseln */
System.out.println();
System.out.println();
System.out.println();
System.out.println("### Verschlüsselten Text eingeben: ###");
/** Input verschlüsselter Text*/
Scanner scanner3 = new Scanner(System.in);
String vertext = scanner3.nextLine();
System.out.println(vertext);
/**----Here is the Problem------*/
BigInteger decbigtext = rsa.decrypt(vertext);
BigInteger entbigtext = new BigInteger(decbigtext.toByteArray());
System.out.println("Entschlüsselter Text:");
System.out.println("------------------------");
System.out.println("Plaintext: " + entbigtext);
break;
default:
System.exit(0);
}
}
}
如果有人能給我解決該問題的方法,那就太好了。 如果該帖子已經存在,請移動或刪除,對不起我的英語不好。
每次運行應用程序時,都會生成一個新的密鑰對。 顯然,您無法使用全新的私鑰解密。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.