Final
Final
Final
Title:
Encryption/Decryption Theory
Notes:
1. Late submissions will be marked normally then mark will be reduced by 10% of total allocated marks per calendar
day late.
2. Marks will be scaled by number of tasks attempted - if you attempt 1/3 of the tasks, you will get 1/3 of the marks.
3. You will get marks even for programs that you can’t get to work, so if you run out of time, submit them anyway.
Assessment Criteria
Criteria Totals
Program coding
Coding style (ie. following “Rules for good programming”)
Implementation of algorithms
Successful completion of programming tasks
Professional presentation of exercises
Cover sheet included
Sufficient text explaining exercise aims and results
Headings where appropriate
Figures labelled
Program testing and sample output
Adequate range of tests carried out
Appropriate presentation of sample output
Marker’s comments:
Encryption.m
Aim:
A program/function with the capacity to encrypt/decrypt any phrase consisting of only letters and
numbers (capitalised and spaces included), with the added functionality of 2 different passwords/keys
and unique representations of letters will be used for increased security. Several checks should also be
in place to ensure code runs smoothly.
The purpose of this program is to be used to send secure messages or sensitive information over a
network that is being monitored. The sender should be able send an encrypted message that cannot be
viewed by an unwanted person. The receiver should be able to decrypt the encryption using the
password related keys and read the message accurately.
Background/Theory:
Cryptography requires five different aspects to function properly in order for the delivery to execute
well. These five are (Gary C. Kessler, 2019):
1. Privacy: To ensure messages sent are secure and not being read by unwanted recipients.
2. Authenticity: To prove and acknowledge that the sender sent the message.
3. Integrity: Works consistently the same using the same information, however cannot be
misrepresented mid-transmission.
4. Acknowledgement: Receiver is able to prove the message was sent by the intended sender,
additionally with proof.
5. Key transfer: A method of transferring keys used for the encryption.
The encryption process is as follows: Alice (sender) sends normal generic text (plain text) into a
process or program which converts into ciphered text (secret code), thus able to send ciphered text over
a server. An observer (Oscar) without the encryption program cannot read ciphered text, thus the
receiver (Bob) can decipher the text and read it privately.
Server Bob (ciphered text) Cipher Process (plain text) Bob receives (plain text)
There exists different methods of cryptography each with their own strengths and weaknesses, thus
different methods share different purposes for encryption (Gary C. Kessler, 2019). Symmetric Keys are
used as the name suggests symmetrically, this is done by using the same key for encryption as well as
decryption (Kefa Rabah, 2005). The name ‘private key’ is used as it assumes both the sender and
An asymmetric key system works differently in that it has a public key attributed with the code to be
ciphered, this system allows communication to occur secretly without the initial secret exchange (Kefa
Rabah, 2005). Two keys are used, the private key and a public key (Satish et al., 2012). Typically users
encrypt using the public key whilst decrypting using the private key, this allows in theory for multiple
users to send and receive messages securely (Satish et al., 2012).
For symmetric key systems, there consists of two different types: stream ciphers and block ciphers.
Stream cipher systems look at each bit of data individually by using a feedback system in which the
key is constantly changing, whilst block cipher systems use the same key to change different ‘blocks’
of data (Satish et al., 2012).
However methods can vary between symmetric key and asymmetric key systems. A symmetric
substitution method for example, takes bits of data from the input and modifies it using an algorithmic
relationship in a way that makes it difficult for someone to view the output and trace it back to the input
(Kefa Rabah, 2005). Contrastingly, the transposition method for asymmetric key systems changes two
bits of information whilst maintaining all other information (Kefa Rabah, 2005).
The encryption program I have designed uses a symmetric substitution method; therefore for security it
is required to use an algorithmic method of converting data. Whilst the idea of using a public key is
enticing, my program will be using a private key system due to the complexity of public key systems.
My method also uses a block cipher method, by this it uses the key to change blocks of data that
represent characters for every bit of data. It therefore handles each character individually and in theory
each character should be represented differently. However vulnerabilities may be present with the
algorithmic method used and therefore should not be used to encrypt sensitive data.
It should be noted that I do use variables labelled ‘Public Key’ as I did intend to use some sort of Public
Key system, however, later I relied more heavily on a private key system due to complexity issues.
Thus, the Public Key used in my program is just an extended private key. Without both keys, the
decryption process cannot be executed.
Examples:
>> encryption
Type 1 for Encryption
Type 2 for Decryption
Please note, only use letters and numbers
Spaces are permitted in passwords and phrases
>1
Input password: kyle!!!
Please input only letters and numbers
Input password: WineBottleLateNite5
Input phrase: hehehehehe
Type 1 to get new public key
Type 2 to reuse old public key
>1
Your public key is: E1Z2jOYimLJfVHI
New phrase: Ht0dkoBNGJoJM63TCwisLdjGuzLdxMigiN5OLWOooeSBbLZJlD
Type 1 for Encryption
Type 2 for Decryption
Please note, only use letters and numbers
Notes: For this example it can be seen that the check works correctly and thus ensures only the correct
letters/numbers are present. The final produced encryption is:
‘Ht0dkoBNGJoJM63TCwisLdjGuzLdxMigiN5OLWOooeSBbLZJlD’. We can break up this code to
see how the program handles different ‘h’ and ‘e’ characters.
Ht0dk oBNGJ oJM63 TCwis LdjGu zLdxM igiN5 OLWOo oeSBb LZJlD
h e h e h e h e h e
However if we use a password that is similar to the original but is not quite the same, some code may
be decrypted despite not having the complete password. Although as the password length increases and
becomes more complex the security of the encryption increases also. Example:
>> encryption
Type 1 for Encryption
Type 2 for Decryption
Please note, only use letters and numbers
Spaces are permitted in passwords and phrases
>2
Input password: winebottlelatenite5
Input phrase: Ht0dkoBNGJoJM63TCwisLdjGuzLdxMigiN5OLWOooeSBbLZJlD
Type 1 to get new public key
Type 2 to reuse old public key
>2
Please input public key: E1Z2jOYimLJfVHI
New phrase:
Type 1 for Encryption
Type 2 for Decryption
Please note, only use letters and numbers
Spaces are permitted in passwords and phrases
>2
Input password: WineBottleLateNite
Input phrase: Ht0dkoBNGJoJM63TCwisLdjGuzLdxMigiN5OLWOooeSBbLZJlD
Type 1 to get new public key
Type 2 to reuse old public key
>2
Please input public key: E1Z2jOYimLJfVHI
New phrase:
Note: In this attempt no result is produced despite being very similar to original password.
For this example the original password used was ‘kylep’ and the original public key was
‘yefgo9KJCEFsdqD’ producing a final encrypted message of
‘VutGz720PxSRBiNOTRnLljwIIF6u67jACeVJaHlEgbWi9SKXBesBeBT’. However the following
variations did not produce any results:
kyle
kyleo
kyle1
kylee
kyleP
Kylep
Example:
>> encryption
Type 1 for Encryption
Type 2 for Decryption
Please note, only use letters and numbers
Spaces are permitted in passwords and phrases
>1
Input password: kylep
Input phrase: Hello world
Type 1 to get new public key
Type 2 to reuse old public key
>1
Your public key is: yefgo9KJCEFsdqD
New phrase: VutGz720PxSRBiNOTRnLljwIIF6u67jACeVJaHlEgbWi9SKXBesBeBT
Type 1 for Encryption
Type 2 for Decryption
Please note, only use letters and numbers
Spaces are permitted in passwords and phrases
>2
Input password: kyle
Input phrase: VutGz720PxSRBiNOTRnLljwIIF6u67jACeVJaHlEgbWi9SKXBesBeBT
Type 1 to get new public key
Type 2 to reuse old public key
>2
Example:
>> encryption
Type 1 for Encryption
Type 2 for Decryption
Please note, only use letters and numbers
Spaces are permitted in passwords and phrases
>1
Input password: 4389amsd23
Input phrase: Hello world can you read me
Type 1 to get new public key
Type 2 to reuse old public key
>1
Your public key is: HrR0ULGhK4cyvZp
New phrase:
MpOlZeC9IcXGk2RGl6taGaCLRqCTsGqdQWI1Ldc8Ql44zpJec63QabOSST9Vplp0L79egdJ3zwvOe
k91LdDC31tiCaJ5axabsTqssahr0bqr38qBuxSrDiR7jhKVQO2HrpbeQmt
Type 1 for Encryption
Type 2 for Decryption
Please note, only use letters and numbers
Spaces are permitted in passwords and phrases
>2
Input password: 4389amsd23
Input phrase:
MpOlZeC9IcXGk2RGl6taGaCLRqCTsGqdQWI1Ldc8Ql44zpJec63QabOSST9Vplp0L79egdJ3zwvOe
k91LdDC31tiCaJ5axabsTqssahr0bqr38qBuxSrDiR7jhKVQO2HrpbeQmt
Type 1 to get new public key
Type 2 to reuse old public key
>2
Please input public key: HrR0ULGhK4cyvZp
New phrase:
'Hello world can you read me'
References:
Boneh, Dan, and Victor Shoup. A Graduate Course in Applied Cryptography. 2016. Accessed May 31,
2019. https://crypto.stanford.edu/~dabo/cryptobook/draft_0_3.pdf.
Dent, Alexander W. "Fundamental Problems in Provable Security and Cryptography." 2006.
https://eprint.iacr.org/2006/278.pdf.
Kessler, Gary C. "An Overview of Cryptography." An Overview of Cryptography. 2019.
https://www.garykessler.net/library/crypto.html.
Rabah, Kefa. 2005. "Theory and Implementation of Data Encryption Standard: A Review. Information
Technology Journal." 10.3923/itj.2005.307.325.
SATISH, GANTI & Raghavendran, Dr. Ch V & T K Mehar, P & P Suresh Varma, Dr. (2012).
SECRET KEY CRYPTOGRAPHIC ALGORITHM. 1. Retrieved from:
https://www.researchgate.net/publication/266389826_SECRET_KEY_CRYPTOGRAPHIC_A
LGORITHM
Appendix:
Encryption.m
function encryption ()
% display 'warning about writing down passwords case sensentive and in a secure
% location.
%
% Input all key variables including encrypt/decrypt, password, public key and
phrase.
%
% after all password variables and default characters are created.
% we now have change the password into encryption. therefore apple becomes a
% length('apple') times 5 string variable. we now have to convert that into a 15
character
% password, this is to shorten and make it more compact.
%
% To do this we take the raw password and compare the first and last letters ('a'
and 'e').
%
%
% 'we now use [code] function to change according to each other.
% Example: 'ap' and 'pl' changes to 'bw' and then 'bw' and 'e' changes to 'bwe'.
% It must always result in the same unique combination.
Start_Program = 1;
Sample_nospace = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
Sample_withspace = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890
';
while Start_Program == 1
PrKCheck=0;
PrKCheck_loop = 1;
while PrKCheck_loop==1
PrKCheck = PrKCheck +1;
private_key = input ('Input password: ', 's');
while PrKCheck>0
if PrKCheck == 1
PrKvar= private_key(PrKCheck:PrKCheck) == Sample_withspace;
else
end
PrKCheck = PrKCheck +1;
end
end
%checks phrase for valid characters
phraseCheck=0;
phraseCheck_loop = 1;
while phraseCheck_loop==1
phraseCheck = phraseCheck +1;
phrase = input ('Input phrase: ', 's');
while phraseCheck>0
if phraseCheck == 1
phrasevar= phrase(phraseCheck:phraseCheck) == Sample_withspace;
else
phrasevar= phrasevar + (phrase(phraseCheck:phraseCheck) ==
Sample_withspace);
end
if phraseCheck == length(phrase)
if sum(phrasevar) == length(phrase)
phraseCheck_loop = 0;
phraseCheck= -100;
else
phraseCheck = -1;
disp('Please input only letters and numbers');
end
end
phraseCheck = phraseCheck +1;
end
end
Public_Key = randsample(Sample_nospace,15);
PubK_Check=1;
while PubK_Check==1
disp('Type 1 to get new public key');
disp('Type 2 to reuse old public key');
PubK_oldnew = input('> ');
if PubK_oldnew == 1 || 2
PubK_Check=0;
else
disp('Please type either 1 or 2');
end
end
if PubK_oldnew == 1
%Setting key
Num=0;
Increment = 1;
while Num>-1
Num=Num+1;
CryptTableSet = strhandle(Num, CryptedPassword(Increment));
if Increment == 16
Increment = 1;
end
end
%Using the public key to convert the private key into a unique key.
Num=0;
while Num< 15
Num=Num+1;
CryptTableSet = strhandle(Num, Public_Key(Num));
SetTable = containers.Map('A', CryptTableSet(1));
SetTable('B') = CryptTableSet(2);
if EncryptOrDecrypt == 1
Selected = phrase(NumTotal);
elseif EncryptOrDecrypt == 2
Constant= (NumTotal*4)-4;
try
Selected = phrase(NumTotal+Constant:NumTotal+Constant+4);
catch
NumTotal = -1;
disp('New phrase: ');
disp(newphrase);
end
end
if EncryptOrDecrypt == 1
newphrase = strcat(newphrase,SetLetters(Selected));
elseif EncryptOrDecrypt == 2
Increment=1;
while Increment<64
if SetLetters(Sample_withspace(Increment))== Selected
newphrase = strcat(newphrase, {Sample_withspace(Increment)});
end
if NumPass == 16
NumPass=1;
end
if NumTotal == length(phrase)
NumTotal = -1;
disp(['New phrase: ', newphrase]);
end
end
end
end
Strhandle.m
% However through testing, this function does not perfectly produce every
% combination for the string.
%String to be transformed.
if TargetLetter == 'A'
CryptTable = 'Ew8dre6LosiTc19bC5hjHKvMYBpFWqSP07OlAINX2fGJUDngaQzxtuykRmZ34V';
elseif TargetLetter == 'B'
CryptTable = 'IAPtZnfrS7MGJExwasyjD39YzikKQHWu2bCXpVh86cUBglON1TvFoL4qdm5Re0';
elseif TargetLetter == 'C'
CryptTable = '2HFvaWjq6KM1YGtDSZPA5kRidX7hLzmryoClNwsugVn0TEBf3c4p8eJ9xIbOUQ';
elseif TargetLetter == 'D'
CryptTable = '7hbpJFqCu1M83ZK9zSTclevxRO6WgXVEmHijfNDkrLQ2wn5sPdIy40aoGUBYAt';
elseif TargetLetter == 'E'
CryptTable = 'pvOJLP7obVUqn8ziCuBXWTSt0MDya4NZlGsRrKAjHxfdIkY1w92e6m3cEQF5hg';
elseif TargetLetter == 'F'
CryptTable = 'btrofLMXhzU2HNpqewgnJYyIkvjmAiGW1KEu8cx40s9Q6Dd7Pa53lZFOBVCRTS';
elseif TargetLetter == 'G'
CryptTable = 'V5xvtDpr3TfhukN2wq1o8aA4mFLZ7REgWzBQMiYJb6HOyXdj9lS0KCUnGcesPI';
elseif TargetLetter == 'H'
CryptTable = 'QPSnIi7yWNHvhmALRf9KtDeqVl8aYXzCBj3MJpG41rO5u0xZsEUgTkc62odbwF';
elseif TargetLetter == 'I'
CryptTable = '5cTfNXvJqyMYWQxtK1DaSwFegpOB3UEoR76zr9bZHhd4LnC8VIusm0AlGiP2kj';
elseif TargetLetter == 'J'
CryptTable = 'P6WEQrNI2R09BCM5aGXfikF3psgSjqo4bVLemdOYnTl8vHxwzuKZDJhcyUAt71';
elseif TargetLetter == 'K'
CryptTable = 'HbNUiCrO4oE8c1e9WKxZmMugnBVD7qz0Xkty6a2SIhJAQsjvRPFTfdwLY5G3lp';
elseif TargetLetter == 'L'
CryptTable = 'dwOyuXaI5SqlKBR3g8rFnNxtJU1eAsV0jLG9okHhfWEviCTpmbYD476c2QPZMz';
elseif TargetLetter == 'M'
CryptTable = 'C3Qwq5ioXN1BYUEFTekbH4gLf926WltR0a7cOGZnupPdxhzm8MIyVDJsSAjKrv';
elseif TargetLetter == 'N'
CryptTable = 'zfLyEWYi9vxKcwoHS7lV5GJjag6dCpr12ebkQNnZ840sXFIuOqThtBDR3UAPmM';
elseif TargetLetter == 'O'
%All variables used are set to zero before loop. This is so function works
%the first time as well as the later instances.
RotationCount = 0;
Increment = 0;
CryptTableCopy = CryptTable;
TransferCount = 0;
TransferCountTotal = 0;
ExceptionRule=0;
TransferCount2 = 0;
ResultCount = 0;
StartProgram=1;
while StartProgram==1
%Counts by 1
RotationCount = RotationCount+1;
% If the ResultCount equals the length, all possible combinations for moving
one letter
%left has been achieved. Therefore we now must move a letter by two
%steps or more. Once we transform it by two or more steps. we then can
%transform that by length(str) amount again resulting in new combos.
if RotationCount == length(CryptTable)
%Incremental counters.
TransferCount = TransferCount +1;
TransferCountTotal = TransferCountTotal + 1;
if TransferCount2 == 1