Sei sulla pagina 1di 21

“DATA ENCRYPTION AND DECRYPTION

USING RSA ALGORITHM IN C"

A report submitted in partial fulfillment of the requirements for the Award of


Degree of

BACHELOR OF TECHNOLOGY
in
COMPUTER SCIENCE AND ENGINEERING
By

APARNA SONKAR
ROLL NO-1884110018

Under Supervision of
Assist. Prof-Mr.Ashish Ranjan Mishra
(Assist.Prof of C.S.E.D. at R.E.C. Sonbhadra)

COMPUTER SCIENCE & ENGINEERING DEPARTMENT


RAJKIYA ENGINEERING COLLEGE
(An AICTE Approved Government Engineering College, Affiliated to AKTU Lucknow)
CHURK SONBHADRA, UTTAR PRADESH – 231206
Month & 2019
Department of Computer Science
and Engineering
Rajkiya Engineering College
Churk, Sonbhadra – 231206, Uttar Pradesh,
India

CERTIFICATE

This is to certify that this internship report entitled “Data Encryption and Decryption using

RSA Algorithm” submitted by Aparna Sonkar, Roll No.: 1884110018,in partial fulfilment of

the requirements for the degree of Bachelor of Technology in Computer Science & Engineering

of Rajkiya Engineering College, Sonbhadra,UP (An AICTE Approved Government Engineering

College, Affiliated to Dr. A.P.J. Abdul Kalam Technical University, Lucknow) during the

academic year 2019-20, is a bonafide record of work carried out under guidance and supervision

of Mr. Ashish Ranjan Mishra(Assistant Professor C.S.E.D. REC SONBHADRA). This is his/her

own industrial training and the report is fit for submission.

Mr. Ashish Ranjan Mishra Dr. Amod Kumar


Tiwari
(Coordinator) (Head of
Department)
Assistant Professor Associate Professor
CSED, REC Sonbhadra CSED, REC
Sonbhadra
DELCARATION

I, Anushka Chaudhary, hereby declare that the Internship Training and Summer Project Report,

entitled “Data Encryption and Decryption using RSA Algorithm", submitted to the Rajkiya

Engineering College, Sonbhadra Uttar Pradesh in partial fulfilment of the requirements for

the award of the Degree of Bachelor of Technology is a record of original training undergone by

me during the period (29/07/2019 - 7/10/2019) under the supervision and guidance of Mr. Ashish

Ranjan Mishra(Assistant professor), Department of Computer Science & Engineering, Rajkiya

Engineering College, Sonbhadra and it has not formed the basis for the award of any

Degree/Fellowship or other similar title to any candidate of any University.

Place: R.E.C. SONBHADRA Name of the student


Date:7/10/2019 ANUSHKA CHAUDHARY
ACKNOWLEDGEMENT

 Thanks to Almighty GOD Who gave us courage and


understanding to start and later on finish this work of
project”Data Encryption And Decryption using RSA
algorithm in C”.
 Thanks to R.E.C. SONBHADRA college management to
provide us all the facility that we needed.
 Special thanks to Honourable Director Prof. V.K. GIRI
Sir for creating such a wonderful environment of
learning in the campus.
 A special thanks to H.O.D. C.S.E.D. -- Dr. Amod Kumar
Tiwari Sir (Assist. Prof.) for his kind motivation.
 A thanks to Udemy.com and Coursera.org to provide
such a wonderful platform for learning Python and
Algorithms respectively.
 Special thanks to Mr. Ashish Ranjan Mishra Sir(Assit.
Prof. CSED ) and Mrs. Kalpana Singh(Assistant
professor CSED)for their guidance and supervision
throughout the project.
DEDICATIONS
 This Project work is dedicated to our parents and our
Professor Sirs,who taught us different roads of life and
directed as towards our destinations.To all those friends
and companions who helped us while doing this work and
made the journey of university carrier easily.
Table of Contents

Certificate i

Company certificate ii

Declaration iii

Acknowledgement iv

1. Project overview and Abstract 1

2. Technology Background 2
3. Modules Covered 3

4. Applications 4

5. Conclusion and Future Scope 5

6. Code and Output 6


