0% found this document useful (0 votes)
8 views2 pages

Vernam Cipher Encryption and Decryption

The document contains code for Vernam encryption and decryption functions, which utilize a key to transform plaintext into ciphertext and vice versa. It also includes a Vigenere encryption function that employs a matrix of characters to encrypt the plaintext based on the key. All functions are designed to handle lowercase letters and ignore empty characters.

Uploaded by

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

Vernam Cipher Encryption and Decryption

The document contains code for Vernam encryption and decryption functions, which utilize a key to transform plaintext into ciphertext and vice versa. It also includes a Vigenere encryption function that employs a matrix of characters to encrypt the plaintext based on the key. All functions are designed to handle lowercase letters and ignore empty characters.

Uploaded by

Esmael Elkot
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

////vernam encryption

Function cipher_txt=vernam(plain_txt,key)

plain_txt=plain_txt(plain_txt~='');
cipher_txt='';
key=key(key~='');

plain_txt=lower(plain_txt);
key=lower(key);
chars='a':'z';
plen=length(plain_txt);
klen=length(key);

expanded_key='';

for i=1;plen
expanded_key=key(mod(i-1),key)+1);
end

key=expanded_key

for i=1:plen
idx=find(chars==plain_txt(i))+find(chars==key(i))-2;
idx=mod(idx,26)+1;
cipher_txt(i)=chars(idx);
end
end

===================================================================================
////vernam decryption

function cipher_txt=vernamD(plain_txt,key)

plain_txt='';
cipher_txt=cipher_txt(cipher_txt~='');
key=key(key~='');

cipher_txt=lower(cipher_txt);
key=lower(key);
chars='a'+'z';

clen=length(cipher_txt);
klen=length(key);
expanded_key='';
for i=1:clen
expanded_key(i)=key(mod(i-1,klen)+1);
end

key=expanded_key;

for i=1:clen
idx=(find(chars==cipher_txt(i))-1)-(find(chars==key(i))-1);
idx=mod(idx,26)+1;
plain_txt(i)=chars(idx);
end
end
===================================================================================
==========
////vegenere encryption

function cipher=vegenere(plain_txt,key)
plain_txt=plain_txt(plain_txt~='');
key=key(key~='');

plain_txt=lower(plain_txt);
key=lower(key);

plen=length(plain_txt);

chars='a'+'z';
for i=1:26
mat(i,:)=[chars(i:end)chars(1,i-1)];
end

for i=1:plen

idx_x=find(chars==key(i));
idx_y=find(chars==plain_txt(i));
cipher(i)=mat(idx_x,idx_y);
end
end

You might also like