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