0% found this document useful (0 votes)
48 views

I MPL Ement at I Onofsubst I T Ut I Onci Pheri NC++

1. The document describes the implementation of a substitution cipher and Caesar cipher in C++. It includes code snippets for encrypting and decrypting text using both ciphers. 2. The substitution cipher code takes in a string, encrypts or decrypts it based on the user's choice, and outputs the result. It uses arrays to store the alphabet and substituted alphabet. 3. The Caesar cipher code also takes a message and key as input, encrypts or decrypts based on the user's choice, handling uppercase and lowercase letters separately. It outputs the encrypted/decrypted message.

Uploaded by

Dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

I MPL Ement at I Onofsubst I T Ut I Onci Pheri NC++

1. The document describes the implementation of a substitution cipher and Caesar cipher in C++. It includes code snippets for encrypting and decrypting text using both ciphers. 2. The substitution cipher code takes in a string, encrypts or decrypts it based on the user's choice, and outputs the result. It uses arrays to store the alphabet and substituted alphabet. 3. The Caesar cipher code also takes a message and key as input, encrypts or decrypts based on the user's choice, handling uppercase and lowercase letters separately. It outputs the encrypted/decrypted message.

Uploaded by

Dev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

Impl
ement
ati
onofSubst
it
uti
onCi
pheri
nC++:
-
/
/Pr
ogr
am f
orsubst
it
uti
onci
pher
#i
ncl
ude<i
ost
ream.
h>
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<coni
o.h>
#i
ncl
ude<st
ri
ng.
h>
v
oidmai
n()/
/Mai
nfunct
ion
{
charch;
/
/ar
rayt
ofi
ndposi
ti
onf
orencr
ypt
ion
charal
pha[
]="
abcdef
ghi
j
klmnopqr
stuv
wxy
z";
/
/ar
rayt
ofi
ndposi
ti
onf
ordecr
ypt
ionchar
al
pha1[
]="
XNYAHPOGZQWBTSFLRCVMUEKJDI
";
i
nti
,
j,
choi
ce,
temp[
100]
;
cl
rscr
();
do
{
chari
nput
[100]
;
cout
<<"
\nEnt
ert
het
ext
:"
;
/
/Inputst
ri
ng
get
s(i
nput
);
cout
<<"
\n1.
Encr
ypt
\n2.
Decr
ypt
\nEnt
erchoi
ce:
";
ci
n>>choi
ce
swi
tch(
choi
ce)
{
case1:
{
/
/Encr
ypt
ion
cout
<<"
\nEncr
ypt
edt
ext:
";
/
/Encr
ypt
edText
f
or(
i=0;
i
<input
[i
]!
='
\0'
;
i++)
cout
<<al
pha1[
temp[
i]
];
br
eak;
}
case2:
{
/
/Decr
ypt
ion
cout
<<"
\nDecr
ypt
edt
ext:
";
/
/Fi
ndi
ngposi
ti
onofi
nputst
ri
ng
f
or(
i=0;
i
<input
[i
]!
='
\0'
;
i++)
{
f
or(
j=0;
j
<26;
j
++)
{
i
f(
input
[i
]==al
pha1[
j]
)
t
emp[
i]
=j;
}}
/
/Decr
ypt
edt
ext
f
or(
i=0;
i
<input
[i
]!
='
\0'
;
i++)
cout
<<al
pha[
temp[
i]
];
br
eak;
}
}
cout
<<"
\nEnt
erchoi
cet
ocont
inue(
Y/N)
:"
;
ci
n>>ch;
}
whi
l
e(ch=='
y'|
|ch=='
Y'
);
get
ch(
);
}
Out
put
:-

Ent
ert
het
ext:
secr
etemessage
1.
Encr
ypt
2.
Decr
ypt
Ent
erchoi
ce:
1
Encr
ypt
edt
ext:
VHYCHMHTHVVXOH
Ent
erchoi
cet
ocont
inue(
Y/N)
:y
Ent
ert
het
ext:
VHYCHMHTHVVXOH
1.
Encr
ypt
2.
Decr
ypt
Ent
erchoi
ce:
1
Encr
ypt
edt
ext:
secr
etemessage
2.
Impl
ement
ati
onofceaserci
pher
:-

