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

Encryption and Decryption

The document discusses encryption and decryption algorithms implemented in MATLAB, including the Caesar cipher, Vigenere cipher, and Hill cipher. The Caesar cipher encrypts messages by shifting each letter by a set number. The Vigenere cipher uses a keyword to shift letters by different amounts throughout the message. The Hill cipher encrypts blocks of letters using matrix multiplication. The document provides algorithms and sample code to encrypt and decrypt messages with each cipher.

Uploaded by

Vishwa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Encryption and Decryption

The document discusses encryption and decryption algorithms implemented in MATLAB, including the Caesar cipher, Vigenere cipher, and Hill cipher. The Caesar cipher encrypts messages by shifting each letter by a set number. The Vigenere cipher uses a keyword to shift letters by different amounts throughout the message. The Hill cipher encrypts blocks of letters using matrix multiplication. The document provides algorithms and sample code to encrypt and decrypt messages with each cipher.

Uploaded by

Vishwa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

ENCRYPTION AND DECRYPTION

AIM:
To implement and to test the performance of the following data encryption
and decryption algorithms using MATLAB.
1)Caesar Cipher
2)Vigenere Cipher
3)Hill Cipher
Caesar Cipher:
Algorithm:
1) Start
2) Get the message and number of shifts as input.
3) During encryption, each alphabet in the message is shifted to the
respective number of shifts and replaced by another alphabet, places down
the line.
4) During decryption, each alphabet in the encrypted text is decremented
to the respective number of shifts and replaced by another alphabet, places
down the line.
5)Stop the program.

Vigenere Cipher:
Algorithm:
1) Start
2) Get the message and key as input.
3) To encrypt a message, equivalent values of message and key are added.
4) The result gives the equivalent of particular alphabet which is the cipher
text.
Program for Caesar Cipher:
clear all;
close all;
clc;
a=input('Enter a string : ','s');
b=input('Enter the shift : ');
c=mod((a+b),128);
disp('ENCRYPTED MESSAGE :');
disp(char(c));
d=mod((c-b),128);
disp('DECRYPTED MESSAGE :');
disp(char(d));

Program for Vigenere Cipher:


clear all;
close all;
clc;
a=input('Enter the string : ','s');
key=input('Enter the Key :','s');
encrypt='';
decrypt='';
len1=numel(a);
len2=numel(key);
if(len1<len2)
key1=key(1:len1);
b=mod(double(a)-128+double(key1),128);
disp('Encrypted message:');
disp(char(b));
c=mod(double(b)-128-double(key1),128);
disp('Decrypted message:');
disp(char(c));
else
num=floor(len1/len2);
for i=1:num
key=strcat(key,key);
end
key1=key(1:len1);
b=mod(double(a)-128+double(key1),128);
disp('Encrypted message');
disp(char(b));
c=mod(double(b)-128-double(key1),128);
disp('Decrypted message');
disp(char(c));
end
5) To decrypt a message, equivalent values of message and key are
subtracted to get the plaintext.
6) Stop the program.
Hill Cipher:
Algorithm:
1) Start
2) Get the message and key matrix as input.
3) To encrypt a message, each block of n letters (considered as an n-
component vector) is multiplied by an invertible n x n matrix against
modulus 26.
4) To decrypt a message, each block is multiplied by the inverse of matrix
used for encryption.
5) Stop the program.

