[英]RSA Encrypting and Decrypting JAVA
I've been trying to decrypt my encrypted message for a while now, but it isn't working as I'd like it to. 我已经尝试解密我的加密消息已有一段时间了,但是它并没有按照我希望的那样工作。 I have my outputs for encryption working, but I can't decrypt the encrypted message!
我有用于加密的输出,但是无法解密加密的消息!
Here are my values: P : 67 Q : 71 PQ : 4757 PhiPQ : 4620 E : 13 D : 1777 这是我的值: P :67 Q :71 PQ :4757 PhiPQ :4620 E :13 D :1777
Here is my output for the encrypted message (when 'hello' is entered): ????
这是加密消息的输出(输入“ hello”时):
????
(Which is working fine) (工作正常)
Here is my output for the decrypted message (when 'hello' is entered): 1109 314 2309 2309 4015
(Which is working, but does not give me back the characters 'hello') 这是我解密后的消息的输出(输入“ hello”时):
1109 314 2309 2309 4015
(正在工作,但没有将字符“ hello”还给我)
We're supposed to implement this formula into the code (C^D)%PQ but I'm not entirely sure how to implement it when decrypting the encrypted message. 我们应该在代码(C ^ D)%PQ中实现这个公式,但是我不确定在解密加密消息时如何实现它。
I'm not sure what the problem is, here is my code below: 我不确定是什么问题,这是下面的代码:
ENCRYPTION 加密
String encryptedMessage = "";
String message = JOptionPane.showInputDialog(null, "Enter a message: ");
int c = 0;
for (int i = 0; message.length() > i; i++) {
char l = message.charAt(i);
int m = l;
c = 1;
int newE = e;
while (newE > 0) {
if (newE % 2 != 0) {
c = ((c * m) % (pq));
}
newE = newE / 2;
m = (((m * m)) % (pq));
}
encryptedMessage = encryptedMessage + (char) c;
}
System.out.println("Encrypted Message is: " + encryptedMessage);
DECRYPTION 解密的
String decryptedMessage = "";
c = 0;
for (int i = 0; encryptedMessage.length() > i; i++) {
char l = encryptedMessage.charAt(i);
int m = l;
c = 1;
int newE = e;
while (newE > 0) {
if (newE % 2 != 0) {
c = ((c * m) % (pq));
}
newE = newE / 2;
m = (((m * m)) % (pq));
}
decryptedMessage = decryptedMessage + " " + (c);
}
// prints out 'decryptedMessage' value
System.out.println("Decrypted Message is: " + decryptedMessage);
}
}
Your code for encryption and decryption look, well, symmetric . 您的加密和解密代码看起来很对称 。 That's not the trick with RSA.
这不是RSA的窍门。 In RSA, you have the public key
e
for encryption and the private key d
for decryption. 在RSA中,您具有用于加密的公钥
e
和用于解密的私钥d
。 I don't even see d
in your decryption code?! 在您的解密代码中我什至看不到
d
?
@EJP was right. @EJP是正确的。
I put in the decryption key and it worked perfectly! 我放入了解密密钥,它工作得很完美! So, instead of my encryption key being
newE
, I switched it with newD
and it finally works now. 因此,我的加密密钥不是
newE
,而是使用newD
切换,现在终于可以使用了。 It was a stupid, small mistake on my part. 就我而言,这是一个愚蠢的小错误。 Thanks for the hints!
感谢您的提示!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.