**Cryptography Tutorials - Herong's Tutorial Examples** - v5.42, by Herong Yang

How to Calculate "M**e mod n"

This section discusses the difficulties of calculating 'M**e mod n'. The intermediate result of 'M**e' is too big for most programming languages.

If you are interested to apply the RSA encryption yourself manually, we need to learn how to calculate "M**e mod n" and "C**d mod n", which looks simple, but difficult to carry out.

First let's see how difficult is to calculate "C**d mod n" directly even with smaller numbers like "62**65 mod 133" as we saw in the previous example.

Here is a sample Perl script to calculate "62**65 mod 133":

# PowerModTest.pl # Copyright (c) HerongYang.com. All Rights Reserved. # print("\n"); print("Wrong answer:\n"); $c = 62**65 % 133; print("62**65 % 133 = ".$c."\n"); print("\n"); print("Correct answer:\n"); $c = 62*(((62**4%133)**4%133)**4%133) % 133; print("62*(((62**4%133)**4%133)**4%133) % 133 = ".$c."\n"); exit(0);

If you run it, you will get:

Wrong answer: 62**65 % 133 = 21 Correct answer: 62*(((62**4%133)**4%133)**4%133) % 133 = 6

So, why we are getting the wrong answer, if you use the expression "62**65 % 133" that matches the formula in encryption algorithm directly? It could be integer overflow on the intermediate result. I am not sure.

You can try this in PHP script too:

<?php # PowerModTest.php # Copyright (c) HerongYang.com. All Rights Reserved. # print("\n"); print("Wrong answer:\n"); $c = pow(62,65) % 133; print("pow(62,65) % 133 = ".$c."\n"); print("\n"); print("Correct answer:\n"); $c = 62*(pow(pow(pow(62,4)%133,4)%133,4)%133) % 133; print("62*(pow(pow(pow(62,4)%133,4)%133,4)%133) % 133 = ".$c."\n"); ?>

Here is the PHP script output. The direct expression also gives a wrong answer.

Wrong answer: pow(62,65) % 133 = 0 Correct answer: 62*(pow(pow(pow(62,4)%133,4)%133,4)%133) % 133 = 6

Conclusion, we can not carry out "M**e mod n" as two operations directly, because the intermediate result of "M**e" can be too big to be processed in exponentiation operations in most programming languages.

We need to find a different way to calculate "M**e mod n" correctly and efficiently.

Table of Contents

Introduction to AES (Advanced Encryption Standard)

DES Algorithm - Illustrated with Java Programs

DES Algorithm Java Implementation

DES Algorithm - Java Implementation in JDK JCE

DES Encryption Operation Modes

PHP Implementation of DES - mcrypt

Blowfish - 8-Byte Block Cipher

Secret Key Generation and Management

Cipher - Secret Key Encryption and Decryption

►Introduction of RSA Algorithm

What Is Public Key Encryption?

RSA Public Key Encryption Algorithm

Illustration of RSA Algorithm: p,q=5,7

Illustration of RSA Algorithm: p,q=7,19

Proof of RSA Public Key Encryption

►How to Calculate "M**e mod n"

Efficient RSA Encryption and Decryption Operations

Proof of RSA Encryption Operation Algorithm

RSA Implementation using java.math.BigInteger Class

Introduction of DSA (Digital Signature Algorithm)

Java Default Implementation of DSA

Private key and Public Key Pair Generation

PKCS#8/X.509 Private/Public Encoding Standards

Cipher - Public Key Encryption and Decryption

OpenSSL Introduction and Installation

OpenSSL Generating and Managing RSA Keys

OpenSSL Generating and Signing CSR

OpenSSL Validating Certificate Path

"keytool" and "keystore" from JDK

"OpenSSL" Signing CSR Generated by "keytool"

Migrating Keys from "keystore" to "OpenSSL" Key Files

Certificate X.509 Standard and DER/PEM Formats

Migrating Keys from "OpenSSL" Key Files to "keystore"