B131590 2012

Acknowledgement
Praises to Allah the Almighty who gave me all the strength to complete the project on time. It has been a tough 3 months and without the support of people around me, the project will not be as how it turned out to be now. First, I’d like to thank my project supervisor, Dr Ana Salagean, for supporting and offering advice and pushing me to keep trying harder and make the project work. Without her continuous guidance I would not be able to finish up this project and enjoy the pressure of trying to get everything done with a good quality. I would also like to express my gratitude to His Majesty the Sultan of Brunei for awarding me with the scholarship and to Brunei’s Ministry Of Education for giving me the opportunity to further my studies and financially support my course. Special thanks to Henrik Vestamark - who have helped me with the arbitrary arithmetic library and not only promptly responded to any queries but also, has offered help at all times. To my father and mother, Ayah, Ummi and my other family members and friends for the continuous support and encouragement it would have not been possible to complete this without their support and comforting words to always be strong and healthy at the same time. I wish them a blessed life both here and the hereafter

Amal Umi Hafizah Md Yusoff

i

B131590 2012

Abstract
The first aim of this project is to explore and implement the flow, method and techniques of one of the public-key cryptography techniques, namely the RSA cryptographic algorithm. This study of understanding the overall flow of the algorithm includes the logical and mathematical aspects that are implemented in the algorithm. The general aim of this project is to implement and evaluate the RSA public key algorithm, including the encryption and decryption algorithm along with the key creation using different mathematical techniques, such as the Euclid algorithm, the extended Euclid algorithm, and the square and multiply algorithm. A more advance aim for this project is also conducted. This aim is to conduct a further study of how the RSA algorithm is able to be increased in security and the time taken to do encryption and decryption, with advance techniques, in particular Chinese Remainder Theory. This project also mentions the usage of RSA in many areas, particularly in Internet protocols such as SSL/TLS and SSH. Later in the project a general view of attacks on RSA is also conducted. The attacks such as cryptanalysis and prime factors choices are looked into in details. A few implementations are made in this project and each of them signifies several aspects of RSA’s technique. It was proved that efficiency in RSA is dependent on the techniques and methods being used in the encryption and decryption algorithm. This was shown by using the time difference during the processes.

Amal Umi Hafizah Md Yusoff

ii

B131590 2012

Table of Contents
Acknowledgement ................................................................................................................................................... i Abstract....................................................................................................................................................................... ii 1.0 Introduction .................................................................................................................................................. 1 2.0 RSA Brief Background .............................................................................................................................. 1 3. 0 RSA Usage ..................................................................................................................................................... 2 4.0 Implementation Tools for RSA .............................................................................................................. 3 5.0 RSA Method................................................................................................................................................... 3 5.1 Encryption Algorithm .......................................................................................................................... 4 5.2 Decryption Algorithm .......................................................................................................................... 6 5.4 Chinese Remainder Theorem ........................................................................................................... 7 6.0 Attacks on RSA’s algorithm .................................................................................................................... 8 6.1 Small Prime Factors .............................................................................................................................. 9 6.2 Ciphertext Attack ................................................................................................................................... 9 6.3 Side-Channel Attacks......................................................................................................................... 10 6.4 Common Modulus............................................................................................................................... 10 6.5 Cycle Attack ........................................................................................................................................... 10 7.0 Using OAEP to Enhance RSA Security ............................................................................................. 11 8.0 Implementation ........................................................................................................................................ 11 8.1 Initial Implementation...................................................................................................................... 11 8.2 Improved Encryption and Decryption Method Implementation .................................... 14 8.3 Further Improved Encryption and Decryption Algorithm Implementation .............. 17 8.4 Improvised Decryption Technique in the Algorithm : CRT .............................................. 25 9.0 Efficiency Comparison ........................................................................................................................... 29 9.1 Simple multiply and modulus method Vs Square and Multiply Method ..................... 30 9.2 Square and Multiply Vs CRT ........................................................................................................... 31 11.0 Conclusion .................................................................................................................................................... 32 11.1 Comparison of Implementation ................................................................................................. 32 11.2 Problem Encountered During Implementation ................................................................... 33 11.3 RSA Conclusion ................................................................................................................................. 34 11.4 Future Works ..................................................................................................................................... 35 Bibliography.............................................................................................................................................................. a

Amal Umi Hafizah Md Yusoff

iii

................................................................................................. a Amal Umi Hafizah Md Yusoff iv .................................................................................................................................................................................................. a Flow of Algorithm for Improvised Decryption Technique in Algorithm ................................................... a APPENDIX C .......................................... a APPENDIX B ......... a Code for further improved encryption and decryption implementation ........................................................................................ a Code for improved encryption and decryption implementation .................................B131590 2012 Appendix A ....................................................................................................................................................................................................... a Code for initial implementation ....... a APPENDIX E ................................................................................................................. a Code for Improvised Decryption Technique Implementation ............................................................................................ a APPENDIX D .................................................................................................................................................................

to have a working RSA tested using small integers. namely Chinese Remainder Theorem. RSA. Comparisons of efficiency will be made at the end of the paper. It is proven that prime factorization is the main element to the complexity and thus helps to ensure the high security in RSA. It takes some mathematical background to understand the algorithm especially when computing the encryption and decryption exponent but it can be relatively easy to comprehend and implement. is mainly used for public key encryption and it is widely used in Internet protocols such as SSL/TLS. is the most widely used public key algorithm due to its high security. which is undeniably one of the best of its kind. Further discussed in the sections. will be the possible attacks on RSA that have been discovered by people who either uses the system. Adi Shamir and Leonard Adleman in 1977. as mentioned before. The current key size is a 1024 bit key which is equivalent to 307 decimal digits. This project will focus a little on the two of the protocols in another section. The first implementation would be an initial implementation. PGP and IPSEC. SSH. Final implementation will be using an advance decryption technique. and the pseudo code.0 Introduction RSA has been most commonly “declared” as one of the safest methods for cryptography. This project will give several implementations to evaluate the efficiency of some of the many techniques used in RSA. named after the inventors Ron Rivest. This is accredited to the public key creation from the algorithm. As mentioned above. It supports encryption and digital signatures. This project will deliberate on the methods used in RSA including the encryption and decryption algorithm. 2. Third implementation will be using the square and multiply technique which is the standard technique in the algorithm currently.B131590 2012 1. Amal Umi Hafizah Md Yusoff 1 . absorbing how long it takes to process the encryption.0 RSA Brief Background RSA. The second implementation is to develop RSA using a simple multiply and modulus technique. RSA gets its high security from the complex factorization of large prime numbers. or by people who have developed a system using RSA as their security mechanism or even a hacker.

The receiver then decrypts the cipher text using his private and public key. Secure Socket Layer (SSL) and Transport Layer Security (TLS) are one predecessor of another.0. in the first service that is the peer’s identity authentication. PGP and SSH. in particular how they use RSA in their system. 0 RSA Usage The strong public key encryption being offered by RSA has been recognized by the public and is implemented in many security-based program or protocols. secure shared key negotiation and reliability of negotiation services. The next program that utilizes RSA is the Pretty Good Policy or better known as PGP. RSA is used for key exchange. It includes encryption. Amal Umi Hafizah Md Yusoff 2 . it consists of two layers. The encrypted file generated by PGP is encoded using IDEA. A further discussion on this part will be given in section 6. Similar to SSL version 3. International Data encryption Algorithm (IDEA) and RSA.2. Radix-64. Next is to discuss each of the protocols in brief detail. there is always a part of the system or algorithms that are vulnerable and can be claim as a weakness. The TLS Handshake Protocol provides authentication. Further description of the encryption and decryption algorithm will be discussed in section 5.2.B131590 2012 In RSA’s public key cryptography.0.1 and 5. Some of the protocols are SSL/TLS. Firstly. where key exchange and server authentication are combined. the sender who wishes to send a message has to obtain the receiver’s public key and perform encryption with it. 3. which are used to manage and transport a secure message over the Internet. for the secret keys and RSA for the public-key. namely TLS Record Protocol and TLS Handshake Protocol. As with any other security algorithm. signing of keys. It is a public-key encryption program designed to secure e-mail transaction over the Internet. then the cipher text will be passed to the receiver. Referring to TLS version 1. decryption and authentication which use three of the best known cryptography algorithms. This section focuses on the protocols that use RSA in their data securing algorithm. which will encrypt the secret key into another round of encryption. the two layers are SSL record protocol and SSL protocol. which also uses RSA for the same purpose.

created by Henrik Vestermark and is designed for C++ programming language and has mathematical functions such as addition. This project uses the Precision Library. subtraction. and data tunneling services for remote access.B131590 2012 Secure Shell (SSH) is another protocol that utilizes RSA. RSA is used to authenticate the identity of communicating users by utilizing its public and private key 4. There are also other types of header files that deal with complex numbers and interval precision. User Authentication Protocol and Connection Protocol. Next the paper discuss the Secure Shell program designed especially for TCP/IP application to provide command shell. SSH have three main components: Transport Layer Protocol. namely public and private keys. some additional tools are needed. In this project an additional tool is implemented.0 RSA Method RSA is as mentioned above one of the strongest encryption algorithms in securing data transactions on a network. There are also operator functions such as logical or and logical and. This project only uses the iprecision package. One of the factors that contributes to that is the keys factors used in the algorithm. file transfer. 5.0 Implementation Tools for RSA The complex algorithm and the use of huge prime numbers are beyond the capability of any programming compilers. along with modern and advanced Amal Umi Hafizah Md Yusoff 3 . also the input output stream. multiplication and modular and exponential computation which are the main operations in the RSA algorithm. It deals with securing the connection to the SSH server and providing a secure link to access the resources at the server. such as iprecision which was designed for integer type and fprecision for floating numbers. which includes division. In order to implement RSA to be fully functioning and to meet the criteria (large keys creation). namely the Arbitrary precision arithmetic library The Arbitrary precision arithmetic library is used to handle large numbers and mathematical operations. The Precision Library has a number of packages for multiple function specification. SSH is a protocol used to secure and authenticate mostly for remote access and file transfer in which RSA is used to generate keys and for authenticating. algorithm. multiplication division and modulus.

