0

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.

rddead
  • 103
  • 10

1 Answers1

1

The answer is simple: You are using Math.pow

Math.pow works on double values - hence it uses floating point numbers. Floating point numbers are never exact, therefore your result is already screwed up at that point.

If you really want to calculate RSA by hand you can use BigInteger.modPow(..).

However your project is not a learning project I strongly recommend to use the Java Cryptography Extension. Therefore use the BigInteger values and create a RSAPrivateKeySpec/RSAPublicKeySpec and use them with Cipher class.

See for example this question: Generating RSA keys for given modulus and exponent

Community
  • 1
  • 1
Robert
  • 39,162
  • 17
  • 99
  • 152
  • Okay, but switching to BigInteger.modPow() would require that its arguments are all BigIntegers to begin with, right? Not being able to cast = they have to be BigIntegers to begin with. This causes issues with a lot of stuff, including n=(p-1)(q-1). Subtracting one from BigIntegers is a pain. How would I go about doing this? I would prefer to use the cryptography extension but solving it by hand is a requirement. – rddead Jun 14 '16 at 14:52
  • 1
    You got it, you have to everything with BigInteger - every calculation if your number do not fit into `long`. You can substract easily: `value = value.subtract(BigInteger.ONE)` – Robert Jun 14 '16 at 15:17
  • I'm trying to make it work but since I'm using for loops to check whether the numbers I have are prime or not there's even more problems to solve now =( BigInteger has a probablePrime method but not a CheckIfPrime method. – rddead Jun 14 '16 at 16:40
  • I guess what I'm asking is, if my instructor SPECIFICALLY said don't use big int for the arguments (everything except the enc and dec values), how would I go around this? – rddead Jun 14 '16 at 17:02
  • Nevermind, figured out what was wrong. I cast int p and q to BigInt and it all worked out. Time to turn in for me. – rddead Jun 14 '16 at 19:19