Reference
1.Project Overview
Working of RSA Algorithm
RSA involves use of public and private key for its operation. The keys are
generated using the following steps:-
1. Two prime numbers are selected as p and q
2. n = pq which is the modulus of both the keys.
3. Calculate totient = (p-1)(q-1)
4. Choose e such that e > 1 and coprime to totient which means gcd (e,
totient) must be equal to 1, e is the public key
5. Choose d such that it satisfies the equation de = 1 + k (totient), d is the
private key not known to everyone.
6. Cipher text is calculated using the equation c = m^e mod n where m is
the message.
7. With the help of c and d we decrypt message using equation m = c^d
mod n where d is the private key.
Note: If we take the two prime numbers very large it enhances security but
requires implementation of Exponentiation by squaring algorithm and
square and multiply algorithm for effective encryption and decryption. For
simplicity the program is designed with relatively small prime numbers.

1.1 Project Objectives


 The goal of every encryption algorithm is to make it as difficult as
possible to decrypt the generated ciphertext without using the
key.
 If a really good encryption algorithm is used, there is no technique
significantly better than methodically trying every possible key.

 It is difficult to determine the quality of an encryption algorithm.


 Algorithms that look promising sometimes turn out to be very
easy to break, given the proper attack. When selecting an
encryption algorithm, it is a good idea to choose one that has
been in use for several years and has successfully resisted all
attacks.
 That’s why we use RSA algorithm which is best algorithm for
Encryption and decryption.
1.2 Abstract
THE RSA CRYPTOSYSTEM
Key generation Choose two distinct primes p and q of
approximately equal size so that their product n = pq is of the
required length. Compute φ(n) = (p-1)(q-1). Choose a public
exponent e, 1 < e < φ(n), which is coprime to φ(n), that is, gcd(e,
φ(n))=1. Compute a private exponent d that satisfies the
congruence ed ≡ 1 (mod φ(n)). Make the public key (n, e)
available to others. Keep the private values d, p, q, and φ(n)
secret.
RSA Encryption scheme
Encryption rule: ciphertext, c = RsaPublic(m) = me mod n, 1 < m <
n-1
Decryption rule: plaintext, m = RsaPrivate(c) = cd mod n
Inverse transformation: RsaPrivate(RsaPublic(m))
RSA Signature scheme
Signing: signature, s = RsaPrivate(m) = md mod n, 1 < m < n-1
Verification: check, v = RsaPublic(s) = se mod n
Inverse transformation: m = RsaPublic(RsaPrivate(m))
First, note that the inverse transformations for encryption and
signing are equivalent, since
RsaPrivate(RsaPublic(m)) = (me mod n)d mod n = med mod n
RsaPublic(RsaPrivate(m)) = (md mod n)e mod n = med mod n
So we only need to show that the decryption rule works; that is,
for (n, e) as defined above,
If c = me mod n for 0 < m < n then m = cd mod n, where d is the
secret exponent that satisfies the relation ed ≡ 1 (mod φ(n)).

All proofs of how RSA works boil down to the fact that for any
integer x, x1+kφ(n) ≡ x (mod n).

Note that the algorithm strictly works for 0 ≤ m < n, but we