also called cipher text. OAEP and also the Chinese Remainder Theory which had already been mentioned earlier in this paper. for example. In the algorithm there are again many techniques to achieve the variable values that will be needed for the encryption process. Next we discuss the whole calculation to determine each value of e and n for the public keys. 5. n) which will be visible globally via the Internet.1 Encryption Algorithm RSA has more than one algorithm . C. To solve gcd(e. n. gcd(e. the value of e is next to be solved. the output will be an encrypted plain text. called the totient function. Below are the formulae to compute n and r. e. To derive at the public encryption key. two large prime numbers are needed initially. normally assigned as p and q. using e and r. and a system modulus. such that their greatest common divisor. Public keys are used to arrive at the ciphertext. denoted as PU(e.r) = 1 . This can be verified by doing the following calculation: ed mod r = 1 or ed -1 is divisible by r In this project the Euler Euclid algorithm will be used to solve e as it is a faster way to compute it rather than using the modulo operation.B131590 2012 padding techniques such as the padding scheme. which then comes together to form : C = me mod n where m is the plain text (message). The message m must be less than n. The condition of the correct value of e is that it must be in the range of 1 < e < r. p and q are used to compute the system modulus n and the value of r. Public keys consists of an encryption exponent. the Euclid algorithm and the square and multiply algorithm.r) by the algorithm. to name a couple.the most used one being implemented in this project. compute a sequence of numbers with both numbers being the first two. Each Amal Umi Hafizah Md Yusoff 4 . n=p*q and r = (p-1) * (q-1) Now that the values of n and r are set. For the encryption process. which will be used to compute the encryption and decryption exponent.

B131590 2012 subsequent number is the remainder of the division of the previous two. Example 2: Computing m45 mod n e = 45 45 = 32 + 8 + 4 + 1 (101101) m45 mod n = m32+8+4+1 mod n m32 m8 m4 m1mod n = (m32 mod n)( m8 mod n )(m4 mod n)(m1 mod n )mod n when m = 7 and n = 11. This is better illustrated in the example below. This is done exactly as the name suggests. write e as a sum of powers of 2.7) 40 7 5 2 1 0 (40 = 7 * 5 + 5) (7 = 5 * 1 + 2) (5 = 2 * 2 + 1) (2 = 2 * 1 + 0) Table 1. To explain better how the square and multiply algorithm work. by squaring and multiplying. C = 745 mod 11 e = 45 45 = 32 + 8 + 4 + 1 (101101) 745 mod 11 = 732+8+4+1 mod 11 732 78 74 71mod 11 = (732 mod 11)( 78 mod 11 )(74 mod 11)(71 mod 11 )mod 11 C = (5)(9)(3)(7) mod 11 = 10 Amal Umi Hafizah Md Yusoff 5 . Example : computing gcd(40. let’s look at another example. then the encryption can be made. Example of computing using Euclid Algorithm When e is validated. similar to writing e in binary form. In the formula C = me mod n. The last computation must result in a zero and the last non-zero number in the sequence will be the gcd(e. The encryption is done using a much faster algorithm namely the fast exponentiation approach or better known as square and multiply approach. then multiply together the corresponding powers of m.r).

Note that the decryption exponent here is denoted as d. Example of computing using the extended Euclid Algorithm u 1 0 1–0x5=1 0 – 1 x 1 = -1 1 – (-1) x 2 = 3 v 0 1 0 – 1 x 5 = -5 1 . P = cd mod n where P will be the decrypted plain text. the private key would be PR(d. n). namely (u.2 Decryption Algorithm Decrypting a message is just the reverse of encrypting plain text. for any two numbers of (r. This is simply an extension to the Euclid algorithm used for the encryption exponent. two other values will be computed. In the algorithm. e).B131590 2012 That conclude how encryption is being done specifically in this project. First take the two numbers e and r. write a sequence of it similar to what we did before. v). n). which is only known to the users. Example : computing gcd(40.7) s 40 7 5 2 1 0 Table 2. the extended Euclid algorithm is implemented. It reads the encrypted cipher text and begins decrypting using another algorithm and a different key.6 x 2 = -17 5 1 2 2 (40 = 7 * 5 + 5) (7 = 5 * 1 + 2) (5 = 2 * 2 + 1) (2 = 2 * 1 + 0) q Amal Umi Hafizah Md Yusoff 6 . such that r u + e v = gcd (r. While for the public key the sender makes available to the whole Internet PU(e.e) which are achieved similar to the Euclid algorithm. aiming to achieve efficiency for the process. Decryption will be done using the following formula. To determine the value of d. an example is given below. is also taken into account.(-5) x 1 = 6 (-5). 5. having the remainder and now an addition of the quotient q.

d is called the multiplicative inverse of e in modulo r arithmetic. If the result of gcd(r. The decryption process can be commenced using the formula mentioned before. the private key PR (d. perform the modulo r function.e) which is the last non-zero value of s. 5. Taking the equation above. where (r * u) + e * v) = 1. that is P = cd mod n. CRT is having two.B131590 2012 To arrive at the values of each column of u and v. It is a theory that is used in RSA to boost up the decryption process for greater efficiency. (r mod r)(u mod r) + (e mod r)(v mod r) = 1 Since r mod r = 0. the following formula is applied: the entry two positions higher – (the entry one position higher x the quotient on the same line) as the font colour has indicated. which is also equal to (r * final u) + (e * final v). Therefore we can conclude that d = (v mod r) Now that the value of d is defined. The output of this calculation will be the gcd(r.4 Chinese Remainder Theorem Chinese Remainder Theorem (CRT) is a theory originated from the idea of congruency in number theory. we can determine the decryption exponent d. Using this theorem will result to having as much as four times faster than the normal RSA decryption algorithm of square and multiply. which determine a value n. This can be clearly illustrated in the figure below along with an example.n) is defined which will remain known only to the sender and recipient. when divided by several given divisors leaves a given remainders. this algorithm is very much preferred. we get (e mod r)(v mod r) = 1. Instead of having just one modular exponent of n in square and multiply method. gcd (40. do a modulo r to each of the term so that it looks like the equation below. In other words. then d comes in such that ed mod r = 1. the reason will be explained next while explaining the algorithm.7) = 1 (40 * 3) + (7 * -17) = 1 With the above calculation using the extended Euclid algorithm. The algorithm for using CRT is a bit complicated than the square and multiply but for the efficiency purpose. Amal Umi Hafizah Md Yusoff 7 .e) = 1.

