0

As part of one of my university assignments I've been asked to code the RSA algorithm to encrypt a user message. My encryption code is here:

BigInteger byte_message = new BigInteger(user_message.getBytes());
BigInteger encrypted_message = byte_message.modPow(public_key, modulus);

Then for the decryption my code is currently:

BigInteger cipher = new BigInteger(encrypted_message);
BigInteger decrypted_message = cipher.modPow(private_key, modulus);

The error Java is throwing at me is:

**The constructor BigInteger(BigInteger) is undefined. 
1 quick fix available: Change type of 'encrypted_message' to 'byte[]'**

How do I convert the encrypted_message from bytes to BigInteger so that I can then run the modPow section of my code?

Any help is much appreciated!

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    Your question title and body did not match. The title said you are asking about converting a byte array to a `BigInteger`. In the body you clearly know how to do that and you are actually asking about how to make a copy of a `BigInteger`. I have fixed this. – Stephen C May 08 '18 at 22:41

2 Answers2

1

encrypted_message is already a BigInteger. There's no need to construct a new BigInteger from it, because you can simply assign it to cipher:

BigInteger cipher = encrypted_message;

Also note that BigInteger (like String) is immutable, so there's (usually) no reason to create a new BigInteger instance equal to one you already have.

k_ssb
  • 6,024
  • 23
  • 47
  • Yes but if I do that and then do: BigInteger decrypted_message = cipher.modPow(private_key_file, modulus); System.out.println("Decrypted message = " + decrypted_message); The output does not match up with my original message. – James Kemp May 08 '18 at 22:20
  • 3
    @JamesKemp Then that must be an unrelated error in your code. If your intent for the line `BigInteger cipher = new BigInteger(encrypted_message);` is to have `cipher` be a `BigInteger` that is equal to `encrypted_message`, then `cipher = encrypted_message` is the correct approach _for that line_. – k_ssb May 08 '18 at 22:23
1

As @pkpnd points out, there is no need to make a copy of the BigInteger object that encrypted_message refers to. A simple assignment of the reference is sufficient.

In the (extremely limited) case that you actually need to do this, you can make a copy as follows:

BigInteger cipher = new BigInteger(encrypted_message.toByteArray());

(They have not provided a BigInteger.clone() method or BigInteger(BigInteger) constructor, since it is almost always unnecessary and inefficient to copy these objects. They are immutable.)


How do I convert the encrypted_message from bytes to BigInteger so that I can then run the modPow section of my code?

You don't need to. You have already done it at the start of your code, and you don't need to do it again. (And if you really do it a second time, then do it the same way that you did at the start of the code!)

 1 quick fix available: Change type of 'encrypted_message' to 'byte[]'**

Note that the fixes suggested by your IDE are not always the best advice. The IDE's compiler doesn't understand what you are intending your code to do. It takes a human brain to do that.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216