exclude the values m = 0, 1 and m = n-1 from the cryptosystem
because there is no secrecy for such messages. Why?
First proof
The first proof is short. It uses the Euler-Fermat Theorem. It
works for messages m that are relatively prime to the modulus n,
that is where gcd(m, n) = 1.
Proof. Suppose gcd(m, n) = 1. The relation ed ≡ 1 (mod φ(n)) gives
that ed = 1 + kφ(n) for some integer k. If c ≡ me (mod n) then,
working modulo n,
cd ≡ med
≡ m1+kφ(n)
≡ m.(mφ(n))k
≡ m.1k, since mφ(n) ≡ 1 (mod n), by the Euler-Fermat theorem,
as gcd(m, n)=1
≡ m (mod n).
Hence m = cd mod n is a unique integer in the range 0 ≤ m < n.
Second proof
The first proof does not include the special cases where the
message m is divisible by one of the prime factors, p or q. There
are p numbers where q|m: that is, m = 0, q, 2q, 3q, ..., (p-1)q.
Similarly there are q numbers where p|m, and thus p+q-1 possible
numbers where gcd(m, n) > 1 (zero occurs twice). Note that p and
q cannot both divide m since we restrict m < n = pq.
The second proof specifically considers the case where gcd(m, n) >
1, but it could easily be written to include the first case above (if
gcd(q, n)=1 then just say "Similarly for q" after the result mod p).
It uses Fermat's Little Theorem and the fact that if s and t are
coprime and a ≡ b (mod s) and a ≡ b (mod t), then a ≡ b (mod st).
† If s and t are coprime and a ≡ b (mod s) and a ≡ b (mod t), then a
≡ b (mod st). Proof. a ≡ b (mod s) ⇒ s|a-b. a ≡ b (mod t) ⇒ t|a-b.
Since s and t are coprime then st|a-b. Hence a ≡ b (mod st).
Proof. Suppose one of the primes, say q, divides m. Then m ≡ 0
(mod q) and so, trivially, we have
0 ≡ m1+kφ(n) ≡ m (mod q).
If q divides m then p cannot also divide m since m < n = pq and so
we must have gcd(m, p) = 1. By Fermat's Little Theorem we have
mp-1 ≡ 1 (mod p)
m(p-1)(q-1) ≡ 1q-1 ≡ 1 (mod p), raising both sides to the power
q-1
mφ(n) ≡ 1, since φ(n) = φ(pq) = (p-1)(q-1)
mkφ(n) ≡ 1k ≡ 1 (mod p), raising both sides to the power k
m1+kφ(n) ≡ m (mod p), multiplying both sides by m.
Now since m1+kφ(n) ≡ m (mod q) and m1+kφ(n) ≡ m (mod p), and
p and q are coprime, then it follows that (see † above)
m1+kφ(n) ≡ m (mod pq) ≡ m (mod n), for all integers m. We have
ed ≡ 1 (mod φ(n)) ⇒ ed = 1 + kφ(n). So, if c ≡ me (mod n) then cd ≡
med ≡ m1+kφ(n) ≡ m (mod n). Hence m = cd mod n is a unique
integer in the range 0 ≤ m < n.
Longer proof of the RSA algorithm
We wrote this proof of the RSA algorithm (pdf, 93 kB) back in
2006, and that in turn is a revised version of something we wrote
in 2002. It uses very elementary principles of number theory.
Apologies to all university tutors who have received plagiarised
versions in students' assignments. The PDF document was written
in LaTex and generated on a Windows system using the wonderful
TeXniCenter. Using λ(n) instead of φ(n)
The original version of RSA defined the secret exponent as d = e-1
(mod φ(n)), where φ is the Euler totient function and φ(n) = φ(pq)
= (p-1)(q-1). In fact you can use the smaller Charmichael function
instead, so that an alternative secret exponent (we'll call it d') is
defined by d' = e-1 (mod λ(n)), where λ(n) = lcm(p-1, q1) = (p-1)(q-
1)/gcd(p-1, q-1). Later refinements of the RSA algorithm like
PKCS#1 use this definition.
But it doesn't matter which one you use. Both d and d' will give
you the same results.
Note that the proofs above use the fact that x1+kφ(n) ≡ x (mod n)
for any integer x. Well, by definition, xλ(n) ≡ 1 (mod n), so we can
just write x1+kλ(n) ≡ x (mod n), and all the proofs will still work.
Both values of d and d' will uniquely decrypt a message c
encrypted with c = me mod n, and both will give the same
signature s = md mod n = md' mod n.
In fact, any secret exponent of the form d' + rλ(n) will work, for
any integer r. Since λ(n) divides φ(n), then our d is one of these.
For practical values of n in the thousands of bits, the difference
between d and d' is a couple of bits. :
2.TECHNOLOGY BACKGROUND
Our Project “Data Encryption and Decryption using RSA Algorithm
Developed in C plateform. It is a general-purpose, procedural
computer program supporting structured programming, lexical
variable scope, Cryptography and file Handling. By design, C
provides constructs that map efficiently to typical machine
instructions, and has found lasting use in applications previously
coded in assembly language. Such applications include operating
systems, as well as various application software for computers
ranging from supercomputers to embedded systems. The basic
requirements of an application are included in this project like
Networking, database connection.
RSA (Rivest–Shamir–Adleman) is one of the first public-key
cryptosystems and is widely used for secure data transmission. In
such a cryptosystem, the encryption key is public and it is different
from the decryption key which is kept secret (private). In RSA, this
asymmetry is based on the practical difficulty of the factorization of
the product of two large prime numbers, the "factoring problem".

PLATEFORM DESCRIPTION
1.1 Introduction: C was originally developed at Bell Labs by Dennis
Ritchie between 1972 and 1973 to make utilities running on Unix.
Later, it was applied to reimplementing the kernel of the Unix
operating system. During the 1980s, C gradually gained popularity.
Nowadays, it is one of the most widely used programming languages,
with C compilers from various vendors available for the majority of
existing computer architectures and operating systems. C has been
standardized by the ANSI since 1989 (see ANSI C) and subsequently
by the International Organization for Standardization. C is an
imperative procedural language. It was designed to be compiled
using a relatively straightforward compiler, to provide low-level
access to memory, to provide language constructs that map
efficiently to machine instructions, and to require minimal runtime
support. Despite its low-level capabilities, the language was designed
to encourage cross-platform programming. A standards-compliant C
program that is written with portability in mind can be compiled for a
wide variety of computer platforms and operating systems with few
changes to its source code; the language has become available on
various platforms, from embedded microcontrollers to
supercomputers.

