0
我試圖用RSA加密來加密用戶輸入。在加密過程中返回NaN的Java RSA算法
在Eclipse中使用我的調試監視器我可以看到,當代碼用'e'爲模擬'n'給asciiLetter供電時,'e'和'n'數字太大,並且正在返回無窮大或NaN。有關如何改變這個問題的任何指導?
我改變了位長度下降從1024到128,但是這並沒有影響。
後來我需要解密,但我試圖讓這部分工作提前。
public class RSA {
static int bitlength = 128;
private static Random r = new Random();
private static BigInteger p = BigInteger.probablePrime(bitlength, r);
private static BigInteger q = BigInteger.probablePrime(bitlength, r);
private static BigInteger n = p.multiply(q);
private BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
private static BigInteger e = BigInteger.probablePrime(bitlength/2, r);
private BigInteger d;
private static BigInteger encryptedMessage; {
while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0) {
e.add(BigInteger.ONE);
}
BigInteger d = (e.modInverse(phi));
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter your message: ");
String clearMsg = input.nextLine().trim(); // String clearMsg = input.next();
System.out.println(clearMsg); // Check message trimmed
char userInputArray[] = clearMsg.toCharArray();
ArrayList<BigInteger> cipherTextArray = new ArrayList<BigInteger>();
for (int i = 0; i < userInputArray.length; i++) {
int letter = userInputArray[i];
BigInteger bigLetter = new BigInteger(String.valueOf(letter));
BigInteger asciiLetter = (BigInteger) bigLetter;
BigInteger encryptedMessage = pow(asciiLetter, e);
BigInteger encryptM = encryptedMessage;
BigInteger encryptedMessageMod = encryptM.mod(n);
cipherTextArray.add(encryptedMessageMod);
}
System.out.println(cipherTextArray);
}
private static BigInteger pow(BigInteger asciiLetter, BigInteger e) {
// TODO Auto-generated method stub
return null;
所以你想知道爲什麼'BigInteger的encryptedMessage = POW(asciiLetter,E);'不工作? ...你沒有實現pow(...) – DarkSquirrel42
這是與代碼有關的事情: private static BigInteger pow(BigInteger asciiLetter,BigInteger e){TODO自動生成的方法存根 return null; 因爲我不知道這是什麼一樣,Eclipse的想將其添加在按我說我是新來的編碼和我有點堅持:( – user7888294
使用'BigInteger的encryptedMessage = asciiLetter.pow(E);?',而不是 – dejvuth