88.

Learn Encryption Techniques with BASIC and C++
(Publisher: Wordware Publishing, Inc.)
Author(s): Gil Held
ISBN: 1556225989
Publication Date: 10/01/98

Previous Table of Contents Next


Exponentiation Operations

While it is possible to attempt to raise a number to a large power, doing so will more than likely exhaust the capacity of your computer as there is a finite limit to the size of integers on each computer. Instead of directly raising a number to a power, you can do the modular reduction after each multiply operation. For example, assume you want to compute 1237 mod 456. Instead of computing 123 multiplied by itself 7 times and then dividing by 456 to obtain the remainder, let’s do modular reduction after each multiply. That is:

     1232 = 123*123 = 15129 mod 456 = 81 mod 456     1233 = 123*81  mod 456 = 9963  mod 456 = 387 mod 456     1234 = 123*387 mod 456 = 47601 mod 456 = 177 mod 456     1235 = 123*177 mod 456 = 21771 mod 456 = 339 mod 456     1236 = 123*339 mod 456 = 41697 mod 456 = 201 mod 456     1237 = 123*201 mod 456 = 24723 mod 456 = 99  mod 456 

While the preceding method reduces the computation to a series of seven small multiplies and seven small divides, most exponents used with RSA are considerably larger. This means that you would more than likely want to obtain a more efficient method to compute exponents.

To raise a number x to an exponent, you can perform a series of squaring operations. For example:

     1232 = 123*123 = 15129 mod 456 = 81  mod 456     1234 = 81*81   = 6561  mod 456 = 177 mod 456 

While you could continue and square 123 again, obtaining the value of 1238, unfortunately the 1237 that you require is not a power of 2. However, if you know what 123x is, then it is relatively easy to compute 1232x, since you obtain that by squaring 123x. Similarly, you can compute 1232x+1, as all you need to do is multiply the result obtained by squaring 123x (1232x) by 123. This means you can perform a sequence of squaring operations to obtain the nearest power of two to the exponent you require, and then multiply the result n times, where n is the difference between the exponent you seek and the nearest computed power of 2. For example:

     1237 mod 456 = 1232 * 1232 * 123 * 123 * 123 mod 456     1234 mod 456 = 177 mod 456 

Then:

     1237 mod 456   = 123*123*123*177 mod 456                    = 123*123*339 mod 456                    = 123*201 mod 456                    = 99 mod 456 

Note that the preceding method required two squaring and three multiplication operations. You can reduce the number of operations further if you note that 123a*123b = 123a+b. This means given:

     1232 mod 456 = 81  mod 456     1237 mod 456 = 387 mod 456 

you would compute the following:

     1236 = 1232*1234 = 81.387 mod 456 = 339 mod 456     1237 = 123*1236 = 123.339 mod 456 = 99 mod 456 

Note that in this example you would compute 1232 mod 456, 1234 mod 456, 1236 mod 456, and then multiply the last result by 123. This reduces the number of computations to two squares and two multiplies.

If you convert the exponent to binary, you can compute 123 raised to a sequence of powers. For example, 7 is 1112 which represents the sequence 12, 112, 1112. Note that each successive power concatenates one additional bit of the desired exponent. Also note that each successive power (from the most significant bit position) is either twice the preceding power or once or more than twice the preceding power. Thus, you can raise 123 to the first, third, and seventh power by repeated squaring together with multiplication by 123 for the bits that are 1. For example, commencing with the first power you start with:

     123 mod 456 

To obtain the third power, you would multiply 123 by its square, i.e., 123 (1232). To then obtain the seventh power, you would obtain the sixth power (1233)2 and multiply by 123. Thus, raising 123 to the seventh power can be accomplished by repeated squaring and, as required, multiplying by 123 for the bits that are 1. Focusing on exponents:

     7 = (((1)2 + 1)2 +1) 

Then you obtain:

     1237 = (((123)2 * 123)2 * 123) 

Using the preceding method you can perform exponentiation of a base to an exponent as follows:

1.  Commence by setting an initial value to 1.
2.  Convert the exponent to binary.
3.  Read the binary value of the exponent bit by bit from high order to low order and square your value for each position. If the value of the bit position is 1, then multiply by the base. After each operation, perform modular reduction to maintain a relatively small intermediate result.

Now that you have an appreciation for techniques to facilitate exponentiation, let’s return to the key computation process. It was noted previously that the inverse of 41 mod 3233 is:

     413231 mod 3233 

Note that 3231 is 1100100111112. This means you can compute 41 raised to the 3231 power by squaring 12 times and multiplying 8 times. Although I will leave it as an exercise for you to compute, let’s assume the answer was x. You would then publish e and n and keep the value x which represents your secret decryption key. Suppose you want to encrypt the famous message ABADABA… Then, the ASCII value of your message m is:

     m = 64656467646564… 

To facilitate computations you would break the message into small blocks. For example:

     m1 = 6465     m2 = 6467     m3 = 6465 

and so on.

Using the encryption key of 41, the first block would be encrypted as:

     c1 = 646541 mod 3233 

Similarly, the second block would be encrypted as follows:

     c2 = 646741 mod 3233 

You would continue the same operation on the remaining blocks until you generated the entire encrypted message. As previously discussed, decryption would be performed on a block by block (c1, c2…) basis, performing the same exponentiation but using the decryption key whose value is x. Thus:

     m1 = c1x mod 3233     m2 = c2x mod 3233 

and so on.


Previous Table of Contents Next


Learn Encryption Techniques with Basic and C++
Learn Encryption Techniques with BASIC and C++
ISBN: 1556225989
EAN: 2147483647
Year: 2005
Pages: 92
Authors: Gil Held

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net