0% found this document useful (0 votes)
13 views12 pages

CNS Observation 6 Programs

The document describes a program to perform encryption and decryption using Caesar cipher and substitution cipher. It includes code snippets in Java to encrypt and decrypt a string using a key for both ciphers.
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)
13 views12 pages

CNS Observation 6 Programs

The document describes a program to perform encryption and decryption using Caesar cipher and substitution cipher. It includes code snippets in Java to encrypt and decrypt a string using a key for both ciphers.
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/ 12

) Wite a c program that Containt

D
a string char poirter) toith a valua'Helo
World.The proaiom Should XoR cach
Proqtam
Chasatenin this tring
eiAing with o and
display the
Proqram.
include<stdioh>
#incude <stdib-hz

incude< Conio h> il As


void main ()
world;:
= llo
He
Charstr 1
char stri[
inti, leh;
len Strlenstr); crs((I;
tor (i=0;1<leng)
sty ITi] = Str(1^0,
printf( "str);.n

printf(') Sdput'
Hello Woil d.
write a Cproqram that Contains
with
o value Hello orla'. Tho tor (izo;icen ;itr )
Rroqiam should AND or and Xol
eathchoTacter in this string wth 24 pritf Cc,st2C);
and duplby the resalt -
prite ("yn)
#include<stdio-h
*includ e cloni oh
Aindude <stringh
Aincude < st db h
Vod main C) Dtput
=Hello world; Hlo t o o t l d a .
Charstr[]
Char stT1 L
Char str 2i
int i, len
len = Strlen (str);
forCiz0; i<lens i t ) l v o

("»c)str 1[3)
printf
printf(n":
3) te olova pioqamg to perfosm
Encypherm and decyrhon Using
a) Ceaser cipher:
decsspteo)Sito34on
Import java.io.Buffev Reados
)
SyutomO4-printlnc'lw);
import java io-TOE Xcephion 'y
import java. io .I nput streanReader; ypt (Stringir iot k)
impost jova.UtlScanhery publ Staie Strinq entt
puble dars Ce aser ciphe

tic Scanner se- new Scannersgieniny Stieg ertrytedo5at


o, ie
ie str
str- len (1;
(; it1)
it1)i
dtr Butleredkeader br -new Butendkeudlen for( it i:
(rew 1nqutStreamReaden Cystem 4n));
pudlicsatie void main(StringCl arqs)thol C0;
iot C= Str.CharAt

Sysitemoutqint
Strinqstr = by.teadine
C):
if CC)
System -but print l"Enter any Sting )
int key = SC- next Tnt (
Sting encrypied - tncpt (st,key);
wy' opayituv klnn yhij bcdef c 26); (c<a)' ie
Coyptedf -key' C=C
de (be
( if
(]ing maiSn Vod Slahe patlic lowevlaw.
(c))s mckes
is
chaacfer cha
) eada 1npud
Stea Recder(aea
Stadie
ffered eo brReadeButfeied
Statie
new Scner Scan
= ppec
Co) Case (charater-is
Sustitaion
Cigko c
lass jublic 1F
ufl*; jova: toport str.Chart
(i = inct
io-K) jaua.inport (inti for
Dogamps ="; ypted String
dec
Cipher:
Subshtution (b) key) iot
(St spt dec string i
stati puble
Vsl. :2x is datcncrypted
a he
echo 0cSting
Cutputoidboot. any Enter
Cchar)c, enruped
t=
m
dasyp import jovo lo*,
ChosC)
icogi< str .lngth import il.*;
for (int imQort jova. io*;
publie clays Hill cphor i new flot 1(;
hart C)) pubu
(r Sts inden f (): Static float C1l1
intie a. new floot C11C3];
=decry gt decy p+b.Char AL totic float C1C] a
Stadie floatC]1b =
decnt
Ted= new float m.
Stoie loat)CJ
dotois'ttc ypr)s Siadie Bufes Rendubr = ferudnew
new Buf
3 Inpud Stream ReaderCsstemin)

Ente a3leter sring


hai pule seatic vo,d main(Striag Qq)
hrowoE1cepion
Tvoue Matihh
j<l;tt)
o.5833333 -0-08SSSsR6
for (int jzo, KIi; k+ t\
for Cint KO;
Deciype&rqiA:hai
for (int ic oj ic 3; it);

Sysrean Oudt 26441);


forlinti co,ir3yitt)
Systee.Gut pritn J:
96tq3))
ublc static void get key meil j thowt
Tolxcephon
Sysem-out prital Eten 3xi notix fo
i+4)ss
kor Gnt iosi<3,

fo (int k co,.ks3:

decypt C(1=dec p-CICUT000


public Satic void inuoue c)

b Cnu1- bli1Ci y- p*b [e]Ci:

f6ninti oyi<3,i t)
fohl i ntj -0,is3, 'it)
af:1C31 le.ne float C);
bli01 =

foh (iot k zo; tez;kt)


folint ízo l<3;tt

fon Cint j-0,j<1;jt)


