Matlab Project、1

Topics: Cryptography, Enigma machine, Substitution cipher Pages: 7 (820 words) Published: December 10, 2012
EK127 Matlab Project 1

Yiming Zhang (C1)

Folder name：Yiming_Zhang

-------------------------------------------------
PART A: Write a function subEncrypt such that will encode a message using the simple substitution algorithm discussed in the introduction.

function encrypt=subEncrypt( plaintxt,key )
%this function help to encode a message using the simple substitution algorithm %Your plaintxt should be entered in all capitals

numkey=(65:90);
%stand for Letters A~Z

encrypt=zeros(1,length(plaintxt));
%vectors with the length of your massage
%equivalents of the encrypted message.

for ASC=1:length(plaintxt)
num=find(numkey==plaintxt(ASC));
encrypt(ASC)=key(num);
end
end

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------

-------------------------------------------------
PART B: Write a function subDecrypt such that it will decrypt a message that was encrypted using the simple substitution method

function decrypt=subDecrypt(ciphertxt,key)
%this function help to convert back the message using the simple %substitution algorithm in the subEncrypt function
%Your plaintxt should be entered in all capitals

numkey=(65:90);
%stand for Letters A~Z

decrypt=zeros(1,length(ciphertxt));
%vectors with the length of your massage
%equivalents of the encrypted message.

for ASC=1:length(ciphertxt)
num=find(key==ciphertxt(ASC));
decrypt(ASC)=numkey(num);
end

end

-------------------------------------------------
PART C: Write a function rotorShift that will shift the letters in a key by one space. -------------------------------------------------

-------------------------------------------------

function newkey=rotorShift(key)
%This function convert your old key into a new order by shift the key by %one order to the right. So whatever you have on the left of your origonal %key will become new key. And it is going to reture the newkey in its order

newkey(2:26) = key(1:25);
%(1:25) is the key in origon order, now we shift it by adding 1 on all of them newkey(1)=key(26)
%then set newKey(1) equal oldkey(26) in order to complete the cricle
end

-------------------------------------------------
PART D: Write a function enigmaEncrypt that will simulate a simplified enigma machine -------------------------------------------------

-------------------------------------------------

function enigma=enigmaEncrypt(plaintxt,key1.key2,key3)
%this function will simulate a simplified enigma machine
%Your plaintxt should be entered in all capitals
%each key corresponds to one of the rotors placed in the enigma machine.
numkey=(65:90);
%stand for Letters A~Z

enigma=zeros(1,length(plaintxt));
enigma2=zeros(1,length(enigma1));
enigma3=zeros(1,length(enigma2));
%vectors with the length of your massage
%equivalents of the encrypted message.

for ASC=1:length(plaintxt)...