THEORY:
Symmetric key cryptography:
Symmetric key algorithms are algorithms for cryptography that use the
same cryptographic keys for both encryption of plaintext and decryption of
cipher text. The keys may be identical or there may be simple
transformation to go between the two keys. The keys in practice represent
a shared secret between two or more parties that can be used to maintain a
private information link. This requirement that both parties have access to
the secret key is one of the main drawbacks of symmetric key encryption,
in comparison with public key encryption (also known as asymmetric key
encryption).
Asymmetric key cryptography:
Asymmetric key cryptography or public key cryptography is a
cryptographic system which uses pairs of keys: public keys(which may be
known to others) and private keys(which may never be known by any
except the owner).The generation of such key pairs depends on
Program for Hill Cipher:
clear all;
close all;
clc;
key=input('Enter the key matrix:');
disp('The key matrix is');
disp(key);
a=input('Enter a string:','s');
answer='';
len1=numel(a);
num=len1/2;
for i=1:num
msg=a(((i-1)*2)+1:2*i);
at=transpose(double(msg)-97);
s=key*at;
s=mod(transpose(s),26)+97;
answer=strcat(answer,char(s));
end
disp('Encrypted message');
disp(answer);
adj=mod(adjoint(key),26);
de=mod(det(key),26);
answer2='';
mul=1;
for i=1:num
msg=answer(((i-1)*2)+1:2*i);
at=transpose(double(msg)-97);
for j=0:25
if(mod(de*j,26)==1)
mul=j;
break;
end
end
s=adj*mul;
s=s*at;
s=mod(transpose(s),26)+97;
answer2=strcat(answer2,char(s));
end
disp('Decrypted message');
disp(answer2);
Cryptographic algorithms which are based on mathematical problems
termed one-way-functions. Effective security requires keeping the private
key private; the public key can be openly distributed without
compromising security.
In such a system, any person can encrypt a message using the intended
receiver’s public key, but that encrypted message can only be decrypted
with the receiver’s private key.
Public key algorithms are fundamental security primitives in modern
cryptosystems, including applications and protocols which offer assurance
of the confidentiality, authenticity and non-repudiability of electronic
communications and data storage.
Caesar cipher:
The Caesar cipher is one of the earliest known and simplest ciphers. It is a
type of substitution cipher in which each letter in the plaintext is shifted a
certain number of places down the alphabet. For example, with a right
shift of 1A would be replaced by B,B would become C and so on. The
method is named after Julius Caesar, who apparently used it to
communicate with his generals.
To pass an encrypted message from one person to another, it is first
necessary that both parties have the ‘key’ for the cipher, so that the sender
may encrypt it
And the receiver may decrypt it. For the Caesar cipher, the key is the
number of characters to shift the cipher alphabet.
Vigenere cipher:
The Vigenere cipher is a method of encrypting alphabetic text by using a
series of interwoven Caesar ciphers, based on the letters of a keyword. It
employs a form of polyalphabetic substitution.
To encrypt, a table of alphabets can be used, termed a tabularecta,
Vigenere square or Vigeneretable. It has the alphabet written out 26 times
in different rows, each alphabet shifted cyclically to the left compared to
the previous alphabet, corresponding to the 26 possible Caesar ciphers.
Output for Caesar Cipher:

Output for Vigenere Cipher:


At different points in the encryption process, the cipher uses a different
alphabet from one of the rows. The alphabet used at each point depends on
a repeating keyword.

Hill cipher:
The Hill Cipher is a poly graphic substitution cipher based on linear
algebra. Invented by Lester S.Hill in 1929, it was the first poly graphic
cipherin which it was practical to operate on more than three symbols at
once. The following discussion assumes an elementary knowledge of
matrices. Each letter is represented by a number modulo 26.
To encrypt a message, each block of n letters (considered as an n-
component vector)is multiplied by an invertible n x n matrix against
modulus 26.To decrypt a message, each block is multiplied by the inverse
of matrix used for encryption .
The matrix used for encryption is the cipher key, and it should be chosen
randomly from the set of invertible n x n matrices(modulo 26).The cipher
can, ofcourse,be adapted to an alphabet with any number of letters; all
arithmetic just need to be done modulo the number of letters instead of
modulo 26.
Output for Hill Cipher:
Result:
Thus a given message is encrypted using Caesar Cipher, Vigenere Cipher,
Hill Cipher and successfully decrypted.

You might also like