I'm writing a program to encrypt and decrypt text using RSA. The values of P, Q, and E are provided by the user and checked to see if they are prime. The program finds N and D. The encryption and decryption are malfunctioning. I've looked at a few of the questions on here and most of them use libraries which for this assignment I'm not meant to use. Code snippet of encryption:
JMenuItem mntmEncrypt = new JMenuItem("Encrypt");
mntmEncrypt.addActionListener(new ActionListener() {
@SuppressWarnings("null")
public void actionPerformed(ActionEvent ev) {
textArea_1.setText("");
String blah = textArea.getText();
int something;
for(int i=0; i<blah.length();i++){
something =(int)(blah.charAt(i));
enc = BigInteger.valueOf((long) (Math.pow(something, e)%n));
textArea_1.append(blah.valueOf(enc) + " ");
}
And malfunctioning decryption:
JMenuItem mntmDecrypt = new JMenuItem("Decrypt");
mntmDecrypt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent em) {
textArea_1.setText("");
String blah2 = textArea.getText();
String[] somethingElse = blah2.split(" ");
char character;
for(int i=0;i<somethingElse.length;i++){
int cipher = Integer.parseInt(somethingElse[i]);
dec = BigInteger.valueOf((long)Math.pow(cipher, d)%n);
System.out.println("Cipher: "+cipher); //print check
System.out.println("d: "+d); //print check
System.out.println("n: "+n); //print check
System.out.println("dec: "+dec); //print check
BigInteger x = BigInteger.valueOf(dec);
int x2 = x.intValue();
char c = (char) x2;
System.out.println(cipher + " " + dec);
String textBack = new String(dec.toByteArray());
textArea_1.append(String.valueOf(dec));
}
}
});
I checked the value of dec using this calculator and it is completely wrong but I can't see why. Any help would be appreciated. I CANNOT make anything BigInteger except enc and dec.