A Study and Implementation of RSA Cryptosystem ∗

† [Implementing the well known Rivest Shamir Adleman Public Key Cryptosystem]

‡ Sinjan Chakraborty Vineet Kumar Computer Science and Engineering Department Computer Science and Engineering Department Jadavpur University Jadavpur University Kolkata, 700032 | WB IN Kolkata, 700032 | WB IN [email protected] [email protected]

ABSTRACT of how generation of random primes, key generation and en- This project involves an implementation of the Rivest Shamir cryption and decryption process takes place in RSA. Being Adleman (RSA)[1] encryption in C. It consists of an undergrad students our search for an easy-to-understand generation of two random prime and a co- C implementation of RSA that involved generation of ran- prime to φ(n) also called euler toitent function. These three dom primes lead no where. On not finding a satisfactory are used to generate a public key and private key. The user implementation, we implemented it. has to enter a message which is encrypted by the public key. The algorithm also decrypts the generated cipher text 2. PUBLIC-KEY CRYPTOSYSTEMS with the help of the private key and returns the plain text In a public key cryptosystem each user places in a public message which was encrypted earlier. file an encryption procedure E. That is, the public file is a directory giving the encryption procedure of each user. The General Terms user keeps secret the details of his corresponding decryp- Plain Text: The general message needed to be encrypted is tion procedure D. These procedures have the following four called plain text. properties: Cipher Text: The Garbage like looking string having no information about plain text is called cipher text. 1. Deciphering the enciphered form of a message M yields M. Formally, Keywords RSA, Public Key Cryptosystem, Asymmetric Key Cryp- D(E(M) = M tosystem, Cryptography, Security. 2. Both E and D are easy to compute 1. INTRODUCTION 3. By publicly revealing E the user does not reveal an This project has been done so that people interested in cryp- easy way to compute D. This means that in practice tography, especially students can get a better understanding only he can decrypt messages encrypted with E, or ∗Permission to make digital or hard copies of all or part of compute D efficiently this work for personal or classroom use is granted without fee 4. If a message M is first deciphered and then enciphered, provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice M is the result. Formally, and the full citation on the first page. †A full code of this implementation is availble at arXiv:1506.04265v1 [cs.CR] 13 Jun 2015 E(D(M) = M https://github.com/lugju/rsa ‡Both author of this paper are freshman students audit- ing the RC Bose Summer Research Internship Program on 3. IMPLEMENTATION RSA ALGORITHM Cryptography at Indian Statistical Institute, Calcutta. Both For Implementation of RSA these four famous authors are members of Linux User’s Group Jadavpur Uni- have been used: - versity. • Sieve of (for generation) • Fermat • Miller-Rabin Primality Test • Encryption-Decryption Algorithms.

Proper discussion about each algorithm is mentioned in next subsections. 3.1 Sieve of Eratosthenes p ) or a ∗ s ∗ 2r = −1(modp). If both of them fail, then p The sieve of Eratosthenes is one of the most efficient ways is definitely composite. Otherwise p is probably prime. We to find all primes smaller than n when n is smaller than can choose another a and repeat the same test. We can stop 10 million or so. Following is the algorithm to find all the after some fixed number of iterations and claim that either prime numbers less than or equal to a given integer n by p is definitely composite, or it is probably prime. It can Eratosthenes method: be shown that for any p, at least (3/4) of the numbers less than p will witness p to be composite 1. Create a list of consecutive integers from 2 to n : (2, 3, when chosen as a in the above test. Which means that if we 4,.... , n). do 1 iteration, probability that a composite number is re- turned as prime is (1/4). With k iterations the probability 2. Initially, let p equal 2, the first prime number. of test failing is (1/4)k or 4(-k). This test is comparatively slower compared to Fermat’s test but it doesn’t break down 3. Starting from p, count up in increments of p and mark for any specific composite numbers and 18-20 iterations is a each of these numbers greater than p itself in the list. These quite good choice for most applications. numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked. int Miller(long long p,int iteration){ 4. Find the first number greater than p in the list that is if(p<2){ /*this conditional statement is not marked. If there was no such number, stop. Otherwise, not required for our given program. let p now equal this number (which is the next prime), and We write it for maintaining generality.*/ repeat from step 3. But When the algorithm terminates, all return 0; the numbers in the list that are not marked are prime. } if(p!=2 && p%2==0){ 3.2 Fermat’s Primality Test return 0; If p is the number which we want to test for primality, then } we could randomly choose a, such that a < p and then calcu- long long s=p-1; late a(p−1)modulo p. If the result is not 1, then by Fermat’s while(s%2==0){ Little Theorem p cannot be prime. What if that is not the s/=2; case? We can choose another a and then do the same test } again. We could stop after some number of iterations and if int i=0; the result is always 1 in each of them, then we can state with for(i=0;i

Enter the Cipher text : 573183424

Decrypted keyword : 5321

Example 2:

39703 66883 Figure 1: (Graph)

F(n) phi value = 2655349164 Public Key : {8068769,2655455749} 8. REFERENCES Private Key : {149069429,2655455749} [1] R.L. Rivest, A. Shamir, and L. Adleman, A Method for Obtaining Digital Signatures and Public-Key Enter The Plain Text : 5321 Cryptosystems, Comm. of ACM, 1983. Encrypted keyword : 2521426694 [2] W. Stallings, Cryptography and Network Security Principles and Practice, 5th ed. USA: Pearson Enter the Cipher text : 2521426694 Education, Inc, 2nd edition, 2011. [3] Knuth, D. E., The Art of Computer Programming, Decrypted keyword : 5321 Vol 2: Seminumerical Algorithms., Addison-Wesley, Reading, Mass., 1969. [4] Diffie, W., and Hellman, M. New directions in Comment: These two examples show how on entering the cryptography. IEEE Trans. Inform. Theory IT-22, same message on two different runs, the value of the cipher (Nov. 1976), 644-654. (Nov. 1976), 644-654. text is different because of the generation of different prime [5] Diffie, W., and Hellman, M. New directions in numbers p and q and the different public and private expo- cryptography. IEEE Trans. Inform. Theory IT-22, nents e and d, each time the program is run. (Nov. 1976), 644-654. (Nov. 1976), 644-654. [6] Diffie, W., and Hellman, M. Exhaustive cryptanalysis 6. PROBLEMS IN IMPLEMENTATION of the NBS data encryption standard, Computer 10, The main problem faced is that for a given message M, the June 1977), 74-84. cipher text C generated is large, nearly a 10 digit number. [7] Miller, G.L. RiemannˆaA˘Zs´ hypothesis and tests for Also, the value of d, i.e., the private exponent is of 10 digits. primality. Proc. Seventh Annual ACM Symp. on the To decrypt M, we have to calculate M = Cdmodn. This Theory of Comptng, Albuquerque, New Mex., May calculation is taking a long time in some cases. 1975, pp. 234-239 [8] Goldwasser, S. and Bellare, M., Lecture Notes on 7. ANALYSIS Cryptography, MIT, July 2008. [9] Rabin, M.O., Probabilistic algorithms. In Algorithms and The time taken on computing is not fixed it changes in each Complexity, J. F. Traub, Ed., Academic Press, New York, & every run. After performing a statistical test of encrypting 1976, pp. 21-40. a plain text (which is 25000) & decrypting it time taken is noted

Value of F(n) Time (in sec.) 1614044880 16.5 946335840 21.5 2186888896 60.0 1927086672 17.6 1595209116 18.8 1424163096 9.4 2917216608 28.2

Table 2: Computational Complexity (Data)

Plotting the data it is extremely clear that computational time of phi function, encryption & decryption of plain text & cipher text respectively is not predictable. Some runs of test with takes more time, some takes less time while complexity of computations remaining approximately fixed.