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":

#- Copyright (c) 2013,, All Rights Reserved.

  print("Wrong answer:\n");
  $c = 62**65 % 133;
  print("62**65 % 133 = ".$c."\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");


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) 2013,, All Rights Reserved.
  print("Wrong answer:\n");
  $c = pow(62,65) % 133;
  print("pow(62,65) % 133 = ".$c."\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.

Last update: 2013.

Table of Contents

 About This Book

 Cryptography Terminology

 Cryptography Basic Concepts

 Introduction to AES (Advanced Encryption Standard)

 Introduction to DES Algorithm

 DES Algorithm - Illustrated with Java Programs

 DES Algorithm Java Implementation

 DES Algorithm - Java Implementation in JDK JCE

 DES Encryption Operation Modes

 DES in Stream Cipher 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 Secure Is RSA Algorithm?

How to Calculate "M**e mod n"

 Efficient RSA Encryption and Decryption Operations

 Proof of RSA Encryption Operation Algorithm

 Finding Large Prime Numbers

 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

 MD5 Mesasge Digest Algorithm

 SHA1 Mesasge Digest Algorithm

 OpenSSL Introduction and Installation

 OpenSSL Generating and Managing RSA Keys

 OpenSSL Managing Certificates

 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"

 Using Certificates in IE (Internet Explorer)

 Using Certificates in Firefox

 Using Certificates in Google Chrome

 Outdated Tutorials


 PDF Printing Version