1.3 History:
The origin of C is closely tied to the development of the Unix
operating system, originally implemented in assembly language on a
PDP-7 by Dennis Ritchie and Ken Thompson, incorporating several
ideas from colleagues. Eventually, they decided to port the operating
system to a PDP-11. The original PDP-11 version of Unix was
developed in assembly language. Thompson needed a programming
language to make utilities. At first,

he tried to make a Fortran compiler, but soon gave up the idea and
made a new language, B, Thompson's simplified version of BCPL.
However, few utilities were written in B because B was too slow, and
B could not take advantage of PDP-11 features such as byte
addressability. In 1972, Ritchie started to improve B, which resulted
in creating a new language C. The C compiler and some utilities
made with it were included in Version 2 Unix. At Version 4 Unix
released at Nov. 1973, the Unix kernel was extensively re-
implemented by C.By this time, the C language had acquired some
powerful features such as struct types. Unix was one of the first
operating system kernels implemented in a language other than
assembly. Earlier instances include the Multics system (which was
written in PL/I) and Master Control Program (MCP) for the Burroughs
B5000 (which was written in ALGOL) in 1961. In around 1977, Ritchie
and Stephen C. Johnson made further changes to the language to
facilitate portability of the Unix operating system. Johnson's Portable
C Compiler served as the basis for several implementations of C on
new platforms.

3.MODULES COVERED
 What is Encryption and Decryption

 What is RSA.

 RSA algorithm.

 Theorems used in RSA.

 Common attacks against security over network.

 Solution for attacks against security.

 Platform description.

 History of C language.

 Scope.

 Snapshot.

References

4.APPLICATION
 Development of RSA algorithm for better security.
5.CONCLUSION AND FUTURE SCOPE

In the end, I would like to conclude that my aim to make this project was to
secure Computer from attacks.

I would like to thanks all those who have helped me and contributed in
making of this project.
6.Code:-
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
int main() {

printf("\nENTER FIRST PRIME NUMBER\n");


scanf("%d",&p);
flag=prime(p);
if(flag==0) {
printf("\nWRONG INPUT\n");
exit(1);
}
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%d",&q);
flag=prime(q);
if(flag==0||p==q) {
printf("\nWRONG INPUT\n");
exit(1);
}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%[^\n]",msg);
for (i=0;msg[i]!=NULL;i++)
m[i]=msg[i];
n=p*q;
t=(p-1)*(q-1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for (i=0;i<j-1;i++)
printf("\n%ld\t%ld",e[i],d[i]);
encrypt();
decrypt();
}
int prime(long int pr) {
int i;
j=sqrt(pr);
for (i=2;i<=j;i++) {
if(pr%i==0)
return 0;
}
return 1;
}
void ce() {
int k;
k=0;
for (i=2;i<t;i++) {
if(t%i==0)
continue;
flag=prime(i);
if(flag==1&&i!=p&&i!=q) {
e[k]=i;
flag=cd(e[k]);
if(flag>0) {
d[k]=flag;
k++;
}
if(k==99)
break;
}
}

long int cd(long int x) {


long int k=1;
while(1) {
k=k+t;
if(k%x==0)
return(k/x);
}
}

void encrypt() {
long int pt,ct,key=e[0],k,len;
i=0;
len=strlen(msg);
while(i!=len) {
pt=m[i];
pt=pt-96;
k=1;
for (j=0;j<key;j++) {
k=k*pt;
k=k%n;
}
temp[i]=k;
ct=k+96;
en[i]=ct;
i++;
}
en[i]=-1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for (i=0;en[i]!=-1;i++)
printf("%c",en[i]);
}

void decrypt() {
long int pt,ct,key=d[0],k;
i=0;
while(en[i]!=-1) {
ct=temp[i];
k=1;
for (j=0;j<key;j++) {
k=k*ct;
k=k%n;
}
pt=k+96;
m[i]=pt;
i++;
}
m[i]=-1;
printf("\nTHE DECRYPTED MESSAGE IS\n");
for (i=0;m[i]!=-1;i++)
printf("%c",m[i]);
}
OUTPUT:-
REFERNCES

rithm.html

Potrebbero piacerti anche