4

Theoretically I know that if n=33, e(public key)=3 and d(private key)=7 I can encrypt a plaintext by using BigInteger class with modPow(e, n), and decrypt with modPow(d,n), but after decryption plaintext is not the same as first.

Here is my code:

  public class KeyTest {
private BigInteger n = new BigInteger("33");
private BigInteger e = new BigInteger("3");
private BigInteger d = new BigInteger("7");

public static void main(String[] args) {
    KeyTest test = new KeyTest();

    BigInteger plaintext = new BigInteger("55");
    System.out.println("Plain text: " + plaintext);

    BigInteger ciphertext = test.encrypt(plaintext);
    System.out.println("Ciphertext: " + ciphertext);

    BigInteger decrypted = test.decrypt(ciphertext);
    System.out.println("Plain text after decryption: " + decrypted);
}

public BigInteger encrypt(BigInteger plaintext) {

    return plaintext.modPow(e, n);
}

public BigInteger decrypt(BigInteger ciphertext) {

    return ciphertext.modPow(d, n);
}
}

The output is:

Plain text: 55 Ciphertext: 22 Plain text after decryption: 22
2
  • 2
    Your "plaintext=55" is greater than the "modulus=n=33" so what is actually getting encrypted is number 22 (55 mod 33). Also it so happens then 22 encrypted under modulus 33 is again 22. Commented Jun 17, 2014 at 14:11
  • The algorithm should be (pow(plaintext, pubKey) mod n), the first operation should be pow and then the mod operation is I'm not right? Commented Jun 17, 2014 at 14:19

1 Answer 1

3

Your plaintext (55) is larger than the the modulus (33), so you can't actually encrypt the message. Consider the following slightly different example:

  • p = 11
  • q = 17
  • n = 187
  • phi(n) = 160
  • Choose e = 3
  • If d = 107 then e * d = 321 = 1 mod phi(n)

So change your code to:

  private BigInteger n = new BigInteger("187");
  private BigInteger e = new BigInteger("3");
  private BigInteger d = new BigInteger("107");

  public static void main(String[] args) {
    KeyTest test = new KeyTest();

    BigInteger plaintext = new BigInteger("55");
    System.out.println("Plain text: " + plaintext);

    BigInteger ciphertext = test.encrypt(plaintext);
    System.out.println("Ciphertext: " + ciphertext);

    BigInteger decrypted = test.decrypt(ciphertext);
    System.out.println("Plain text after decryption: " + decrypted);
  }

  public BigInteger encrypt(BigInteger plaintext) {

    return plaintext.modPow(e, n);
  }

  public BigInteger decrypt(BigInteger ciphertext) {

    return ciphertext.modPow(d, n);
  }
}

Output:

Plain text: 55
Ciphertext: 132
Plain text after decryption: 55
Sign up to request clarification or add additional context in comments.

1 Comment

I understand now why after decryption in my code i am getting "wrong" answer, because it is impossible to calculate bigger number than 33 after mod 33 operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.