0% found this document useful (0 votes)
22 views13 pages

Encryption - Decrytption - Octave Help Codes

Uploaded by

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

Encryption - Decrytption - Octave Help Codes

Uploaded by

waleedashraf337
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

IT 6010-Maths for Computing

Octave Help Codes


Caesar Cipher System
%Caesar Encryption
k=3; % key or no. of shifts
n=26; % key space or total no.of letters/charaters
m='HELLOWORLD'; % plaintext
v=m-65; % convert plaintext to integers or numerical values
for n=26 and n=58. (v=m-48 for n=78)
e=mod(v+k,n); % encryption formula
char(e+65) % convert values back to letters or characters
for n=26 and n=58. [ char(e+48) for n=78 ]

%Caesar Decryption
k=3; % key or no. of shifts
n=26; % key space or total no.of letters/charaters
m='KHOORZROUG'; % ciphertext
v=m-65; % convert plaintext to integers or numerical values
for n=26 and n=58. (v=m-48 for n=78)
d=mod(v-k,n); % decryption formula
char(e+65) % convert values back to letters or characters
for n=26 and n=58. [ char(e+48) for n=78 ]
Affine Cipher System

%Affine Encryption

a=5; % keys
b=8;
n=26; % key space or total no.of letters/charaters
m='HELLO'; % plaintext
v=m-65; % convert plaintext to integers or numerical values
for n=26 and n=58. (v=m-48 for n=78)
e=mod(a*v+b,n); % encryption formula
char(e+65) % convert values back to letters or characters
for n=26 and n=58. [ char(e+48) for n=78 ]
Affine Cipher System
%Finding inverse (For smaller values)
a=5; % change this according to your a
n=26; % change this according to your n
for i=1:n
if mod(a*i, n)==1;
inverse=i
end
end

%Affine Decryption
a_inv=21; % change this according to your a inverse
b=8;
n=26; % key space or total no.of letters/charaters
m='HELLO'; % plaintext
v=m-65; % convert plaintext to integers or numerical values
for n=26 and n=58. (v=m-48 for n=78)
e=mod(a_inv*(v-b),n); % decryption formula
char(e+65) % convert values back to letters or characters
for n=26 and n=58. [ char(e+48) for n=78 ]
BRUTE FORCE- Ceasar Cipher

C = 'KHOORZROUG'; % Ciphertext (you can change this


according to the given ciphertext)
for k = 0:25 % Possible keys (no. of shifts)
P = char(mod(C - 65 - k, 26) + 65); % Decryption
printf('Shift %d: %s\n', k, P);
end
RSA Cipher System
Finding d, that is the inverse of e ( for bigger values)
function [g, d, k] = extended_gcd(a, b)
if b == 0
g = a;
d = 1;
k = 0;
else
[g, x1, y1] = extended_gcd(b, mod(a, b));
d = y1;
k = x1 - floor(a / b) * y1;
end
end

Note:
• a is the e, and b is the phi (Φ)
• If d is negative, add it to phi to get a positive d
RSA Cipher System
Modular Exponentiation
function result = mod_exp(base, exp1, mod1)
result = 1; % Initialize result

base = mod(base, mod1); % Take base mod mod

while exp1 > 0


if mod(exp1, 2) == 1 % If exp is odd
result = mod(result * base, mod1); % Multiply
base with result
end
exp1 = floor(exp1 / 2); % Divide exp by 2
base = mod(base^2, mod1); % Square the base
end
end
Example 1. Vector Translation
A robot's current position is represented by a vector p1 = [2,3] (position 1
vector). If m represents the movement vector, then find m if the robot
needs to move 5 units to the right and 4 units down and find the new
position of the robot.

p1 = [2, 3]; % Define the initial position vector


m = [5, -4]; % Define the movement vector
p2 = p1 + m; % Calculate the new position
Example 2. Vector Translation
A character in a 3D game world is controlled by the player using both the
keyboard and mouse. The character can move forward/backward and
strafe left/right. The character's movement is defined by two vectors:
• Forward Movement Vector
f = [0, 0, 1]— This vector represents movement along the z-
axis in the game’s world coordinates.
• Strafe Movement Vector
s = [1, 0, 0]— This vector represents movement along the x-axis in
the game’s world coordinates.

a. If the player presses the forward and right keys simultaneously,


compute the character’s total movement representing the combined
movement vector and describe its movement.
b. If the player presses the forward and backward keys at the same time,
what is the forward movement vector? the backward movement
vector?
Example 2. Vector Translation
a. If the player presses the forward and right keys simultaneously,
compute the character’s total movement representing the combined
movement vector and describe its movement.

f = [0, 0, 1]; % Define the forward movement vector


s = [1, 0, 0]; % Define the strafe movement vector
m = f + s; % Calculate the combined movement vector
Example 2. Vector Translation
b. If the player presses the forward and backward keys at the same time,
what is the forward movement vector? the backward movement vector?
Describe the character’s movement.

f = [0, 0, 1]; % Define the forward movement vector


b = [0, 0, -1]; % Define the backward movement vector
m = f + b; % Calculate the combined movement vector
Example 3. Vector Scaling
The character is moving forward with a direction vector, v = [0,0,1] where:
• 0 along the x-axis means no strafing (sideways) movement.
• 0 along the y-axis means no vertical movement (staying on the
ground)
• 1 along the z-axis means the character is moving forward.
The player's current speed is represented by a scalar value s = 5, where s
indicates the speed in units per second (e.g., meters per second).
Calculate the actual movement vector based on the direction and speed

s = 5; % Scalar speed in units per second


v = [0, 0, 1]; % Moving forward in the z-axis
v_s = s * v; % Perform scalar multiplication
Example 4. Rotation
A spaceship in a 2D plane is represented by a triangle with vertices
A=(0,0), B=(2,0), C=(1,2). Rotate the spaceship 45-degrees counter-
clockwise around the origin.

% Define initial vertex as row vector


% Change this line according to the given vertex
v = [2;0]; % Row vector

% Rotation matrix
theta = deg2rad(45); % Convert to radians
R = [cos(theta), -sin(theta); sin(theta), cos(theta)]; %
Counterclockwise rotation matrix

% Apply rotation
rot_v = R*v

You might also like