Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Objective and Overview Marks: out of 5%

Submission Rules
▪ Submit in Word or PDF format on blackboard.
▪ Attach screenshots of the output for all the questions.
▪ Include the front page with All the details (Course description, Student id, name etc.)
▪ Due on 25th November, 2021 – late submissions will be penalized (0.25 Mark for each
day).
▪ This lab is individual work.
▪ Dr Moatsum Alawida

In this lab, we have two sections to learn on the hash functions. In the first section we are
working on crypto tool, that has large of algorithms and another tool of cryptanalysis. In
the second section, we have code MD5 in MATLAB, to study it carefully.

Part A: CryptoTool
CrypTool 2 (CT2) is a modern e-learning program for Windows, which visualizes
cryptography and cryptanalysis. It includes not only the encryption and cryptanalysis of
ciphers, but also their basics and the whole spectrum of modern cryptography.

1- Firstly, install the CrypTool 2 (CT2 from this link


https://www.cryptool.org/en/ct2/downloads
2- Go to Template and go over all of the items, then write all of the items down below.
3- Select Caesar cipher and generate the cipher text to these plaintexts. On the Home bar, you
can find the Run and stop button to generate plaintext and ciphertext or to run any another
cryptographic algorithm.

a. Plaintext = Hello , number Input (key) 9


b. Plaintext = Helloo , number Input (key) 9
c. Plaintext = Heello , number Input (key) 9
d. Plaintext = Helllo , number Input (key) 9
e. Write all the ciphertexts and change the number Input to be 10 and write the results.

4- Select hash function and select MD5 and Play and write the hash value below. Change an
one bit and also write the hash value.
5- Write your Student Id and find the hash value and keep a screenshot below.
6- In MD5 collision, click on Play on the home bar and write the input and output below.
7- In MD5 collision, change an one of the inputs by inserting one number and find the result
and keep a screenshot below.
8- In MD5 collision, change one of the inputs by inserting two numbers and find the new
output and keep a screenshot below.
9- In SHA-1 collision, click on Play on the home bar and write the input and output below.
10- In SHA-1 collision, change an one of the inputs by inserting one number and find the
new output and keep a screenshot below.
11- In SHA-1 collision, change one of the inputs by inserting two numbers and find the new
output and keep a screenshot below.
12- In DES-Known-Plaintext Analysis. Explain what the analysis is based on the cryptoToll's
instructions.
13- Create a new workspace with the graphical editor in the Main function. The leftmost
components bar has a large number of algorithms and tools. Then choose XOR from the
list of classical ciphers. Drag it to Workspace by clicking on it. Then go to Tools and look
for Text Input and Text Output. Add two Text Inputs, fill in any values, then connect them
all together with a Xor box. Then press the Play button. Try looking at the images.
14-Similarly, we can design algorithms with a variety of inputs and outputs. Make a similar
design with your name as seen in the figure below.

15-Design new algorithms while taking into account the number of input and output files that will
be used.
Keccak
AES
Blowfish

Part B: MD5 code


The MD5 code is given in this part, which you should copy into MATLAB. MD5 is the name of
the function that you should save it as. Make a new Notepad file with your name, ID, and any other
information you want to include. Save the Notepad file in the C directory, using your first name
as the file name. Use this command line to run the MD5 function in MATLAB.
Hash=md5(' C:\ first name.txt');

Keep a screenshot of the hash value.


Code MD5
% md5 Compute MD5 hash function for files
%
% d = md5(FileName)
%
% md5() computes the MD5 hash function of
% the file specified in the string FileName
% and returns it as a 64-character array d.
% The MD5 message-digest algorithm is specified
% in RFC 1321.
% The code below is for instructional and illustrational
% purposes only. It is very clear, but very slow.
% (C) Stefan Stoll, ETH Zurich, 2006
function Digest = md5(FileName)
% Guard against old Matlab versions
MatlabVersion = version;
if MatlabVersion(1)<'7'
error('md5() requires Matlab 7.0 or later!');
end
% Run autotest if no parameters are given
if (nargin==0)
md5autotest;
return;
end
% Read in entire file into uint32 vector
[Message,nBits] = readmessagefromfile(FileName);
%--------------------------------------------------
% Append a bit-1 to the last bit read from file
BytesInLastInt = mod(nBits,32)/8;
if BytesInLastInt
Message(end) = bitset(Message(end),BytesInLastInt*8+8);
else
Message = [Message; uint32(128)];
end
% Append zeros
nZeros = 16 - mod(numel(Message)+2,16);
Message = [Message; zeros(nZeros,1,'uint32')];
% Append bit length of original message as uint64, lower significant uint32 first
Lower32 = uint32(nBits);
Upper32 = uint32(bitshift(uint64(nBits),-32));
Message = [Message; Lower32; Upper32];
%--------------------------------------------------
% 64-element transformation array
T = uint32(fix(4294967296*abs(sin(1:64))));
% 64-element array of number of bits for circular left shift
S = repmat([7 12 17 22; 5 9 14 20; 4 11 16 23; 6 10 15 21].',4,1);
S = S(:).';
% 64-element array of indices into X
idxX = [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
1 6 11 0 5 10 15 4 9 14 3 8 13 2 7 12 ...
5 8 11 14 1 4 7 10 13 0 3 6 9 12 15 2 ...
0 7 14 5 12 3 10 1 8 15 6 13 4 11 2 9] + 1;
% Initial state of buffer (consisting of A, B, C and D)
A = uint32(hex2dec('67452301'));
B = uint32(hex2dec('efcdab89'));
C = uint32(hex2dec('98badcfe'));
D = uint32(hex2dec('10325476'));
%--------------------------------------------------
Message = reshape(Message,16,[]);
% Loop over message blocks each 16 uint32 long
for iBlock = 1:size(Message,2)

% Extract next block


X = Message(:,iBlock);

% Store current buffer state


AA = A;
BB = B;
CC = C;
DD = D;
% Transform buffer using message block X and the
% parameters from S, T and idxX
k = 0;
for iRound = 1:4
for q = 1:4
A = Fun(iRound,A,B,C,D,X(idxX(k+1)),S(k+1),T(k+1));
D = Fun(iRound,D,A,B,C,X(idxX(k+2)),S(k+2),T(k+2));
C = Fun(iRound,C,D,A,B,X(idxX(k+3)),S(k+3),T(k+3));
B = Fun(iRound,B,C,D,A,X(idxX(k+4)),S(k+4),T(k+4));
k = k + 4;
end
end

% Add old buffer state


A = bitadd32(A,AA);
B = bitadd32(B,BB);
C = bitadd32(C,CC);
D = bitadd32(D,DD);
end
%--------------------------------------------------
% Combine uint32 from buffer to form message digest
Str = lower(dec2hex([A;B;C;D]));
Str = Str(:,[7 8 5 6 3 4 1 2]).';
Digest = Str(:).';
%==================================================
function y = Fun(iRound,a,b,c,d,x,s,t)
switch iRound
case 1
q = bitor(bitand(b,c),bitand(bitcmp(b),d));
case 2
q = bitor(bitand(b,d),bitand(c,bitcmp(d)));
case 3
q = bitxor(bitxor(b,c),d);
case 4
q = bitxor(c,bitor(b,bitcmp(d)));
end
y = bitadd32(b,rotateleft32(bitadd32(a,q,x,t),s));
%--------------------------------------------
function y = rotateleft32(x,s)
y = bitor(bitshift(x,s),bitshift(x,s-32));
%--------------------------------------------
function sum = bitadd32(varargin)
sum = varargin{1};
for k = 2:nargin
add = varargin{k};
carry = bitand(sum,add);
sum = bitxor(sum,add);
for q = 1:32
shift = bitshift(carry,1);
carry = bitand(shift,sum);
sum = bitxor(shift,sum);
end
end
function [Message,nBits] = readmessagefromfile(FileName)
[hFile,ErrMsg] = fopen(FileName,'r');
error(ErrMsg);
%Message = fread(hFile,inf,'bit32=>uint32');
Message = fread(hFile,inf,'ubit32=>uint32');
fclose(hFile);
d = dir(FileName);
nBits = d.bytes*8;
%============================================
function md5autotest
disp('Running md5 autotest...');
Messages{1} = '';
Messages{2} = 'a';
Messages{3} = 'abc';
Messages{4} = 'message digest';
Messages{5} = 'abcdefghijklmnopqrstuvwxyz';
Messages{6} =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
Messages{7} = char(128:255);
CorrectDigests{1} = 'd41d8cd98f00b204e9800998ecf8427e';
CorrectDigests{2} = '0cc175b9c0f1b6a831c399e269772661';
CorrectDigests{3} = '900150983cd24fb0d6963f7d28e17f72';
CorrectDigests{4} = 'f96b697d7cb7938d525a2f31aaf161d0';
CorrectDigests{5} = 'c3fcd3d76192e4007dfb496cca67e13b';
CorrectDigests{6} = 'd174ab98d277d9f5a5611c2c9f419d9f';
CorrectDigests{7} = '16f404156c0500ac48efa2d3abc5fbcf';
TmpFile = tempname;
for k=1:numel(Messages)
[h,ErrMsg] = fopen(TmpFile,'w');
error(ErrMsg);
fwrite(h,Messages{k},'char');
fclose(h);
Digest = md5(TmpFile);
fprintf('%d: %s\n',k,Digest);
if ~strcmp(Digest,CorrectDigests{k})
error('md5 autotest failed on the following string: %s',Messages{k});
end
end
delete(TmpFile);
disp('md5 autotest passed!');

End lab.

You might also like