Uploaded by Mariyam Ashraf

Lab#01(crypto)

advertisement
CE-408: Cryptography & Network Security
SSUET/QR/114
LAB#01
Commands & Functions used for Cryptography
OBJECTIVE
The purpose of this lab is to present the commands and function of MATLAB used for cryptography.
MATLAB
MATLAB (Matrix laboratory) is an interactive software system for numerical computations and graphics.
A numerical analyst called Cleve Moler wrote the first version of Matlab in the 1970s. It has since evolved
into a successful commercial software package. As the name suggests, Matlab is especially designed for
matrix computations: solving systems of linear equations, computing eigen values and eigenvectors,
factoring matrices, and so forth. In addition, it has a variety of graphical capabilities, and can be extended
through programs written in its own programming language. Many such programs come with the system;
a number of these extend Matlab's capabilities to nonlinear problems, such as the solution of initial value
problems for ordinary differential equations.
Characteristics of MATLAB:
1. Slow (compared with FORTRAN or C) i.e. not pre-compiled. Avoid for loops; instead use vector
form.
2. Automatic memory management, i.e., you don't have to declare arrays in advance.
3. Shorter program development time than traditional programming languages such as FORTRAN
and C.
4. Can be converted into C code via MATLAB compiler for better efficiency.
5. Many application-specific toolboxes available.
Main toolboxes which are going to be used are: communication toolbox, signal processing toolbox and
other supplement tool boxes.
Simple commands and statements:


Arithmetic operations and variables.
Matrix and vectors.
Lab#01: Commands and functions used for cryptography
Page |1
CE-408: Cryptography & Network Security