Mq) (mod p) P = Mq + (h * q) As can be seen in the algorithm above. this contributed to the faster decryption process. Amal Umi Hafizah Md Yusoff 8 . four additional values will be derived using the formula below: 1. Naturally. “The important thing to remember is that no matter how secure you think your system is. PU(e. it has been said that its strength lies in the complex prime factorization of the encryption and decryption exponents. n)consists of an encryption exponent and the overall modulus. Mp and Mq. q-1 (mod p) To calculate 3. 2001). there may be something you have not considered.B131590 2012 With the knowledge of p and q. dp = d(mod p-1) 2. dq = d(mod q-1) 3. extended Euclid algorithm is being used.” (Killeen. some try to find weaknesses or any opening through which to attack the strength or most important factor of the program. we are computing two modular exponentiations.0 Attacks on RSA’s algorithm Kileen said of the attacks of cryptography attacks. This reminds the system developer that there will always be flaw or imperfection in what one has developed. The public key. 6. Although it requires extra computing but the value to be computed. Since RSA is mostly being used for its public-key algorithm that is exactly what attackers are focus upon. Mp and Mq must be calculated using below’s formula     Mp = mdp (mod p) Mq = mdq (mod q) h = q-1 * (Mp . that are the exponent and system modulus are smaller. For RSA. And so to decrypt a message m.

which consists of PR(d. the attacker would need to determine the value of p and q. This could be handled by not allowing small factors to be used and putting a limit on the minimum bit of numbers for the prime factors. which are the factors that make up modulus n. they would easily get the value of d. Factoring small or insufficiently large enough integers is not easy to do.p. 2007). one must choose to use a more efficient method to factor n . p and q. Upon knowing the totient function (r).q). Lenstra. Barker. Dodson. Burr. given that the prime factors are very large numbers. Leyland. Therefore to prevent this attack. Polk. In this type of attack. Some examples of the factored modulus of RSA are. although the current key is 1024bit. When factoring using small numbers the system can easily be brought down by hackers using brute force or exhaustive key methods. Even though it sound simple.B131590 2012 6.2 Ciphertext Attack An attacker would try to obtain the private key. a 1024 bit RSA key would not be sufficient to provide cryptographic protection after 2011 (Barker. Especially in the world of today. Lioen. and Zimmerman using number field sieve as stated by (Aboud.1 Small Prime Factors The first and most obvious weaknesses which could arise when developing RSA is to allow small prime factors to be used for the prime exponents. and this will result in the security being very weak.where it has been proved by many researchers that factoring n is not impossible at all. & Smid. Bruce. Since in the public key there is e and n. which if they succeed will give them the value for the totient function ((p-1)(q-1)). it is a very complex task to do. Montogomery. Arjen and Mark and RSA-140 was factored by Cavallar. Murphy. 2009). 6. RSA-120 was factored by quadratic sieve by Thomsan. the attacker has the knowledge of both plaintext and the encrypted text. Amal Umi Hafizah Md Yusoff 9 . if there is any at all.

The method of repeated squaring has its vulnerability towards attacks. Although the public and private keys will be safely generated.3 Side-Channel Attacks Simple Power Analysis (SPA) and Differential Power Analysis (DPA) are two of the most known types of the side-channel attacks. an attempt will be made to encrypt the ciphertext repeatedly until the plaintext can be seen. 2001 is “to use the public exponent of the public key to re-encrypt the text.5 Cycle Attack As the name suggests.” Although he stated that any exponent that is co-prime to totient function r. Amal Umi Hafizah Md Yusoff 10 . & Peng.1 with the use of a fast binary method is what is commonly used in the RSA algorithm due to its efficiency. where it made use of the technique of looping to infuse an attack. Classical power-analysis protected implementation of exponentiation is being used and main idea of the remedy is to randomize the private exponent at each calculation. would work. 6. The suggested remedy for this attack by Kileen. having the knowledge of ciphertext. While on the loop. it would be an impractical attack as it takes a very long time to cycle large prime factors. Zheng. it is shared among the respective people in the organization which would allow an attacker to eavesdrop when any transactions are being made. the attack is conducted in cycles. Although this is a possible attack. 6. It has been proved that these two are the most had cause disastrous effect on RSA cryptosystem. the most widely used protect against this attack is exponent randomization. According to (Zhang . the public modulus n would be generated by the administration and users would use the same public modulus with others. 2008).B131590 2012 6. the numbers of cycles are counted are used to decrypt the ciphertext.4 Common Modulus This is a situation when in an organization. he suggested that the use of 216 . One of the solutions to this attack is to avoid sharing the modulus and use the public-key cryptography which includes the exchanging of secret keys for symmetric cryptosystems and the use of digital signatures.

Amal Umi Hafizah Md Yusoff 11 . which is a public key. the formula to calculate the encryption and decryption using the power and modulus method are as below: c1 = power(m. It is a padding method that is used to increase the security of RSA algorithm that produced a scheme called RSA-OAEP.0 Implementation Four sets of implementation will be carried out in this project to compare the efficiency of the four different methods used in doing the encryption and decryption process. Johnson and Stephen M. the Euler Euclid and extended Euclid algorithm are implemented respectively. It was designed based on a trapdoor permutation (RSA) and taking the counter measuring of chosen plaintext attack (IND-CPA) into account when designing this algorithm. Using the time package in C++. When combined with RSA. Although the iprecision library is utilized in this phase. Upon testing.1 Initial Implementation The first implementation is a very simple and basic program for RSA algorithm. It is designed to test if the algorithm could be put into coding. To determine the e and d value. Matyas. the first one when it could change a deterministic encryption scheme such as RSA into a probabilistic scheme with an addition of an element of randomness and the second one is it could prevent any partial decryption of ciphertexts by making certain that an attacker would not be able to recover any part of the original text without being able to invert the trapdoor one-way permutation f. check_prime() is implemented to verify the prime numbers that were received from user via console input. small prime integers were used for working functionality. it is proven to be able to resist the chosen ciphertext attack.e). The function can only work with small integers. For that reason.0 Using OAEP to Enhance RSA Security OAEP is short for Optimal Asymmetric Encryption Padding which was invented by Mihir Bellare and Phillip Rogaway with enhancement by Don B. it was ascertained that OAEP satisfies two main aims. 8. However. 8. the difftime will determine the results in this observation which will be shown in the Results and Testing section later on in the documentation.B131590 2012 7.

such as an unnecessary long calculating time due to inefficient method of power(). return c. in the test. n>1. however. if( (int) ( i % (int_precision)2 ) == 0 ) return 1. int mod. int_precision b) { int_precision c=a. The use of the power() function as can be seen beneath. for(count=3. and the result returns that they are both prime numbers. the values of p and q are tested for their prime validation. From the Figure 1 below. Two sets of tests are conducted in the example illustrated in the figures below. count+=2) divide the number by every odd number from 3 to (num-1) { //Loop to Amal Umi Hafizah Md Yusoff 12 . count * count <i && check==0.1. it can only be used by small numbers. int check = 0. int check_prime(int_precision i) { int_precision count. including the Euclid and extended Euclid algorithm.1 Experiment Settings The prime numbers to be tested in this phase will be small integers.B131590 2012 c = c1 % n. int_precision power(int_precision a. and the second sets are 79 and 97. The first sets of prime numbers are 17 and 23. for (int n=b. This method altogether has shortcomings. These formulae are very inefficient and take a long time to process both encryption and decryption. while check_prime() can only be used in small number. The function to check the prime number is as illustrated below. } The selected code for this phase of implementation is attached in the Appendix A 8. n--) c*=a. Both the functions of power() and check_prime() work perfectly. definitely causes extended time in the processing.

if(mod == 0) 1 if the number gets divided { check = 1. This statement will be verified in the next test that was carried out. //meaning its not prime } } return check. } //Statement to change the variable 'check' to Figure 1 First set of test in initial implementation In the first test is we can note that we have e as 43 and d as 131. This is a normal figure where d is expected to have a bigger value than e which caused the decryption process to be slower than encryption.B131590 2012 mod = i % count. Amal Umi Hafizah Md Yusoff 13 .

8.B131590 2012 Figure 2 Second test on the initial implementation As can be seen in the Figure 2 above. in Amal Umi Hafizah Md Yusoff 14 . These issues will be resolved in the next implementation. 8. The power() function is also very inefficient as it will take a long time to do the calculation.2 Improved Encryption and Decryption Method Implementation The improved implementation also uses the same algorithm in determining the public and private keys but an improved method of encryption and decryption is used replacing the simple power() function with the use of the simple multiply and modulus technique. 57 seconds is considered very slow and this needs to be improved. For a small number. the value of e is 43 and d is 1219 which results in the 0 second encryption and 57 seconds decryption process.1.2 Issues Arising During Initial Implementation The use of a simple power and modulus formula is very inefficient even when used with small numbers.

e1 = ex_1. The second and third experiment is to input two prime numbers with three digits which are 281 and 173. int_precision m_1. e1.B131590 2012 order to know the time taken for encryption to take place. with values of e. //This is the simple multiply and modulus formula . x. m and d as 9897. The simple_multiply_modulus() function is defined as illustrated below: int_precision simple_multiply_modulus(int_precision ex_1. Amal Umi Hafizah Md Yusoff 15 . x = m_1. for example 103 has three digits. int_precision n_1) { int_precision c. and for third values are 433 and 547.i++) c = c * x % n. The first experiment in this phase is to input two prime numbers of two digits below 30 that are 19 and 23. taking the number of digits being the prime numbers. 100 and 146519. } Three sets of test are conducted for this implementation. There are big differences in time or the encryption and especially decryption. c = c % n. 100 and 31513. return c. n = n_1. c = 1. m and d as 8999.Improved Implementation for(int i = 0. i < e1. with values of e.

B131590 2012 Figure 3 Test 1 of Simple multiply and modulus method Figure 4 Test 2 of Simple multiply and modulus method Amal Umi Hafizah Md Yusoff 16 .

8. next implementation would try to increase the efficiency of the method.B131590 2012 Figure 5 Test 3 of Simple multiply and modulus method 8. would take more than half an hour to process the encryption and decryption.2 Issues Arising During Improved Implementation The simple multiply and modulus method has a better efficiency compared to the power method in the initial implementation as it can support a larger number.3 Further Improved Encryption and Decryption Algorithm Implementation Amal Umi Hafizah Md Yusoff 17 . it is still inefficient when being tested with bigger number than what has been tested.2. Due to that. Nevertheless. that is around a value of 600.

The int_precision value namely p. the encryption process. The codes are as shown below //calculate ciphertext .encryption ctext = power(m. x = x * x % n. Much bigger numbers are now able to be tested. producing the ciphertext will be calculated using the square and multiply algorithm which proves to be more efficient than the previous way. hence the simple algorithm. ctext = ctext % n. More to this implementation is file storage for the prime factors. using the square and multiply technique’s code is as follows: //calculate ciphertext . respectively. the project can now offer user to either choose whether to create keys from scrape or they choose the ones saved to file from the previous run of the program. private key creation and the encryption and decryption function. } return c. (e."w"). extended_euclid() and square_and_multiply() in the coding. Timers in this implementation have also been added for the time it takes to process e and d. Amal Umi Hafizah Md Yusoff 18 . "w" std::string sP. A proper algorithm is now constructed. q. // just write. sP = _int_precision_itoa(&p). keys and message will be added to the implementation. n) will first be converted to string to store the value in string form using the formula below: in_p = fopen("in_p. and the revised way. having four functions. euclid(). e1 = e1/two.encryption .B131590 2012 The first and second implementations are to make sure that the algorithm is able to be implemented into code and is able to run. (d.e). while (e1 > zero) { if (e1 % 2 == one) c = c * x % n. Moving on to a more efficient code.txt". Having this implemented. each for public key creation.using square and multiply algorithm c = 1.n).

50 and 300 digit number. it is converted back to int_precision. "%s\n". Amal Umi Hafizah Md Yusoff Figure 6 Test of 10 digits numbers on square and multiply algorithm 19 . sP. 20. The first test to be conducted in this phase is the use of large numbers of 10.B131590 2012 if (in_p == NULL) { perror("Error opening the file"). When reading the values from file. Following figures are the example conducted. exit(0).c_str()). } else { fprintf(in_p. } fclose(in_p).

The encryption exponent is again a smaller value than the decryption exponent which results in encryption in 0 second and decryption in 2 seconds.B131590 2012 Figure 7 Test for file storage From Figure 6 and Figure 7. which results in 19 digits n. The text files that are storing these values can be seen in the following pages. the tests are on 10 digits number of p and q. Figure 6 displays the test on file storage. A comparison of the plain text (message) and the decrypted text is being made to ensure the decryption is working. It extracts the values from the previous (Figure 6)’s computations and uses it as its value assigning to the corresponding values. when users entered the ‘f’ for the choice of extracting keys from file. Amal Umi Hafizah Md Yusoff 20 .

B131590 2012 Figure 8 Files that store values of p.n) respectively. in_e.d and m from Figure 6 Bearing in mind that the values that are being stored are those that were saved during Figure 6’s program run.txt and in_d. except for the message m’s value. As the user chose to extract the saved keys (Figure 7).q. (e. these values are used. 50 and 300 digit numbers where the results of increase in time of processes in each increase of number of digits.e. Users are prompted for a new message to be encrypted and decrypted.n) and (d. The next figures are the test of 20.txt each is storing the public and private keys. Amal Umi Hafizah Md Yusoff 21 .

B131590 2012 Figure 9 Test of 20 digits of square and multiply algorithm Amal Umi Hafizah Md Yusoff 22 .

B131590 2012 Figure 10 Test for 50 digits for square and multiply algorithm Amal Umi Hafizah Md Yusoff 23 .

B131590 2012 Figure 11 Test of 300 digits of square and multiply algorithm Amal Umi Hafizah Md Yusoff 24 .

Therefore in the final implementation.1 Issues Arising During Further Improved Implementation The main issue in this implementation is that the user’s input of prime numbers for p and q are not able to be verified for its prime.4 Improvised Decryption Technique in the Algorithm : CRT The decryption process in the last section is taking too long for any specified digit numbers selected. due to time constraint. Therefore an advance technique is being implemented replacing the square and multiply method for decryption with CRT method. Therefore users must make sure that the entered values for p and q are both prime number. Second issue is its decryption process is taking too long.0 on comparison of techniques. 8. However. There are three tests conducted. The tests are conducted together with the previous method for comparison purposes.B131590 2012 The tests on large numbers which maximum digit number is 300 are successful for both encryption and decryption. the prime validator for large numbers could not be implemented. Amal Umi Hafizah Md Yusoff 25 . as it has a bigger value of d to be computed. The results for timing of these various tests will be discussed in the section 9. an advance technique will be implemented to have a faster decryption algorithm 8.3. 100 and 300 digits into tests as can be seen in the figures in the following pages. having 70.

B131590 2012 Figure 12 Test 1 for CRT method with 70 digits Amal Umi Hafizah Md Yusoff 26 .

B131590 2012 Figure 13 Test 2 for CRT method with 100 digits Amal Umi Hafizah Md Yusoff 27 .

B131590 2012 Figure 14 Test 3 for CRT method with 300 digits Amal Umi Hafizah Md Yusoff 28 .

To have a valid comparison. 9. As mentioned earlier in the section 5. Amal Umi Hafizah Md Yusoff 29 . Its algorithm of processing modulo p and q instead of n works the magic of speeding up the process.7 Issues Arising During Improvision The CRT has managed to speed up the time to decrypt messages by the formula of: h = q-1 * (Mp .B131590 2012 The CRT method was able to be implemented and is perfectly functioning after being tested to the maximum digit number. ptextcrt = mq + (h * q). the numbers are smaller. the use of identical sets of values is being carried out as can be seen earlier in the sections of this paper. even though there are more to be computed. The results of the comparison will again be discussed in the section 9. Following will be the results comparison graphs from the methods compared. this project could only do a less efficient way to convert formula above into C++ code as below h1 = (qinv * (mp-mq)).p). The CRT has only managed to cut down half of the time rather than four times as has been claimed by the developers.0 Efficiency Comparison This project has been implementing several techniques and methods to compare the efficiency of the different techniques. Chinese Remainder Theorem (CRT) is an advance decryption technique that was design to speed up and improve efficiency of the decryption process. which in the observation slows down the computation.4.Mq) (mod p) P = Mq + (h * q) Due to time constraint. //call the modular() h = modular(h1.0 8.

with values of e. The second and third experiment is to input two prime numbers with three digits which are 281 and 173.1 Simple multiply and modulus method Vs Square and Multiply Method 25 21 20 15 15 Simple multiply and modulus Square and Multiply 10 5 0 0 1 0 Test 2 0 Test 3 Test 1 Figure 15 Time Taken for Encryption in Seconds Figure 15 is a graph that shows the results in second for the encryption time. e and d being 100. The first set was a comparison between simple multiply and modulus of m. The time difference in the decryption is pretty big and therefore proven that the simple and multiply method is very inefficient. 100 and 31513. For that fact. m and d as 8999. 120 and 611seconds for decryption. 15 and 21 second for encryption and 0. 43 and 175. Meanwhile Figure 16 in the next page is showing the results of decryption time. while using simple multiply and modulus have a varying results of 0. Amal Umi Hafizah Md Yusoff 30 . due to the small prime numbers being used. m and d as 9897. 100 and 146519. further comparison is nearly unbearable to perform as it took a very long time to process the decryption by simple multiply and modulus method.B131590 2012 9. with values of e. Almost all the results of using square and multiply tests shows 0 second. and for third values are 433 and 547.

2 Square and Multiply Vs CRT 3000 2780 2500 2000 Square and multiply 1217 1000 CRT 1500 500 115 0 55 Test 1 205 99 Test 2 Test 3 Figure 17 Time Taken for Decryption in Seconds Amal Umi Hafizah Md Yusoff 31 .B131590 2012 700 600 500 400 300 200 100 0 0 Test 1 0 120 0 Test 2 0 Test 3 Simple multiply and modulus Square and multiply 611 Figure 16 Time Taken for Decryption in Seconds 9.

B131590 2012 Figure 17 shows the difference of time to process between square and multiply and CRT in decryption process. The second implementation is to use the simple multiply and modulus technique which was more efficient than the first technique but is not sufficient for large number which is not good enough for RSA as it deals with very large number and may increase in the size as well. encryption is always faster than decryption because although the same method is being used. making it faster to compute than d factor. but the decryption is still taking a long time. While in the second implementation.0 Conclusion 11.284) / 3 = 2. The encryption process proved to be very fast and efficient. 11. the prime checking is already proven to be very time consuming. there is an advanced technique that Amal Umi Hafizah Md Yusoff 32 .0707 2780/1217 = 2.149 The method of CRT in this project manages to pull up the speed of decryption by 2. In every method. It also performed prime checking in that phase and worked fine with small numbers.284 (2. which proved to be very inefficient especially for large numbers. However.0909 205/99 = 2. The third implementation is using the fast exponential technique. The first test was tested for a 70 decimal digits.1 Comparison of Implementation The first implementation was using the simple power function.0909 + 2. while test 2 and test 3 are 100 and 300 respectively. The average difference between the two methods is 115/55 = 2.149 times due to the inefficient method in C++ code chosen in this project.0707 + 2. the square and multiply technique which is the current technique being used in the RSA. the encryption exponent e is smaller than decryption exponent d.

i++) { mul1 = a * 7. } time(&end). // for timer a = "11111111111111111111111111111111111111111111". using this library. time(&start). a simple test has been conducted to multiply a 10000 times of two numbers and the time taken to do so is quite long. a = a + 123456789. for (int i = 1. mul2 = a * 9. int main() { int_precision a. Amal Umi Hafizah Md Yusoff 33 .B131590 2012 has been designed specifically to reduce the time taken to process decryption. This may be due to the arbitrary arithmetic precision library package that is being used in this project itself. the decryption processing time is reduced by at least half the processing time of the square and multiply technique. This project proved that using Chinese Remainder Theorem. mul2. diff = difftime(end. The developer of the library has been working with improving the library from time to time and it was proven that more efficient way could be achieved to lessen the time taken for encryption and decryption. start). double diff. end. ans. the iprecision library. i < 10000. it seems like it still wouldn’t be efficient enough as compared to real RSA application of encryption and decryption algorithm. mul1. ans = mul1 * mul2. 11. To support this statement. time_t start.2 Problem Encountered During Implementation Despite the proven efficiency of the techniques being used in this project.

Although there have been several attempts to attack it. Amal Umi Hafizah Md Yusoff 34 . 11. The key length will be extended in the future to ensure the security is maintained and still intact. It is widely used for public key encryption for most protocol or security programs.3 RSA Conclusion RSA has been around since 1977 and has been accepted as one of the most secure algorithms in the cryptography world. } Figure 18 Test of multiplying 10000 times Figure 19 Test of multiplying 20000 times The results of multiplying 10000 times in a loop and 20000 rimes loop are 19 and 39 seconds and this proves that the library still needs to improve on the calculation time. Currently developer teams are working on perfecting the 2048bit key and possibly the 3072bit key.B131590 2012 cout<<"\n\t\t\tIt took "<<diff<<" seconds to multiply 10000000"<<endl. cout<<"ans : "<<ans. the algorithm still withstands attacks at a given measure.

A simple power and modulus system would take the longest time to decrypt while the most efficient method to encrypt is the fast exponent method better known as square and multiply method and for decryption is the CRT advanced technique. 11. Next we could consider implementing advanced techniques and revised algorithms such as the padding scheme. OAEP. OEAP could be used to prevent current attacks being carried out such as block cipher attacks. There is much more work in this area that could be done in the future. Optimal Asymmetric Encryption Padding. due to time constraints. would be an excellent start as a further project.4 Future Works This project is aimed to evaluate and implement the RSA algorithm. one might want to use a different arbitrary arithmetic precision library for comparison purposes and to evaluate the efficiency using different library packages. only a few different methods could be analyzed and compared. This method can contribute to the efficiency of the algorithm. and so too can the choice of library package of arbitrary arithmetic precision. The ability of the library would determine how fast it could encrypt and decrypt a message. Amal Umi Hafizah Md Yusoff 35 .B131590 2012 The several implementations this project have proved that the choice of algorithm has effects on the efficiency of the RSA encryption and decryption process. It would open up many more evaluation of techniques and revised algorithm. In addition. Implementing the prime validator such as the AKS (A grawal–Kayal–Saxena test) library.

. An Efficient Method for Attack RSA Scheme. E.2. B. from PGP Encryption: http://www. Retrieved September 6. R. 2012. Bronson.B131590 2012 Bibliography Aboud. (1998-2012). (2007.edu/lists/small/small. 2012. (c1999). T. Chichester : Wiley . J. Retrieved July 28. Karlton.html Freier. Cryptography and network security : principles and practices. Caldwell. (2008). from PCWorld: http://www. & Kocher. March). J. Mishmash. & Rescorla. Random Small Primes. Schneier.html Salomaa. London : Springer. Fred W.html Kirk. Recommendation for Key Management . (2009). 808-813. N. and source code in C . W. Stinson.com/irish_ronan/rsa/attacks.. Chapman & Hall/CRC. The Secure Socket Layer Protocol Version 3. C. (1996. C..wikipedia. 2012. Retrieved September 9. Stallings.. London : PWS Pub . Applied cryptography : protocols. 2012. Public-key cryptography. A first book of ANSI C. P. algorithms.html Dierks. Pacific Grove . Burr. (1994-2012).0. O. G. Pluggable Encryption Algorithm In Secure Shell(SSH) Protocol. (2006).Part 1 : General (Revised). CA : Brooks/Cole . a . Vanitha. R. M. S. NIST National Institute of Standard and Technology.pcworld. Barker. Barker. Journal of IEEE. (1963-1964).tripod. from The Prime Pages: http://primes. Bronson. 2012. Arvind. P. 587-591.. G. Iyappan. (2002). Possible Attacks on RSA. Polk. Cryptography : theory and practice. & Geetha..mishmash. from Wikipedia: http://en. S. Pearson Education. Key size. IETF. W. Atkinson. (2011). The Transport Layer Security (TLS) Protocol Version 1. Retrieved September 10. J. (c2001). (c1996). (2001). A. P.. W. 2009 Second International Conference on Emerging Trends in Engineering & Technology.com/article/132184/researcher_rsa_1024bit_encryption_not _enough. D. P. New York . Internet Engineering Task Force (IETF). W. C++ for engineers and scientists .. April). Computer Security. (2009).. Berlin . Key size. J.org/wiki/Key_size Killeen. (2001).com/fredspgp/pgp. Retrieved September 9.utm.. K. from RSA: Hacking and Cracking: http://members. Pacific Grove. & Smid. A. Researcher: RSA 1024-bit Encryption not Enough. I. E.

hvks.. (2011). Analysis of RSA based on Quantitating Key Security Strength.. X.com/Numerical/arbitrary_precision. X. X. M. 15. (2008). 2008 11th IEEE Singapore International Conference on Communication Systems. Retrieved June 17. 148-151. from Numerical Methods: http://www. Zheng. W. Procedia Engineering. Lou. 2012. (2002-2012). Arbitrary precision package. A Side-channel Attack Countermeasure Based on Segmented Modular Exponent Randomizing in RSA Cryptosystem. Wang.. Vestermark. Y.html Zhang . & Peng. b . B.. H.B131590 2012 Tan.. & Pan.

} int main() { while (1){ int_precision int_precision int_precision int_precision value.h> #include <stdio. n.h> #include <time. // p and q values . n>1. if(mod == 0) //Statement to change the variable 'check' to 1 if the number gets divided { check = 1.prime numbers c1. int check = 0.h> #include <cmath> #include "iprecision. } int check_prime(int_precision i) { int_precision count. //meaning its not prime } } return check. return c. #include "stdafx. // for the encryption and decryption a . for(count=3. for (int n=b.h> #include <math. count * count <i && check==0. // cipher text // caculated in the program n = p * q int_precision d.h" #include <iostream> #include <iomanip> #include <stdlib. c.h" using namespace std. q.B131590 2012 Appendix A Code for initial implementation // rsa_p2. int_precision power(int_precision a. if( (int) ( i % (int_precision)2 ) == 0 ) return 1. int mod. n--) c*=a. count+=2) //Loop to divide the number by every odd number from 3 to (num-1) { mod = i % count. e. int_precision b) { int_precision c=a. p.cpp : Defines the entry point for the console application.

r1 = e1.one. if (check_prime(p) || check_prime(q)) { cout<<"There is error! p or q. endEncrypt. time_t startEncrypt. cin >> q. r1. or both is/are not prime no"<<endl. endDecrypt. //generate random number for e if (b != r) a = b. e1 = rem. b. gcd_x = false. cout<<"\nPlease enter two relatively prime numbers: \nfor p: "<<endl. cin >> p. prime_x = true. diffDecrypt. quo = r1 / e1. r = raa * rbb.\n"<<endl. int_precision rbb = q . int_precision one = 1. rem = r1 % e1. //random number for e int_precision e1. // for timer bool prime_x. int_precision ra. cout<<"\n\tR : "<<r<<endl<<endl. e1 = a. cout<<"\nq: ". n = p * q. cout<<"and for q: "<<endl.B131590 2012 int_precision r. int_precision raa = p . cout<<"p: ". prime_x = false. two = 2. cout<< "Please enter 2 prime numbers for p and q values: " <<endl. // e's variables. r1 = r. cin >> q. startDecrypt. int_precision limite = r . quo. //loop to check prime numbers entered do { //Prompt users to enter 2 values to be assigned to the respective variables. while(rem != zero) { b .one.rb. do { b = rand()%limite + two. cin >> p.m. zero = 0. //loop to check gcd(e. // calculated in the program r = (p-1)(q-1) int_precision a. // for timer double diffEncrypt.r) = 1.one.rem. } else cout<<"\nBoth p and q are prime numbers. }while (prime_x == false).

} int_precision c2.v3a.v3a.r) == 1 e = a. u3a. //remainder 1. quod = s1 / s2. u2 = 0. } }while (gcd_x == false || (e < two || e > r)). v2 = 1.encryption using simple power and modulus c1 = power(m.v2. using extended euclid algorithm int_precision s1. u1. v2 = v3b.e). cout<<"\n\t\t\tIt took "<<diffEncrypt<<" seconds to encrypt the message"<<endl. //then it means that gcd(e.v3b. u2 s3 = s1 % s2. 0. time(&startEncrypt). v3a = quod * v2. u1 s2 = s3."<<n<<")"<<endl. time(&endEncrypt). s2. = u2.u3a. r1 = e1. //calculating plaintext from ciphertext. diffEncrypt = difftime(endEncrypt. s1 = s2. cout<<"\nMessage is : "<<m<<endl<<endl. //calculate d. while (s3 != 0) { u3a = quod * u2. quod = s1 / s2. //calculate ciphertext . } if (rem == zero && r1 == one) { gcd_x = true. int_precision c3. //generate m (plaintext as a random number and display m = 5 + rand()%limit. v3b = v1 . int limit = 50. startEncrypt). int s3. c = c1 % n. u3b. u2. e1 = rem. c . rem = r1 % e1. u3b = u1 . e. s1 % s2. int_precision quod.B131590 2012 quo = r1 / e1. v1 = v2. = u3b. //limit to small number for initial implementation cout<<"\nEncryption exponent : "<<e<<endl. s1 s2 s3 u1 v1 = = = = = r. cout<<"\n\t\tCiphertext : "<<c<<endl<<endl. v1. cout<<"\n\tPublic Encryption Key : ("<<e<<".

c2 = power(c. diffDecrypt = difftime(endDecrypt. } else { d= v2 % r. } //for continuous use of the program char cont. time(&endDecrypt). cin>>cont. c3 = c2 % n. } return 0."<<n<<")"<<endl. } d . time(&endDecrypt). c2 = power(c. if (cont == 'x' || cont == 'X') { break. cout<<"\n\t\tPlain text is: "<<c3<<endl. time(&startDecrypt). cout<<"\nPlease enter any key to continue. cout<<"\n\tPrivate Decryption Key : ("<<d<<". c3 = c2 % n. startDecrypt). cout<<"\n\t\t\tIt took "<<diffDecrypt<<" seconds to decrypt the message"<<endl. } cout<<"\n\n". cout<<"\n\nDecryption exponent: "<<d<<endl. diffDecrypt = difftime(endDecrypt. d). otherwise enter x to exit\n"<<endl. d). cout<<"\n\nDecryption exponent: "<<d<<endl. time(&startDecrypt). cout<<"\n\t\tPlain text is: "<<c3<<endl.decryption using simple power and modulus //d = v mod r if (v2 < zero) { d = r + v2. cout<<"\n\t\t\tIt took "<<diffDecrypt<<" seconds to decrypt the message"<<endl."<<n<<")"<<endl. cout<<"\n\tPrivate Decryption Key : ("<<d<<".B131590 2012 //calculate plaintexttext . startDecrypt).

h> <math. e1. quo. quo = r1 / e1. two = 2. rem. e1 = rem. rem = r1 % e1. r1. rem = r1 % e1. ctext.h> <time. zero = 0.h> <stdio.B131590 2012 APPENDIX B Code for improved encryption and decryption implementation // rsa_9. int_precision one = 1. int_precision euclid(int_precision rnd_e. bool prime_x. int_precision r_1) { int_precision p_e. e1 = rem.r) != 1. gcd(e."<<endl.cpp : #include #include #include #include #include #include #include #include #include #include #include "stdafx. r1 = e1. a . p_e = rnd_e. // // EDITED FROM : // rsa_7. e1 = rnd_e.cpp : Defines the entry point for the console application. while(rem != zero) { quo = r1 / e1. } if (rem == zero && r1 == one) { gcd_x = true. } else { //cout<<"There is an error. r1 = r_1.h" <iostream> <fstream> <string> <sstream> <stdlib. gcd_x.h" using namespace std. r1 = e1.h> <cmath> "iprecision. n.

v3a. c = 1. 0. u2 = 0. quod = s1 / s2. b . x. } return p_e.v3b. //remainder 1. int_precision m_1. } else { p_d = v2 % r_2. //calculate d. = u3b. int_precision quod. v1. u2 s3 = s1 % s2. //d = v mod r if (v2 < zero) { p_d = r_2 + v2. v3b = v1 .u3a.v2. u3a.B131590 2012 gcd_x = false. u2. v3a = quod * v2. while (s3 != 0) { u3a = quod * u2. quod = s1 / s2. x = m_1. int_precision n_1) { int_precision c. v2 = 1. } int_precision simple_multiply_modulus(int_precision ex_1. int s3. //This is the simple multiply and modulus formula . i < e1. e_2. u1 s2 = s3. e1 = ex_1. } return p_d. c = c % n. u3b. s1 % s2. n = n_1. using extended euclid algorithm int_precision s1.Revised Implementation for(int i = 0. s1 s2 s3 u1 v1 = = = = = r_2.i++) c = c * x % n. int_precision e_2) { int_precision p_d. e1. } int_precision extended_euclid(int_precision r_2.v3a. = u2. } u3b = u1 . v2 = v3b. u1. s1 = s2. //calculating plaintext from ciphertext. v1 = v2. s2.

one. char method. int ra. // cipher text int_precision e. while (1) { cout<<"\n\n\t\tFor Comparision Version"<<endl. // for the encryption and decryption value.B131590 2012 return c. //This is square and multiply method. cout<<"e: "<<endl. e1 = e1/two. b. } return c. int_precision n_1) { int_precision c. while (e1 > zero) { if (e1 % 2 == one) c = c * x % n. m. int_precision rbb = q .rem. r = raa * rbb.diffDecrypt. quo. } int main() { int_precision p.rb. // for timer double diffEncrypt. e1 = ex_1. c = 1. cin>>q. int_precision raa = p . cout<<"q: "<<endl. int_precision m_1. // p and q values . d. n = p * q. // calculated in the program r = (p-1)(q-1) **********m file int_precision a. time_t startEncrypt. x. // for timer char cont. cout<<"p: "<<endl.prime numbers **************** randomly generated and check if its prime. x = x * x % n. q. // e's variables.endEncrypt. e1.one. cout<<"\n\tR : "<<r<<endl<<endl. **********************key from file option int_precision r. r1. cin>>p. endDecrypt. library for checking prime numbers int_precision ptext. } int_precision square_and_multiply(int_precision ex_1. srand(time (NULL)). c . //random number for e int e1. x = m_1. n = n_1. startDecrypt. Improved Implementation.

B131590 2012
cin>>e; cout<<"m: "<<endl; cin>>m; //encryption - call the function power(); cout<<"\n\t\tEncryption Public Key : ("<<e<<","<<n<<")"<<endl; cout<<"\n\nEnter A or a for method 1 or Enter B for Method 2"<<endl; cin>>method; if (method == 'A' || method == 'a') { time(&startEncrypt); ctext = simple_multiply_modulus(e, m, n); time(&endEncrypt); } else if (method == 'B' || method == 'b') { time(&startEncrypt); ctext = square_and_multiply(e, m, n); time(&endEncrypt); } else { cout<<"Enter either A or B"<<endl; exit(0); } cout<<"\n\tEncrypted ciphertext : "<<ctext<<endl<<endl; diffEncrypt = difftime(endEncrypt, startEncrypt); cout<<"\n\t\t\tIt took "<<diffEncrypt<<" seconds to encrypt the message"<<endl; cout<<"d: "<<endl; cin>>d; cout<<"\n\n\t\tDecryption Private Key : ("<<d<<","<<n<<")"<<endl; //decryption - call the function power(); if (method == 'A' || method == 'a') { time(&startDecrypt); ptext = simple_multiply_modulus(d, ctext, n); time(&endDecrypt); } else if (method == 'B' || method == 'b') { time(&startDecrypt); ptext = square_and_multiply(d, ctext, n); time(&endDecrypt); } else { cout<<"Enter either A or B"<<endl; exit(0); }

d

B131590 2012
cout<<"\n\tDecrypted plain text : "<<ptext<<endl<<endl;

diffDecrypt = difftime(endDecrypt, startDecrypt); cout<<"\n\t\t\tIt took "<<diffDecrypt<<" seconds to decrypt the message"<<endl; cout<<"\n\nTo continue press any key or press x to terminate the program..\n\n"<<endl; cin>>cont; //cout<<cont; if (cont == 'x' || cont == 'X') { cout<<"\n\n\t\tThank you for using the program.."<<endl<<endl; cout<<"\n\n***************** Terminating the program! ****************"<<endl<<endl; break; } } }

e

B131590 2012

APPENDIX C
Code for further improved encryption and decryption implementation
// rsa_7.cpp : Defines the entry point for the console application. #include #include #include #include #include #include #include #include #include #include #include "stdafx.h" <iostream> <fstream> <string> <sstream> <stdlib.h> <stdio.h> <math.h> <time.h> <cmath> "iprecision.h" //arbitrary arithmetic precision library

using namespace std; int_precision one = 1, zero = 0, two = 2, n, ctext; bool prime_x, gcd_x;

int_precision euclid(int_precision rnd_e, int_precision r_1) { int_precision p_e, e1, r1, quo, rem; e1 = rnd_e; r1 = r_1; quo = r1 / e1; rem = r1 % e1; r1 = e1; e1 = rem; while(rem != zero) { quo = r1 / e1; rem = r1 % e1; r1 = e1; e1 = rem; } if (rem == zero && r1 == one) { gcd_x = true; p_e = rnd_e; } else { //cout<<"There is an error, gcd(e,r) != 1."<<endl; gcd_x = false;

a

e1 = e1/two. } int_precision square_and_multiply(int_precision ex_1. c = 1. u2 = 0. //calculating plaintext from ciphertext. = u2. using extended euclid algorithm int_precision s1. s1 % s2. x = x * x % n.B131590 2012 } return p_e. } int_precision extended_euclid(int_precision r_2. //d = v mod r if (v2 < zero) { p_d = r_2 + v2.u3a. } else { p_d = v2 % r_2. 0. v3a = quod * v2. } u3b = u1 . u2. quod = s1 / s2. v2 = 1. while (s3 != 0) { u3a = quod * u2. while (e1 > zero) { if (e1 % 2 == one) c = c * x % n. u2 s3 = s1 % s2. int_precision quod.v3a. e_2. u3b. v1 = v2. quod = s1 / s2. u1. v1. e1 = ex_1. e1. s1 s2 s3 u1 v1 = = = = = r_2. //remainder 1. s1 = s2. x = m_1. //calculate d.v3b. v3b = v1 .v3a. int_precision m_1. = u3b. v2 = v3b. s2. } return p_d. u3a. int_precision e_2) { int_precision p_d. n = n_1.v2. int s3. int_precision n_1) { int_precision c. u1 s2 = s3. x. b .

time_t startE. **********************key from file option int_precision r.one. cout<<"\t\t\t\tOR\n". cout<<" Enter 'c' for creating own key\n". q. r = raa * rbb. *in_p. int_precision limite = r . *in_e. diffEncrypt. cin >> q. int ra. cin >> p. startEncrypt. bool opt. char cont. startDecrypt. cout<<"\n\tR : "<<r<<endl<<endl. endD. //random number for e int e1. startD. diffDecrypt. } int main() { int_precision p. // p and q values . int_precision raa = p . *in_q. // e's variables.one. // for timer double diffE. // cipher text //int_precision n. n = p * q. if(key_opt == 'c') //Create key from scratch { cout<<"\n\nYou have chosen to create your own keys. library for checking prime numbers int_precision ptext. cin>>key_opt. diffD. *in_msg. endDecrypt. cout<<"\nand for q: "<<endl. // calculated in the program r = (p-1)(q-1) **********m file int_precision a.rem. char key_opt. int_precision rbb = q . *in_d.one. b. // for timer FILE *input. d. cout<<" Enter 'f' for choosing existing key from file"<<endl. m. // for the encryption and decryption value. c . quo.B131590 2012 } return c.prime numbers **************** randomly generated and check if its prime. // caculated in the program n = p * q int_precision e. //61 while (1) { do { cout<<"\n\n\t\t\tUser's option:\n". endEncrypt.\n"<<endl. endE. srand(time (NULL)). opt = true. nada. cout<<"***************** Welcome to the program! *****************"<<endl. r1. cout<<"\nPlease enter two relatively prime numbers: \nfor p: "<<endl.rb.

char* spc = (char*)malloc( sizeof( char ) *(sp. int_precision qc(sqc). //a loop to obtain a valid encryption value do { b = rand()%limite + two. int_precision pc(spc). sq. //record the time to generate encryption exponent time(&startE).length() +1) ). }while (gcd_x == false || (e < two || e > r)).length() +1) ).sq). strcpy( sqc.is_open()) { getline(in_p. se. e = euclid(a. //generate random number for e if (b != r) a = b.close(). euclid(). cout<<"q: "<<q<<endl. opt = true. } else d .} //reading q from file as string and convert to char and int_precision ifstream in_q ("in_q.txt"). //reading p from file as string and convert to char and int_precision ifstream in_p ("in_p. } else {cout<<"Unable to open file"<<endl.c_str() ).txt"). strcpy( spc. std::string sp. sp. } else if (key_opt == 'f') //Extract existing values from file { cout<<"\n\nYou have chosen to extract existing key from file. cout<<"\n\t\t\tIt took "<<diffE<<" seconds to produce an e"<<endl. q = qc. startE). sq.B131590 2012 //Call public key creation function.c_str() ). char* sqc = (char*)malloc( sizeof( char ) *(sq.sp).is_open()) { getline(in_q. p = pc. in_p. time(&endE). diffE = difftime(endE. if(in_p. r).close(). if(in_q. in_q. cout<<"Please follow the next instructions\n"<<endl. \n". sn. cout<<"p: "<<p<<endl.

num2. cout<<"Please enter either 'c' or 'f'"<<endl. int_precision ec(sec).eof()) { indata >> num. //to temporarily store string num into array before converting to string char num1 = arrRead[0]. } indata >> num.one.close().c_str() ). num1. num1.length() +1) ). num2. arrRead[0] = num. must be between 2 e .length() +1) ).one.c_str() ). arrRead[1] = num.open("in_e.one. if(!indata) { cerr <<"Error opening the file"<<endl. to n-1 //limit for message. } else //when user input other than c or f { opt = false. } }while (opt == false).txt").B131590 2012 {cout<<"Unable to open file"<<endl. n = nc. int_precision raa = p . int_precision rbb = q . int_precision limit = n-one. while (!indata. char* sec = (char*)malloc( sizeof( char ) *(num1. std::string arrRead[2] = {}. exit(1). r = raa * rbb.} //reading e and n from file as string and convert to char and int_precision ifstream indata. num2 = arrRead[1]. char* snc = (char*)malloc( sizeof( char ) *(num2. } indata. cout<<"\tR : "<<r<<endl<<endl. indata. int_precision limite = r . std::string num. int_precision nc(snc). e = ec. strcpy( sec. strcpy( snc.

to make sure that decryption works (when m == ptext) if ( m == ptext ) cout<<"\n\nBoth decrypted text and original plain text match. diffD = difftime(endD. ptext = square_and_multiply(d.call the function square_and_multiply(). //**********************************************FILE STORAGE BEGINS************************************************************// f . //start timer to record the tme taken to decrypt time(&startDecrypt). //decryption . cout<<"\n\t\t\tIt took "<<diffEncrypt<<" seconds to encrypt the message"<<endl. time(&endD). //Call private key creation function extended_euclid().B131590 2012 do { cout<<"\n\n\tPlease enter message(m) to be encrypted (2 < m < "<<n-1<<") : "<<endl. ctext. }while (m < two || m > limit). //encryption . e). cin>>m. diffDecrypt = difftime(endDecrypt. cout<<"\n\t\t\tIt took "<<diffDecrypt<<" seconds to decrypt the message"<<endl.call the function square_and_multiply()."<<n<<")"<<endl. time(&endDecrypt). //start timer to record the tme taken to encrypt time(&startEncrypt). cout<<"\n\tEncrypted ciphertext : "<<ctext<<endl<<endl. startEncrypt)."<<n<<")"<<endl. n). cout<<"\n\t\t\tIt took "<<diffD<<" seconds to produce a d"<<endl. //start a timer to record how long it takes to obtain d time(&startD). time(&endEncrypt). cout<<"\n\tDecrypted plain text : "<<ptext<<endl<<endl. startD). //for verifying. diffEncrypt = difftime(endEncrypt. cout<<"\n\n\t\tDecryption Private Key : ("<<d<<". m. startDecrypt). ctext = square_and_multiply(e. d = extended_euclid(r. n). else cout<<"\n\nOpps! Seems the decryption is not working properly =( \n\n"<<endl. cout<<"\n\t\tEncryption Public Key : ("<<e<<". \nThe encryption and decryption are successful!!!\n\n"<<endl.

} fclose(in_p). sN}. } else { fprintf(in_p. sP. g . std::string arrFile[2] = {sE. } } rewind(in_e). } else { fprintf(in_q. //to store q value (key) to a file to allow user to choose from existing keys in_q = fopen("in_q. if (in_p == NULL) { perror("Error opening the file"). "w" std::string sQ. exit(0).txt". sE = _int_precision_itoa(&e)."w"). // just write."w"). exit(0).txt". if (in_e == NULL) { perror("Error opening the file"). "w" std::string sP. "%s\n".c_str()). g++) { fprintf(in_e. } else { for (int g = 0. g < 2. } fclose(in_q). "%s\n". sQ."w"). // just write. "%s\n".txt". sN. sP = _int_precision_itoa(&p). // just write. exit(0). //to store e value (key) to a file to allow user to choose from existing keys in_e = fopen("in_e. sQ = _int_precision_itoa(&q). arrFile[g].c_str()).B131590 2012 //to store p value (key) to a file to allow user to choose from existing keys in_p = fopen("in_p. "w" std::string sE. sN = _int_precision_itoa(&n). fclose(in_e). if (in_q == NULL) { perror("Error opening the file").c_str()).

txt".\n\n"<<endl. std::string sM."w"). "%s\n". in_msg = fopen("in_msg. exit(0). exit(0).B131590 2012 //to store d value (key) to a file to allow user to choose from existing keys in_d = fopen("in_d. sM = _int_precision_itoa(&m). h++) { fprintf(in_d. } else { for (int h = 0. //just one msg per file. break. cin>>cont. } } } h . sN}. std::string arrFile1[2] = {sD.c_str()). sD = _int_precision_itoa(&d).."<<endl<<endl. h < 2. } else { fprintf(in_msg. } fclose(in_msg). arrFile1[h]. } } rewind(in_d)."w"). //ask user if they still want to use the program cout<<"\n\nTo continue press any key or press x to terminate the program. "%s\n".. sD = _int_precision_itoa(&d). cout<<"\n\n***************** Terminating the program! ****************"<<endl<<endl. sM. // just write. if (in_msg == NULL) { perror("Error opening the file"). if (cont == 'x' || cont == 'X') { cout<<"\n\n\t\tThank you for using the program.c_str()). "w" std::string sD. if (in_d == NULL) { perror("Error opening the file"). fclose(in_d).txt".

h" //arbitrary arithmetic precision library using namespace std. quo. // Implementing CRT // // EDITED FROM : // rsa_7. int_precision one = 1.h> <stdio. rem.h> <time. gcd_x. int_precision b) { return (a % b + b) % b.h> <math.B131590 2012 APPENDIX D Code for Improvised Decryption Technique Implementation // rsa_10.cpp #include #include #include #include #include #include #include #include #include #include #include "stdafx.h> <cmath> "iprecision. quo = r1 / e1. e1. r1 = e1. e1 = rnd_e. rem = r1 % e1. while(rem != zero) { quo = r1 / e1. int_precision r_1) { int_precision p_e. e1 = rem.cpp : Defines the entry point for the console application.h" <iostream> <fstream> <string> <sstream> <stdlib. } int_precision euclid(int_precision rnd_e. bool prime_x. r1 = e1. zero = 0. e1 = rem. } if (rem == zero && r1 == one) { a . r1. int_precision modular(int_precision a. two = 2. rem = r1 % e1. r1 = r_1.

int s3. } u3b = u1 . //d = v mod r if (v2 < zero) { p_d = a_2 + v2. u1. s1 s2 s3 u1 v1 = = = = = a_2. pinv. v3a = quod * v2. //e or q s1 % s2. v1 = v2.v3a. 0. e1."<<endl. u2 = 0. u2. //r or p b_2. x. } int_precision square_and_multiply(int_precision ex_1.B131590 2012 gcd_x = true.v3b. int_precision quod. u3a. e1 = ex_1. int_precision m_1. } int_precision extended_euclid(int_precision a_2. quod = s1 / s2.v2. } else { p_d = v2 % a_2. } return p_e. int_precision n_1) { int_precision c. } return p_d. no. v3b = v1 . int_precision b_2) { int_precision p_d. gcd_x = false. no = n_1. //remainder 1. u2 s3 = s1 % s2. v1. = u3b. u1 s2 = s3. //calculate d. p_e = rnd_e.r) != 1. x = m_1. //calculating plaintext from ciphertext. while (s3 != 0) { u3a = quod * u2. u3b. gcd(e. = u2. s1 = s2. quod = s1 / s2. v2 = 1.v3a. } else { //cout<<"There is an error. v2 = v3b. using extended euclid algorithm int_precision s1.u3a. b . c = 1. s2. qinv.

startD. startEncrypt. } int main() { int_precision p. pinv.rb. char cont.rem. opt = true. e1 = e1/two. // for timer FILE *input. time_t startE. mp. int ra. // e's variables. *in_e. cout<<"\nPlease enter two relatively prime numbers: \nfor p: "<<endl. bool opt. cout<<"\t\t\t\tOR\n". endEncrypt. if(key_opt == 'c') //Create key from scratch { cout<<"\n\nYou have chosen to create your own keys. q. endDecrypt. double diffCrt. *in_msg. quo. r1. //crt's variables int_precision dp.B131590 2012 while (e1 > zero) { if (e1 % 2 == one) c = c * x % no. // for the encryption and decryption value. d. *in_p. qinv. char key_opt. dq. *in_d. // p and q values . while (1) { do { cout<<"\n\n\t\t\tUser's option:\n". cin >> p. diffDecrypt. cin >> q. // for timer double diffE. //random number for e int e1. diffD. m. // calculated in the program r = (p-1)(q-1) int_precision a.prime numbers int_precision ptext. int_precision r. mq. endCrt. cout<<"\nand for q: "<<endl. } return c. // cipher text int_precision n. diffEncrypt. // caculated in the program n = p * q int_precision e. b. cout<<" Enter 'f' for choosing existing key from file"<<endl. cout<<"***************** Welcome to the program! *****************"<<endl. c . x = x * x % no.\n"<<endl. cin>>key_opt. nada. ctext. endD. srand(time (NULL)). startDecrypt. endE. cout<<" Enter 'c' for creating own key\n". *in_q. time_t startCrt.

cout<<"\n\t\t\tIt took "<<diffE<<" seconds to produce an e"<<endl. if (b != r) a = b.c_str() ). se.is_open()) { getline(in_p. //generate random number for e cout<<"\nRandomly generated 'e': "<<b<<endl. sn. //reading p from file as string and convert to char and int_precision ifstream in_p ("in_p. if(in_p. strcpy( spc. diffE = difftime(endE.is_open()) d .one. \n".one. r = raa * rbb. cout<<"p: "<<p<<endl.B131590 2012 n = p * q. e = euclid(a.one.txt"). time(&endE). startE).close(). std::string sp.} //reading q from file as string and convert to char and int_precision ifstream in_q ("in_q.length() +1) ).sp). int_precision pc(spc). time(&startE). sq. opt = true. } else {cout<<"Unable to open file"<<endl. //a loop to obtain a valid encryption value do { b = rand()%limite + two. euclid(). }while (gcd_x == false || (e < two || e > r)). in_p. cout<<"Please follow the next instructions\n"<<endl. p = pc. char* spc = (char*)malloc( sizeof( char ) *(sp. } else if (key_opt == 'f') //Extract existing values from file { cout<<"\n\nYou have chosen to extract existing key from file. if(in_q.txt"). int_precision raa = p . sp. r). int_precision rbb = q . int_precision limite = r . //Call public key creation function. cout<<"\n\tR : "<<r<<endl<<endl.

arrRead[0] = num. strcpy( sec. sq. cout<<"\tR : "<<r<<endl<<endl. } indata >> num. num1. int_precision rbb = q . exit(1). e = ec.sq).c_str() ).close(). int_precision raa = p . num2.one.one. int_precision limite = r . //to temporarily store string num into array before converting to string char num1 = arrRead[0]. arrRead[1] = num. char* sec = (char*)malloc( sizeof( char ) *(num1. } indata. num2. num1.B131590 2012 { getline(in_q.one. n = nc. if(!indata) { cerr <<"Error opening the file"<<endl. strcpy( snc. cout<<"q: "<<q<<endl. char* sqc = (char*)malloc( sizeof( char ) *(sq. e . int_precision ec(sec).open("in_e.} //reading e and n from file as string and convert to char and int_precision ifstream indata. } else {cout<<"Unable to open file"<<endl. char* snc = (char*)malloc( sizeof( char ) *(num2.c_str() ).close(). num2 = arrRead[1]. while (!indata.txt"). std::string num.eof()) { indata >> num. indata. in_q.length() +1) ).c_str() ). int_precision nc(snc). std::string arrRead[2] = {}.length() +1) ). int_precision qc(sqc). strcpy( sqc.length() +1) ). q = qc. r = raa * rbb.

time(&endEncrypt). //start timer to record the tme taken to encrypt time(&startEncrypt).call the function square_and_multiply(). }while (m < two || m > limit). //decryption . n). diffD = difftime(endD. cout<<"\n\tEncrypted ciphertext : "<<ctext<<endl<<endl. diffDecrypt = difftime(endDecrypt. } }while (opt == false). int_precision limit = n-one. d = extended_euclid(r. to make sure that decryption works (when m == ptext) f . cout<<"\n\n\t\tDecryption Private Key : ("<<d<<". startEncrypt). cout<<"\n\t\tEncryption Public Key : ("<<e<<". startDecrypt). to n-1 do { cout<<"\n\n\tPlease enter message(m) to be encrypted (2 < m < "<<n-1<<") : "<<endl. ctext. cout<<"\n\tDecrypted plain text : "<<ptext<<endl<<endl. ptext = square_and_multiply(d. time(&endD). ctext = square_and_multiply(e.B131590 2012 } else //when user input other than c or f { opt = false. cin>>m."<<n<<")"<<endl. //start timer to record the tme taken to decrypt time(&startDecrypt)."<<n<<")"<<endl. diffEncrypt = difftime(endEncrypt. must be between 2 //Call private key creation function extended_euclid(). //start a timer to record how long it takes to obtain d time(&startD). cout<<"Please enter either 'c' or 'f'"<<endl. e). //for verifying. startD). //encryption . time(&endDecrypt). //limit for message. cout<<"\n\t\t\tIt took "<<diffEncrypt<<" seconds to encrypt the message"<<endl. cout<<"\n\t\t\tIt took "<<diffDecrypt<<" seconds to decrypt the message"<<endl. cout<<"\n\t\t\tIt took "<<diffD<<" seconds to produce a d"<<endl. n). m.call the function square_and_multiply().

dq = d % (q-1). //for troubleshooting. //call the square_and_multiply() for power and modulus operation //mp = (ctext^dp)%p.ctext.q). //start timer for crt based decryption time(&startCrt). //************************************FILE STORAGE BEGINS***************************************// g .". h1. dq and qinv as part of private key taking p and q value as modular rather than n dp = d % (p-1). } else cout<<"\n\nOpps! Seems the crt decryption is not working properly =( \n\n"<<endl. mq = square_and_multiply(dq. cout<<"\nDecryption using crt worksss!!!\n\n"<<endl. //h = (qinv * (mp-mq)) % p h1 = (qinv * (mp-mq)). qinv = extended_euclid(p. diffCrt = difftime(endCrt.p). \nThe encryption and decryption are successful!!!\n\n"<<endl.B131590 2012 if ( m == ptext ) cout<<"\n\nBoth decrypted text and original plain text match.p). else cout<<"\n\nOpps! Seems the decryption is not working properly =( \n\n"<<endl. int_precision h. mp = square_and_multiply(dp. //call the modular() h = modular(h1. //compute dp. time(&endCrt).ctext. ptextcrt. ptextcrt = mq + (h * q). cout<<"qinv : "<<qinv<<endl.q). cout<<"\n\t\t\tIt took "<<diffCrt<<" seconds to decrypt the message using CRT"<<endl. startCrt). cout<<"\n\t\tDecrypted plaintext using CRT : "<<ptextcrt<<endl. to make sure that CRT decryption works (when ptext == ptextcrt) if (ptext == ptextcrt) { cout<<"\n\nBoth decrypted using sq n mul and crt text match. //***************************CRT implementation*************************************** cout<<"\n\t\tDecryption using CRT"<<endl.

sP. sP = _int_precision_itoa(&p). if (in_q == NULL) { perror("Error opening the file"). sN}. } else { fprintf(in_q. } } rewind(in_e). h . "%s\n". g++) { fprintf(in_e. g < 2. } fclose(in_p).txt". "w" //converts p to store it into file in string format std::string sP. if (in_e == NULL) { perror("Error opening the file"). //to store e value (key) to a file to allow user to choose from existing keys in_e = fopen("in_e."w"). //to store q value (key) to a file to allow user to choose from existing keys in_q = fopen("in_q. // just write. sN = _int_precision_itoa(&n). exit(0). std::string arrFile[2] = {sE."w"). "w" std::string sE. "%s\n".txt".c_str()). } else { for (int g = 0. // just write. sQ. } else { fprintf(in_p. exit(0). sE = _int_precision_itoa(&e). if (in_p == NULL) { perror("Error opening the file").B131590 2012 //to store p value (key) to a file to allow user to choose from existing keys in_p = fopen("in_p. arrFile[g].txt"."w"). exit(0). "w" std::string sQ. // just write. fclose(in_e). sQ = _int_precision_itoa(&q). sN. "%s\n".c_str()). } fclose(in_q).c_str()).

"%s\n". // just write. cin>>cont. sN}. } } rewind(in_d). "%s\n". arrFile1[h]. sD = _int_precision_itoa(&d). //ask user if they still want to use the program if (cont == 'x' || cont == 'X') { cout<<"\n\n\t\tThank you for using the program.c_str()). if (in_msg == NULL) { perror("Error opening the file"). } else { for (int h = 0."w"). sM. } else { fprintf(in_msg. sD = _int_precision_itoa(&d). break. exit(0). h++) { fprintf(in_d.txt". h < 2. } } } i . in_msg = fopen("in_msg. exit(0). std::string arrFile1[2] = {sD.c_str()).."w"). std::string sM. cout<<"\n\nTo continue press any key or press x to terminate the program.. } fclose(in_msg)."<<endl<<endl.B131590 2012 //to store d value (key) to a file to allow user to choose from existing keys in_d = fopen("in_d. if (in_d == NULL) { perror("Error opening the file"). "w" std::string sD.\n\n"<<endl. cout<<"\n\n***************** Terminating the program! ****************"<<endl<<endl. //just one msg per file. fclose(in_d). sM = _int_precision_itoa(&m).txt".

B131590 2012 j .

the totient function Compute e value. r = (p-1)(q-1).    Open files for the values of p. d.   Ii  The new value of e will be generated until the check conditions are satisfied. Press ‘f’ to choose existing key from file. Display the value of e and the time it took to process it. e. e. Read the values from respective files and display on the command window. iv Prompt user to enter message to be encrypted. EA timer startE will be set just before generating a random number for e using rand() function. n = p*q . and n that has been stored from previous use of the program. to determine how long it takes to generate and validate the encryption exponent value.  The message will be checked if it is within range that must be more than 2 and less than n-1. User will be prompted to re-enter the value of message if the message entered earlier is not within the range set earlier until the condition is a . If choice = f.B131590 2012 APPENDIX E Flow of Algorithm for Improvised Decryption Technique in Algorithm Let user know the option of key creation : i Press ‘c’ to create own key or  If choice = c. Display the r value. Calculate the value of r. which will be done using the auclid() function taking the value of e and r as the parameters. r = (p-1)(q-1) Display r. q.r) = 1 which will be verified by the Euclid algorithm. The limit of e will be between 2 and r-1 and must meet the condition of gcd(e.     Ask user to input two relatively prime numbers for p and q values Calculate n and r. Iii Press either ‘c’ or ‘f’  If choice != ‘f’ or ‘c’  Display error message if user input other characters other than ‘c’ or ‘f’.

d. display a message of decryption is unsuccessful ix.  Return d and time taken viii Perform decryption. display a message of decryption is a success. square_and_multiply () having d. Call the extended_euclid() function to compute qinv  Calculate decryption using crt technique: o Start timer for CRT-based decryption o Calculate the mp and mq by calling the square_and_multiply() function o Calculate h by calling the modular() function. q = d % (q-1). m and n as parameters. display a message of decryption is a success. D is calculated using the extended Euclid algorithm. Perform CRT’s calculation.B131590 2012 met.  Assign message to the variable m. using square_and_multiply() function  Compute dp. n) Perform encryption. using extended_euclid() function having r and e as parameters. o End the timer o Display the ptextcrt and the time it takes to decrypt using CRT technique x. v vi Display the public key as (e. Compare the ptext with the CRT-based decrypted ptextcrt  If both are equal. ix.  Set timer  Assigned the result to ptext variable and display to user with time taken for decrypting the ciphertext.  Else. calling the function square_and_multiply() having e. also using the same function of encryption. ctext and n as parameters. b .  Another timer will be set to record the time it takes to process the encryption. Compare the original message m with the decrypted ptext  If both are equal. The encryption will be done using the square and multiply method which results will be assigned to ctext variable and display to user alongside the time taken to encrypt the message. dq and qinv as part of private key taking p and q as modular dp = d % (p-1).  Timer is set. vii Generate decryption exponent.

txt will store the values of e and n. display a message of decryption is unsuccessful x. Store the variables of p. in_m. q and m to respective files of in_p. for public encryption keys.txt.  These values in the files will be read if the user chooses the option of extracting existing keys from files (choice = ‘f’). user will be program will welcome user once again and start program from beginning If user enters x.B131590 2012 Else. While in_e. xi.txt. terminate the program c . and in_d. in_q. Ask if the user still wants to continue to use the program.  Enter any key to continue or enter x to exit and terminate the program   If user enter any key.txt.txt will store the values of d and n which indicates private keys for decryption.

Sign up to vote on this title
UsefulNot useful