#i
ncl
ude<i
ostr
eam>
#i
ncl
ude<st
ri
ng.h>

usi
ngnamespacest
d;

i
ntmai n()
{
  cout <<"Ent ert hemessage: \n";
  charmsg[ 100] ;
  cin.getl
ine( msg, 100) ; //takethemessageasi nput
  inti ,j
,lengt h,choi ce, key;
   cout<<" Ent erkey :" ;
  cin>>key ; //taket hekeyasi nput
  lengt h=st rlen( msg) ;
   cout <<"Ent ery ourchoi ce\ n1.Encr y
ption\ n2.Decr
ypt
ion\
n";
  ci n>>choi ce;
   if( choice==1) //forencryption
{
    charch;
    for(inti =0; msg[ i]!='\
0';++i)
{
      ch=msg[ i
];
      
/ /encr y ptforlower caseletter
      If(ch>=' a'&&ch<=' z'
)
{
       ch=ch+key ;
       if(ch>' z')
{
         ch=ch-' z'+'a'-1;
       } 
       msg[ i
]=ch;
      }
      // encr yptf orupper caseletter
      el sei f( ch>=' A'&&ch<=' Z')
{
       ch=ch+key ;
       if( ch>' Z')
{
         ch=ch-' Z'+'A'-1;
       }
       msg[ i]=ch;
      }
    }
    printf (
"Encr y
pt edmessage: %s" ,
msg) ;
   }
   el
seif(choice==2)
{
//fordecry
pti
on
    charch;
    for(
inti=0;msg[
i]!
='\
0';
++i
)
{

     ch=msg[ i];
      //decryptforl owercasel ett
er
      i
f(ch>=' a'&&ch<=' z')
{
       ch=ch-key ;
       if(
ch<' a' )
{
         ch=ch+' z'-'a'+1;
       }
       msg[i]=ch;
      }
      //decryptforupper casel etter
      
elseif(ch>=' A'&&ch<=' Z')
{
       ch=ch-key ;
       i
f(ch<' A')
{
         ch=ch+' Z' -'
A' +1;
       }
       msg[ i]=ch;
      }
    }
    cout<<" Decr yptedmessage: "<<msg;
   }
getch(
);
}

OUTPUT:
-
Forencrypti
on:
Enterthemessage:
tut
orial
Enterkey:3
Enteryourchoi
ce
1.Encrypti
on
2.Decrypti
on
1
Encr
ypt
edmessage:
wxwr
uldo

Fordecrypti
on:
Enterthemessage:
wxwr ul
do
Enterkey:3
Enteryourchoi
ce
1.Encrypti
on
2.Decrypti
on
2
Decryptedmessage:t
utori
al
3.I
mpl
ement
ati
onofHi
l
lCi
pheri
nC++:
-
/
/Pr
ogr
am f
orHi
l
lCi
pher
#incl ude<iost ream>
#incl ude<mat h.h>
usingnamespacest d;
fl
oaten[ 3][
1] ,de[3][1],a[
3][
3],b[3]
[3]
,msg[3][
1],m[3]
[3]
;
voidget Key Mat rix
()
{
//getkeyandmessagef rom user
  i nti,j;
  charmes[ 3];
  cout <<" Enter3x3mat ri
xforkey( shoul
dhav ei
nverse):
\n"
;
  f or(i=0; i<3; i
++)
  f or(j=0; j<3; j
++)
{
    ci n>>a[ i][
j]
;
    m[ i
][j]=a[ i]
[j
];
   }
  cout <<"\nEnt erast r
ingof3l ett
er(
useAt hroughZ):";
  cin>>mes;
  for(i=0; i <3; i++)
  msg[ i
][
0]=mes[ i]-65;
}

voidencr ypt()
{
/
/encr yptsthemessage
  i
nti,j,
k;
  f
or(i=0; i<3; i++)
   f
or (
j=0; j <1;j++)
    for(k=0; k<3; k++)
   
en[ i
][
j]=en[ i
][
j]+a[ i
][
k]*msg[k][
j]
;
  
cout <<"\nEncry pt
edst r
ingis:
";
   
for (
i=0; i<3; i++)
    cout<<(char )
(fmod( en[i
][
0],
26)+65) ;
//modul o26i stakenf oreachelementofthemat
ri
xobt
ainedbymul
ti
pli
cat
ion
}