SSUET/QR/114
Matrix operations and point wise operations.
Element extraction from a matrix.
Basic functions and special matrices
Creating m files and basic programs
Variables in MATLAB:
Type of variable in MATLAB is determined when it is created. One don’t have to declare variable before
assigning it a value. There are two types of variable.
i)
Scalar Variables
Those variables that have one (1) row and one (1) column
ii)
Vector Variables
Vector is a matrix with wither one (1) row or one (1) column (1xn) or (nx1)
iii)
Matrix Variables
Those variables have (n) row and (m) column (nxm)
Try the following codes:
Open a new m file and name it prog1 then execute the following commands:
%Always use comments
a = 5;
% numeric variable
b = [1 2 3]
% A row vector which also can be written as b = [1,2,3]
c = [4; 5; 6]
% A column vector which also can be written as
% c = [4 5 6]' where ' indicates the transpose of a matrix
d = [2 4 5;3 3 3; 1 2 7]
%matrix creation 3 by 3
f = []
%to create an empty matrix
g = ones(3,4)
%3by4 matrix with all ones
h = zeros(6)
%create 6by6 matrix of all zeros
size(g)
%finds the size of g matrix
size(g,1)
%finds number of rows in g
Lab#01: Commands and functions used for cryptography
Page |2
CE-408: Cryptography & Network Security
size(g,2)
%finds number of columns in g
length(b)
%number of elements in b
length(c)
%number of elements in c
length(g)
%number of elements in a single row in g
SSUET/QR/114
Random Number Generators:
There are 3 types of random number generator (RNG)
1) True Random Number Generator (TRNG)
E.g. flipping coin (flipping coin 100 times), lottery, and thermal noise in chips/semiconductors,
mouse movement and keystroke timings
2) Pseudo Random Number Generator (PRNG)
PRN are computed i.e. they are deterministic and often computed with the following function
S0= seed
Si+1=F(Si)
These cannot use for the key because they are deterministic e.g. in software testing etc.
3) Cryptographically Secure PRNG (CPRNG)
CPRNGs are PRNGs with an additional property--- the numbers are unpredictable
k = randint(3,4)
%3by4 randomly generated 0s and 1s.
l = randint(4,4,[3,5])
%4by4 randomly generated integer numbers from 3 to 5.
m = randsrc(4,4,[3:5])
%same as l
n = randsrc(4,4,[3,5])
%4by4 randomly generated integer numbers of 3 and 5 only.
o = rand(2,2)
%2by2 randomly generated numbers between 0 and 1 %uniformly
distributed.
randperm(5)
%generate random permutation of integer 1 to n
p = magic(5)
%5by5 magic matrix. See the details of this matrix in the %detailed
help. Note that the magic matrix can only be a %square matrix
%Operations on matrices
Lab#01: Commands and functions used for cryptography
Page |3
CE-408: Cryptography & Network Security
q = 2*g
%scalar multiplication
r = b + c'
%Addition
s=g*m
%matrix multiplication
t = b' .* c
%element wise multiplication
u = o .^ 4
%each element in o is raised to power 4
det(m)
%the determinant of m
v = eye(4)
%identity matrix of 4by4
SSUET/QR/114
%Element extraction
m(2,2)
%extracts the element intersection at 2nd row 2nd column in m
m(1:3,2)
%extracts the elements at 1 to 3 rows and 2nd column in m in %this
way using the range sign : , you can extract any element %or elements
depending on the intersections
m(:,3)
%means extract the whole 3rd column
m(1,:)
%means extract the whole 1st row
m(:,:)
%extracts the whole matrix m
m(:)
%arrange m in a single column vector
w = [b' c d]
%concatenate different matrices into a single one such that the
%resultant matrix forms an acceptable shape.
z = [4:0.1:5]
%creates a row vector having an increment of 0.1
diag(l)
%extracts the diagonal elements of the square matrix
%Basic functions
sum(d)
mean(d)
max(d)
min(d)
sin(d)
Lab#01: Commands and functions used for cryptography
Page |4
CE-408: Cryptography & Network Security
SSUET/QR/114
cos(d)
exp(d)
log(d)
%for more built in functions, see the help on list of functions
whos
%detailed information about the variables
clear d m
%clears the variables d and m
whos
%confirm that m and d are cleared
clear all
%clears all currently assigned variables
Logical Operations in MATLAB
Many logical operations are used in cryptography for encryption and decryption.
and
Find logical AND
not
Find logical NOT
or
Find logical OR
xor
Find logical exclusive-OR
Xor is most commonly used operator in cryptography.

C = xor(A,B)
C = xor(A,B) performs a logical exclusive-OR of arrays A and B and returns an array containing elements
set to either logical 1 (true) or logical 0 (false). An element of the output array is set to logical 1 (true)
if A or B, but not both, contains a nonzero element at that same array location. Otherwise, the array element
is set to 0.

C = bitxor(A,B) returns the bit-wise XOR of A and B.
Arithmetic and Logical Shift Operators
Right and left arithmetic and shift operators are used to shift the bits.

c=bitsra(a,k)
c=bitsra(a,k) returns the result of an arithmetic right shift by k bits on input a for fixed-point operations.
For floating-point operations, it performs a multiply by 2-k.
Lab#01: Commands and functions used for cryptography
Page |5
CE-408: Cryptography & Network Security
SSUET/QR/114
If the input is unsigned, bitsra shifts zeros into the positions of bits that it shifts right. If the input is
signed, bitsra shifts the most significant bit (MSB) into the positions of bits that it shifts right.
 c = bitsll(a, k)
c = bitsll(a, k) returns the result of a logical left shift by k bits on input a for fixed-point
operations. bitsll shifts zeros into the positions of bits that it shifts left. The function does not check
overflow or underflow.
 c = bitsrl(a, k)
c = bitsrl(a, k) returns the result of a logical right shift by k bits on input a for fixed-point
operations. bitsrl shifts zeros into the positions of bits that it shifts right. It does not check overflow or
underflow.
Some basic programming. Open a new m file and name it prog2 then execute the following
commands
function res = prog2( v ) %function definition. This function can be called from any %place
after passing it the argument v such that v is a row (in %this %example)
for b=1:size(v,2)
%for loop
ind(b) = v(b) * randint(1);
end
%terminate the loop with end
if (ind-v == 0)
%if-else condition
display('all ones sequence') %to display a text or value
else
display('not all zeros sequence')
end
%to terminate the if-else or if
Lab#01: Commands and functions used for cryptography
condition
Page |6
CE-408: Cryptography & Network Security
SSUET/QR/114
Lab Tasks:
1. Perform all the above commands by initializing a matrix of 4 x 5.
2. Initialize two matrices of size 3 x 2. Apply all the logical operations on them.
3. Using a programming approach, create the following matrix:
9
9
9
9
9
9
9
9
9
9
0
0
0
0
0
0
0
9
9
0
0
0
0
0
0
0
9
9
0
0
0
0
0
0
0
9
9
0
0
0
0
0
0
0
9
9
0
0
0
0
0
0
0
9
9
0
0
0
0
0
0
0
9
9
0
0
0
0
0
0
0
9
9
9
9
9
9
9
9
9
9
4. A system is performing encryption operation on a data using the figure below. The data is your
Roll no. Convert your data into binary. The key is generate randomly by CPRNG. The length of
the key should be same as input. . Display the encrypted message.
Lab#01: Commands and functions used for cryptography
Page |7
CE-408: Cryptography & Network Security
SSUET/QR/114
5. Writ a program that generates the 9 x 9 matrix randomly. Extract the 3 x 5 matrix from it. Generate
key matrix of the same size as of extracted matrix randomly. Perform Xor operation.
6. A system is performing an encryption operation on a text data using the figure shown below. The
letters are mapped into bits in the following manner: the letter is converted into its corresponding
sequence number in the English alphabets and then that number is converted into binary
representation. The length of the key used the same as the length of a single letter in bits. Display
the encrypted message in text form. Assume any key of your choice.
The text to encrypt is your complete name e.g. Ibrahim. Hint: discard the spaces between name
and the capitalization of letters.
7. Write the differences between Xor and bitwise Xor and right arithmetic and logical operator.
Lab#01: Commands and functions used for cryptography
Page |8
Download