Encopd Sti ngis(dx
Inuey e Matiis:
0. 3333 333G 6. 4166666G impost java io.BufferedReoder,
inpott Jav impott jaaa-Cypto-i0.Input Stream
-6333 3333y Readu;)ol
imgort javaz. Crypto. Seciet key tackoxy;
import jaua Crypto.spec DE Sede KeySpec
import sun. misc- &ASE64Decoder:
658333 37 ioapprtimpottsun:misc. BASEG4 En Coder
pubie class DES {
Deptes gi:hai -03333334 private String my Encysonkcy
Cncrypiomonßcheme
lue
private String my

pivate Stig my ncrypionbcheme,


Secrer keykey
Static Buf fered Reader brs new Buffer edReader

Tog0Cade oqplation oqichere


myEneIy pht
BASEG4¬ Nodon bases4en codor
(neBASE 64
-sCHEME,
Abytes
UN ODE-fokMAf); cncodai.encoda
encyphed String =base6u
key(UNI(
toye kkeyfactb
Secoct
mretKogPa Catch lExcephion )
vaceC):
tackT
c.printS
Scheme3d
s03up.laai
lkey-ay Seezekeyfactboyqenerae
Setret myspee)s
nll,Ne
Stringdypted Toa =

Decyt_m oDe, Key);


Cphea init(uptoa baleby de Coder new
BASEEHD eCoda
BASE64 Decoder );
bytelJencryped Tet- baye 64decoden deCode
Be(encypted sting), dofindl lea(ngped
byhelpaintexts unenc(Typted String. byteC plantext cipheu
qet Byte lUnICoDE foRMAi Cplanlert),
Ai byte CncyptedTeset Gehy do Final dexyptedteKt-bytes 2 stia
CPaintext)s 3
Catch(Ex
IracoC); SAaing enTypted = myEncryptor. cncvyot
tae (sthing To
e prit Ertayot);
TehondencryptedText; Saning decgftod - my Enctypon.decoyet (ency pie)
System.out. phinth C"ns+sigToEnrypi?
bytes
Sting Sthing To Enc
Paivateslati sgpt)20owvayni
Sstem.out pint in("mEncieted ulue' enmedl;
System out-primt Ih ("niDeryped val
`triagbaffenstiag ButETng Shring baton ckcred);

for (int i o; is byteslength; i4+)

stringbufktr.opperd (cha)byteslil);
ektbnst tingBufferto Stng
0;CPL
puole siate vsid main (Stiog axqs C1)t
hrow sEx

GDES my tnctyptor- new DE S(O;


Stringning to Encypt ba. (ealline ;
5.Pro qramto implement Boo Fich
algovithm logic. File InptStre am fn= mew file putStncam 'nputile a
Phoqramthhigtmetigersh
FileCuasreanfout - neuw File Outyut Stru am ('output fle t
Cipherlutputtream Cout znewCiphenOutputStheam
import javo io.Pile Lopt Seannsn Po fout, Giphncat);
io FleOutput Stheam:
importjava ío int input D;
impott java :Sccutity Key; whileCl input afinead 0)!=-)
import javaL- Crypto. apron?gptsnoa
impolt javat.crypte. ciphenOtputSrscam; cout-whçte (input);
import javaz. (uypto -key Gene abn;
inport Sun. mise. BASE 64 Encodeh ;
puble ckss Bloufishf Cot Close ();
pablik state void main (thing (jargs) thows Exephoný
To Do code applicationogiche Key Gheneton
qcneradekuqo
Ciphencpherbut-Cipeget Instance ("Blbufiih ltEBl Toli;zation Wecoh of Hhe Cipher i dI(MX2WRoQ =
N, Padding");
iphenlut init(ciphu. ENCRYPT_MODE, sectet key) ; Contentsof input File.trt : Hello World
BSEG4Encodexercoden= newBASE 64 Encoden); Contentsofoutpt FiletaJö"Na['

byteivCJe phnutqetU O3
Systenout. prínth ('nihaizahon Vecteh oF Hhe Cphe
tencoder. en code li);
6. l'roq1amming
ting mesage "AES sill socks;l3
Riin dael algosithm logicsi liat
key henemtonkqen= key Ghenexato
phomam t.qet Instan Ce
("AEs"),
Lmport javaSeubity.*j Kgen. init (I23);
import javat. Cry phor*,
(42 and a56 brts mnay not be aw ai lable
inport javax -("ypto. spec*;
inport java io-#; specs.Secter keyskey= kçen qene vote key);
puble cdajs AES £ byte C]baw = Skey
qtEncoded);
putblcstaic Stting as Hex (bye burci) SecvetkeySpec=new Secoet keySpec (raw, "AEr);
StNog Buffe Strbuf e newSthing Buefer (buf lengk Ciphe CipheY : Cipher.get Tnstence ("AES);
*2);
inti;
oiphen.init(Cigher. ENCRY PT MODE,Skey Spec);
byte (lencrypted- C'pk dofinal
fon(i-0; icbuf length; i+) (arqslength =-oJresag:
iECGntl bufCil4Oxff) <oxo)sl Systen aut riotin("'ncrypted
str buf.append ("o"); string:"tas
Ciphen. init(cgheDECRYpTMOOE, Heslencny ral
SkeySper);
sthDXÉ,4):
buf-appund (long:to[ring (int) buf[i4 byteciOriginal = ciphen. dofinal Cenc ryted)y
Seton shbuf. to Sthig(3;
public stati c void man (stringc]ara) thsous Sqstemout printhn( oignatring: toijndStringr" +
Excep ton as Hen (origina));

You might also like