![]() |
home | contact us | about us | |
| The RSA Algorithm (by Lou Brown, last updated 15-MAR-2010) The Brief This is a guide for C# developers who want to implement the RSA algorithm in their applications. Even though RSA was developed in the late 70's, it is still unhackable, and still used today to secure every credit card transaction on the Internet. Two code files are available to help demonstrate the principles. SimpleProgram.cs encrypts and decrypts a single character using small prime numbers. HelloWorldProgram.cs uses larger numbers to encrypt and decrypt a short message. Both are single C# files that compile as console applications. The function names and code shown on this page are taken from these files. I have compiled and run both files on XP with VS2005. Another file you will need is the BroccoliProducts.Maths library, which contains a class called JumboInteger for dealing with very large numbers.
How Does RSA Work? RSA makes use of a mathematical one-way function - the multiplying of two prime numbers. If someone told you they had multiplied two prime numbers together and the answer was 175,828,273, it would be a real chore working out that the two prime numbers were 17,159 and 10,247. Even computers do not like this sort of mathematics. However multiplying 17,159 and 10,247 together is, of course, a trivial task. This function is easy to do one-way and difficult to reverse - it is asymmetric, and RSA is known as an asymmetric cipher. A chap called Rivest (who is the "R" is RSA) devised a one-way function that uses this asymmetric function to transform a number into gobbledygook. He then devised another function which, if the two original prime numbers are known, transforms the gobbledygook back into the original number. The following is a simple example of the RSA algorithm in action. The code for this sample can be found in SimpleProgram.cs. Although I have done my best to explain the programmatic flow, I am not equipped to explain the Euclidean mathematics which RSA is reliant upon. And most of this is buried in the JumboInteger class away from view anyway. I have added a link at the bottom of this page for anyone who really gets of on Prime Number Theory. |
![]() |
This example assumes Alice wants to send a message to Bob.
|
![]() |
The JumboInteger Class In reality the numbers p and q need to be massive. At least 10340 to be unhackable by 100,000 personal computers in 1000 years. Computer languages such as C and Visual Basic do not currently have the ability to calculate AB where A and B are 10340 (although the forthcoming version of Microsoft's .NET will include a large integer class). A special type of integer is required that will support large number mathematics - the JumboInteger. The JumboInteger class is included in the BroccoliProducts.Math library, which is freely available. JumboInteger objects can be used as if they were Int32 objects. However because of the potential size of a JumboInteger, caution should be taken that the operations being performed are written in the most efficient way possible. The JumboInteger class can represent numbers up to ±101073741824, but doing anything other than basic addition and subtraction from a number that large could tie up a personal computer for tens of years. The HelloWorldProgram.cs demonstrates using JumboInteger to perform an RSA encryption and decryption. Randomly Generating a Prime Number There is a function in the JumboInteger class MakeRandomPrime( Int32 iBits ), which generates a random number iBits long. To do this MakeRandomPrime generates random numbers, iBits wide, until one is found that may be prime. I use the term "may be" because the only way to be absolutely sure a number is prime is to divide it by every number from 2 up to the number, and this would take too long for large numbers. The process of filtering possibly prime numbers employed by MakeRandomPrime is as follows:
Please be aware that for large prime numbers, 1024 bits and more, this function can take several minutes. |
|
The HelloWorldProgram C# File The HelloWorldProgram.cs file demonstrates RSA using prime numbers of 512 bits to encrypt and decrypt the message "Hello World!". The steps involved are similar to those described above for the SimpleProgram.cs example, but there is some extra code for making sure the numbers d and e are valid. The string "Hello World!" is 12 characters long, which in Unicode, is 24 bytes, or 192 bits. The value of N=pxq must be larger than 192 bits, so both p and q must be at least 192/2 bits, or 96 bits. However in this example I am using 512 bit prime numbers to demonstrate handling jumbo integers.
In reality, the RSA algorithm is slow and processor-intensive compared to block ciphers such as DES and IDEA. RSA is rarely used for encrypting whole messages or real-time encryption of audio. Instead, RSA is used as a key wrapper for fast single-key encryption algorithms. For example, a message could be encrypted using the DES algorithm, and a randomly generated DES key. This DES key is then encrypted using an RSA public key. The recipient of the message decrypts the wrapped DES key with their private RSA key, and deciphers the message. This use of RSA as a key-wrapper has the benefits of a fast encryption algorithm and no key-distribution problem. MORE INFORMATION Wikipedia - RSA Wikipedia - Public Key Cryptography Wikipedia - The Miller-Rabin Primality Test Bletchley Park - World War II Code Breakers SOFTWARE WORKSHOP SUPPORT Return to the Software Workshop. |
![]() |
© 1998-2010 Broccoli Products Ltd Reg Number: 2895355 Reg Office: 27 Old Gloucester Street, London. WC1N 3AX |
Bug Report Form | Privacy Policy Copyright Notice Liability Disclaimer Contact Us |