I have my RSA encryption working... it's just that I'm totally stuck on how to decrypt the RSA. Here is the code that I have right now and any little hints would gladly be appreciated. My output shows the encrypted message as gibberish, but my decrypted message isn't popping out as it should:
EDIT: Oh, assume that P, Q, PQ, PhiPQ, E and D are already found. I have these found already. I just don't know exactly how to code the decryption of the RSA.
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);
// ********************************************************************
// Decrypting
// ********************************************************************
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 = (int)((Math.pow(c, d)) % pq);
}
decryptedMessage = decryptedMessage + c;
}
// prints out 'decryptedMessage' value
System.out.println("Decrypted Message is: " + decryptedMessage);
}
}
Aucun commentaire:
Enregistrer un commentaire