v
oidinv er
sematri
x()
{
//
findinv
erseofkeymat
ri
x
   i
nti,j,
k;
  fl
oatp, q;
  for(
i =0;i<3;i
++)
   f
or(j=0;j<3;j++)
{
    i f(i==j)
      b[ i]
[j]
=1;
    el se
       b[i]
[j]
=0;
  }
  for
(k=0; k<3; k++)
{
    for (
i =0; i<3; i
++)
{
      p=m[ i
][k];
      q=m[ k][k]
;
      
for(j=0; j<3;j++)
{
       if(i!=k)
{
         m[i]
[j]=m[i]
[j
]*q-p*m[k]
[j
];
         b[
i]
[j]=b[i
][j
]*
q-p* b[
k][
j]
;
       }
      }
    }
   }
  for
(i=0; i<3;i++)
  for
(j=0; j<3;j++)
   b[i][
j]=b[ i
][j
]/m[ i]
[i
];
  cout<<"\n\nInv erseMat rixis:
\n";
  for
(i=0; i<3;i++)
{
    f or(j=0; j<3; j
++)
    cout <<b[i][j
]<<"";
    cout <<"\n" ;
   }
}

voi
ddecr y pt(
)
{
//decr yptthemessage
   i
nti ,j
, k;
   i
nv ersemat ri
x();
   for (
i=0; i<3;i++)
   for (
j=0; j<1;j++)
   for (
k=0; k<3; k++)
   de[ i
][
j]=de[ i]
[j
]+b[ i]
[k]*en[k]
[j
];
    cout <<"\nDecry pt
edst ri
ngis:";
   f or(
i =0; i<3;i++)
   cout <<(char)(fmod( de[i
][
0],26)+65);
//modul o26i st akent ogettheori
ginal
message
   cout
<<"
\n"
;
}

i
ntmai n(
)
{
  getKeyMatri
x()
;
   encrypt
();
  decr ypt
();
}

OUTPUT:
-
Ent
er3x3mat
ri
xforkey(
shoul
dhav
einv
erse)
:
1
0
1
2
4
0
3
5
6

Ent
erast
ri
ngof3l
ett
er(
useAt
hroughZ)
:ABC

Encr
ypt
edst
ri
ngi
s:CER

I
nv erseMatri
xis:
1.090910.227273-0.
181818
-0.5454550.
1363640.0909091
-0.0909091-0.
2272730.
181818

Decr
ypt
edst
ri
ngi
s:ABC
4.
GCDusi
ngEucl
i
deanal
gor
it
hm usi
ngr
ecur
sion:
-

#include<st dio.h>
#include<st dli
b.h>
#include<mat h.h>
//Calculatesgcdofn1andn2r ecur
siv
ely
i
ntgcd( inta, i
ntb)
{
 
  
 if(a==0)
 
  
   
  
retur nb;
 
  
 else
 
  
   
  
retur ngcd( b%a, a)
;
}
voidmai n()
{
 
  
 intn1,n2,answer ;
 
  
 printf
("Ent erthet wonumbers\n")
;
 
  
 scanf ("%d%d" ,
&n1,&n2)
;
 
  
 answer =gcd( n1,n2);
 
  
 printf
("GCD( %d,%d)=%d" ,
n1,
n2,answer
);
}

GCDusi
ngExt
endedEucl
i
deanal
gor
it
hm:
-

#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
dli
b.h>
#i
ncl
ude<mat
h.h>

/
/It
erat
ivef
unct
iont
ocal
cul
ategcdoft
wonumber
s
/
/usi
ngEucl
i
d’sAl
gor
it
hm
i
nteucl
i
d(i
nta,
intb)
{
i
ntr
;
/
/loopt
il
lremai
nderi
s0
whi
l
e(b>0)
{
/
/cal
cul
ater
emai
nder
r=a%b;

/
/abecomesbandbbecomesr
a=b;
b=r
;
}

r
etur
na;
}

