# Long, modulo and RSA-algorithm

**URL:** <https://discourse.processing.org/t/long-modulo-and-rsa-algorithm/6727>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [December 18, 2018, 9:57am UTC](https://discourse.processing.org/t/long-modulo-and-rsa-algorithm/6727 "2018-12-18T09:57:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![crizzer](https://avatars.discourse-cdn.com/v4/letter/c/d9b06d/32.png) [@crizzer](https://discourse.processing.org/u/crizzer)\
**Post date:** [December 18, 2018, 9:57am UTC](https://discourse.processing.org/t/long-modulo-and-rsa-algorithm/6727/1 "2018-12-18T09:57:11Z")

</div>

As a school project, I have to make a program that can encrypt and decrypt code, whilst also registering the encoded code in a database.

But I’ve encountered a problem:

To encrypt a message using RSA, you need to lift the message to a power and then take the modulo. That’s all fun and games, until I start using double digit numbers. The result of the exponent quickly reaches the long max value, 9,223,372,036,854,775,807.

This is a major problem, since taking the modulo of 9,223,372,036,854,775,807 will always result in the same result, making every bit of message the same.

Does anybody have a way to work around this? The math program Maple is able to do the calculations perfectly, but it’s quite a bother to be switching between the two programs in an assignment.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![figraham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/figraham/32/4161_2.png) [@figraham](https://discourse.processing.org/u/figraham)\
**Post date:** [December 18, 2018, 12:14pm UTC](https://discourse.processing.org/t/long-modulo-and-rsa-algorithm/6727/2 "2018-12-18T12:14:44Z")

</div>

Check out [BigInteger](https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html) it’s designed for storing numbers that exceed the max size of a long. There’s also BigDecimal for floating point numbers.

```auto
import java.math.BigInteger;

void setup() {
  BigInteger bigInt = new BigInteger(String.valueOf(Long.MAX_VALUE));
  println(bigInt);
  println(bigInt.multiply(new BigInteger(String.valueOf(Long.MAX_VALUE))));
}

//Output
9223372036854775807
85070591730234615847396907784232501249

```

---

<div class="post-metadata">

**Author:** ![crizzer](https://avatars.discourse-cdn.com/v4/letter/c/d9b06d/32.png) [@crizzer](https://discourse.processing.org/u/crizzer)\
**Post date:** [December 18, 2018, 5:00pm UTC](https://discourse.processing.org/t/long-modulo-and-rsa-algorithm/6727/3 "2018-12-18T17:00:16Z")

</div>

Thank you very much, might just work 😃