/
/mai
nfunct
ion
i
ntmai
n()
{
FI
LE*
in,
*out
;

i
n=f
open(
"i
nput
.t
xt"
,"r
");
i
f(i
n==NULL)
{
pr
int
f("
Cannotopensour
cef
il
e.\
n")
;
exi
t(
1);
}

out=f
open(
"out
put
.t
xt"
,"w"
);
i
f(out==NULL)
{
pr
int
f("
Cannotopendest
inat
ionf
il
e.\
n")
;
exi
t(
1);
}

i
nta,
b;
charch;

/
/dot
il
lend-
of-
fi
lei
sreached
whi
l
e(!
feof
(i
n))
{
/
/readnextpai
rofi
nputf
rom t
hef
il
e
f
scanf
(i
n,"
%d%d"
,&a,
&b)
;

/
/cal
cul
ategcdandpr
intt
otheout
putf
il
e
f
pri
ntf
(out
,"eucl
i
d(%d,
%d)=%d\
n",
a,b,
eucl
i
d(a,
b))
;

/
/got
onextl
i
ne
ch=get
c(i
n);
}

/
/cl
osei
nputandout
putf
il
est
ream
f
close(
out
);
f
close(
in)
;

r
etur
n0;
}
OUTPUT:
-
eucl
i
d(2740,
1760)
=20
eucl
i
d(6,
4)=2
5.
Implement
ati
onofRSAAl
gor
it
hm i
nC(
Encr
ypt
ionand
Decr
ypti
on):
-

#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
dli
b.h>
#i
ncl
ude<math.
h>
#i
ncl
ude<st
ri
ng.h>

i
ntx,y,
n,t,i,fl
ag;
l
onginte[50],d[50],
temp[
50]
,j
,m[
50]
,en[
50]
;
charmsg[100];
i
ntpri
me(longi nt);
voi
dencryption_key();
l
ongintcd(longi nt
);
voi
dencrypt();
voi
ddecrypt();

i
ntmai n()
{
pri
ntf(
"\nENTERFIRSTPRI
MENUMBER\n"
);
scanf("
%d",&x);
fl
ag=pr ime(x)
;
i
f(fl
ag==0)
{
pri
ntf(
"\nI
NVALIDINPUT\
n")
;
exi
t(0);
}
pri
ntf(
"\nENTERSECONDPRIMENUMBER\n"
);
scanf("
%d",&y);
fl
ag=pr ime(y)
;
i
f(fl
ag==0| |x==y)
{
pri
ntf(
"\nI
NVALIDINPUT\
n")
;
exi
t(0);
}
pri
ntf(
"\nENTERMESSAGEORSTRINGTOENCRYPT\
n")
;

scanf("
%s",msg);
for
(i=0; msg[i
]!=NULL;
i++)
m[i]=msg[ i
];
n=x*y ;
t=(x-1)*(y-1)
;
encrypti
on_key()
;
pri
ntf("
\nPOSSIBLEVALUESOFeANDdARE\
n")
;
for
(i=0; i<j-
1;i
++)
pri
ntf(
"\n%ld\
t%ld"
,e[
i]
,d[
i]
);
encrypt(
);
decrypt(
);
ret
urn0;
}
i
ntpr i
me( l
ongintpr)
{
i
nti;
j=sqr t
(pr)
;
for(
i=2; i<=j;
i++)
{
if(
pr%i ==0)
r
eturn0;
}
ret
urn1;
}

/
/funct i
ont ogenerateencr
ypt
ionkey
v
oi dencr ypti
on_key()
{
i
ntk;
k=0;
f
or (
i =2; i<t;i
++)
{
i
f(t%i ==0)
cont inue;
fl
ag=pr ime(i
);
i
f(fl
ag==1&&i !
=x&&i !
=y)
{
e[k]=i ;
flag=cd( e[
k])
;
i
f(fl
ag>0)
{
d[k]=f l
ag;
k++;
}
if(
k==99)
break;
}
}
}
l
ongi ntcd( l
onginta)
{
l
ongi ntk=1;
while(1)
{
k=k+t ;
i
f(k%a==0)
retur
n(k/a)
;
}
}

/
/ functi
ont oencryptthemessage
v
oi dencr ypt()
{
l
ongi ntpt ,ct,
key=e[ 0]
,k,
len;
i=0;
l
en=st rlen(msg);
whi l
e(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
("\n\nTHEENCRYPTEDMESSAGEI
S\n"
);
for(i=0; en[i
]!=-
1;i++)
printf
("%c",en[i
])
;
}

/
/functi
ont odecryptt
hemessage
v
oi ddecry
pt()
{
l
ongintpt,ct,key=d[0]
,k;
i=0;
whil
e(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;
pri
ntf("
\n\nTHEDECRYPTEDMESSAGEI
S\n"
);
for(i=0;m[ i
]!=-
1;i
++)
printf
("%c",m[
i]
);
printf
("\n"
);
}

OUTPUT:
-
Ent
erf
ir
stpr
imenumber

Ent
ersecondpr
imenumber

13

Ent
ermessageorst
ri
ngt
oencr
ypt

Hel
l
o

Possi
blev
aluesofeanddar
e

5 29

11 59

17 17

19 19

23 47

29 5

31 7

Theencr
ypt
edmessagei
s

Hezzi

Thedecr
ypt
edmessagei
s

Hel
l
o
I
6.mpl
ement
ati
onofDi
ff
ie-
Hel
l
manAl
gor
it
hm:
-

/
*Thi
spr
ogr
am cal
cul
atest
heKeyf
ort
woper
sons 
usingt heDiffie-Hel lmanKeyexchangeal gorit
hm * /
#include<stdio. h>
#include<mat h.h>
 
 
//Powerf unct iont or eturnv alueofa^bmodP
l
ongl ongi ntpower (longl ongi nta, longlongintb,
 
  
   
   
  
  
   
  
  
  
   
   
   
   
   
longl ongi ntP)

 
  
 if(b==1)
 
  
   
  r
eturna;
 
 
 
  
 else
 
  
   
  r
eturn(((longl ongi nt)pow( a, b))%P) ;
}
 
 
//Driverprogr am
i
ntmai n()
{
 
  
 longl ongintP, G, x, a,y,b,ka, kb; 
 
  
    //Bot htheper sonswi l
l beagr eedupont he 
 
  
   
  /
/publ i
ckey sGandP 
 
  
 P=23;
//Apr imenumberPi staken
 
  
 printf("
Thev alueofP: %ll
d\ n",P); 
  G=9;
/
/Apr i
mi tv erootf orP, Gi staken
 
  
 printf("
Thev alueofG: %ll
d\ n\n", G);
 
 
   //Al i
cewi llchooset hepr ivatekeya 
 
  
 a=4;
/
/ai sthechosenpr ivatekey  
 
  
 printf("
Thepr ivatekeyaf orAl i
ce: %ll
d\n",
a) ;
 
  
 x=power (G, a,P) ;
//get sthegener atedkey
 
  
 / /Bobwi llchooset hepr i
v atekeyb
 
  
 b=3;
//bi sthechosenpr i
vatekey
 
  
 printf("
Thepr ivatekeybf orBob: %ll
d\n\n",b);
 
  
 y=power (
G, b,P) ;
//get sthegener atedkey
 
//Gener at ingt hesecr etkeyaf tertheexchange
 
  
   
  /
/ofkey s
 
  
 ka=power (y ,a,P) ;//Secr etkeyf orAli
ce
 
  
 kb=power (x,
b,P)
;//Secr
etkeyf
orBob
 
  
  
 
 
  
 pri
ntf
("Secr
etkeyf
ortheAli
ceis:
%lld\n"
,ka)
;
 
  
 pri
ntf
("Secr
etKeyfort
heBobis:%ll
d\n",
kb);
 
  
  
 
 
  
 ret
urn0;
}

OUTPUT:
-
Thev
alueofP:
23

Thev
alueofG:
9
Thepr
ivat
ekeyaf
orAl
i
ce:
4

Thepr
ivat
ekeybf
orBob:
3

Secr
etkeyf
ort
heAl
i
cei
s:9

Secr
etKeyf
ort
heBobi
s:9

You might also like