Cyber Law1

You might also like

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

A

LAB REPORT

OF

COMPUTER SECURITY & CYBER LAW

By

Beniksha Thapa

Exam Roll: 10402/19

Submitted to:

Krishna Bhandari

Department of Management

Nepal Commerce Campus

In the partial fulfillment of the requirements for

the course Computer Security and Cyber Law.

MinBhawan, Kathmandu

March, 2023
Index

LAB TITLE OF THE LAB SIGNATURE


No.
LAB 1: UNDERSTANDING AND IMPLEMENTING SHIFT
CIPHER
Objectives:
The objectives of the lab dealt with understanding and implementing shift cipher. We aim to
receive hands on experience on how to encrypt and decrypt the message using shift cipher.

Theory:
A shift cipher is a substitution cipher, the principle of which is to shift the letters by one or
more values in the alphabet.
Example: The letter A shifted by 1 place in the alphabet becomes B

How to encrypt using the shift cipher?


The shift cipher encryption uses an alphabet and a key (made up of one or more values) that
shifts the position of its letters. A letter in position N in the alphabet, can be shifted by X into
the letter located at position N+X
How to decrypt using the shift cipher?
Decryption requires knowing the shift used and the alphabet. Take a letter in position N in the
alphabet that has been encrypted by a shift of X, it must be shifted by -X to return to its original
position N-X.
Source Code & Output:
1. Encryption & Decryption
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1">
<title>Cipher Text</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-
alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-
GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<h2>Encryption</h2>
<script>
function caesarCipher(str, num) {
num = num % 26;
str = str.toUpperCase();
let result = "";
for (let i = 0; i < str.length; i++) {
let ascii = str.charCodeAt(i);
if (ascii >= 65 && ascii <= 90) {
ascii += num;
if (ascii > 90) {
ascii -= 26;
}
else if (ascii < 65) {
ascii += 26;
}
}
result += String.fromCharCode(ascii);
}
return result;
}
document.write(caesarCipher("benikshya",3));
</script>
</div>
<div class="container">
<h2>Decryprion</h2>
<script>
function caesarCipher(str, num) {
num = num % 26;
str = str.toUpperCase();
let result = "";
for (let i = 0; i < str.length; i++) {
let ascii = str.charCodeAt(i);
if (ascii >= 65 && ascii <= 90) {
ascii -= num;
if (ascii > 90) {
ascii -= 26;
}
else if (ascii < 65) {
ascii += 26;
}
}
result += String.fromCharCode(ascii).toLowerCase();
}
return result;
}
document.write(caesarCipher("ehqlnvkbd",3));
</script>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-
alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-
w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN"
crossorigin="anonymous"></script>
</body>
</html>
Output:

Conclusion:

From above lab we gained hands on experience on implementing shift cipher.


LAB 2: UNDERSTANDING AND IMPLEMENTING PLAYFAIR

CIPHER
Objectives:
The Playfair cipher is designed to keep the message secret from unauthorized parties. By replacing pairs of
letters in the plaintext message with corresponding pairs of letters from the Playfair grid, the resulting
ciphertext message is difficult to read without knowledge of the encryption method and the key used to
create the grid.

Theory:
The Playfair cipher is a manual symmetric encryption technique that uses a 5x5 matrix of letters to encrypt
and decrypt messages. It was invented by Charles Wheatstone in 1854 and was widely used during World
War I and World War II.

Here's how the Playfair cipher works:

Key Setup: Choose a keyword or phrase to use as the key for the cipher. Remove any repeated letters and
then fill in the remaining spaces of a 5x5 matrix with the remaining letters of the alphabet (excluding "J",
which is usually replaced by "I").

Message Preparation: Divide the plaintext message into pairs of letters, ignoring any punctuation, spaces,
or repeated letters. If the message has an odd number of letters, add an "X" to the end to make it even.

Encryption: For each pair of letters in the plaintext, apply the following rules to obtain the corresponding
ciphertext

Source code:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1">
<title>Playflair</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-
alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-
GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Encryption in PlayFair Cipher</h1>
<script>
function playfairEncrypt(plainText, key) {
// Clean the plain text by removing spaces and making all characters
uppercase
plainText = plainText.replace(/ /g, "").toUpperCase();
// Generate the key table by removing duplicates and combining I and
J
key = key.toUpperCase().replace(/J/g, "I").replace(/[^A-Z]/g, "");
key += "ABCDEFGHIJKLMNOPQRSTUVWXYZ".replace(/J/g, "I");
key = key
.split("")
.filter(function (item, pos, self) {
return self.indexOf(item) == pos;
})
.join("");
// Initialize the ciphertext and digraphs array
var cipherText = "";
var digraphs = [];
// Split the plain text into digraphs (two-letter combinations)
for (var i = 0; i < plainText.length; i += 2) {
if (i == plainText.length - 1) {
digraphs.push(plainText.charAt(i) + "X");
} else if (plainText.charAt(i) == plainText.charAt(i + 1)) {
digraphs.push(plainText.charAt(i) + "X");
i--;
} else {
digraphs.push(plainText.charAt(i) + plainText.charAt(i + 1));
}
}
// Encrypt each digraph
for (var j = 0; j < digraphs.length; j++) {
// Find the position of the letters in the key table
var letter1 = key.indexOf(digraphs[j].charAt(0));
var letter2 = key.indexOf(digraphs[j].charAt(1));
// Find the row and column of each letter in the key table
var row1 = Math.floor(letter1 / 5);
var col1 = letter1 % 5;
var row2 = Math.floor(letter2 / 5);
var col2 = letter2 % 5;
// Encrypt the digraph based on the row and column of each letter
if (row1 == row2) {
cipherText += key.charAt(row1 * 5 + (col1 + 1) % 5);
cipherText += key.charAt(row2 * 5 + (col2 + 1) % 5);
} else if (col1 == col2) {
cipherText += key.charAt(((row1 + 1) % 5) * 5 + col1);
cipherText += key.charAt(((row2 + 1) % 5) * 5 + col2);
} else {
cipherText += key.charAt(row1 * 5 + col2);
cipherText += key.charAt(row2 * 5 + col1);
}
}
// Return the encrypted ciphertext
document.write (cipherText);
}
playfairEncrypt("meet me after toga party","almighty");
</script>
</div>
<div class="container">
<h1>Decryption</h1>
<script>
function playfairDecrypt(cipherText, key) {
// Generate the key table by removing duplicates and combining I and
J
key = key.toUpperCase().replace(/J/g, "I").replace(/[^A-Z]/g, "");
key += "ABCDEFGHIJKLMNOPQRSTUVWXYZ".replace(/J/g, "I");
key = key
.split("")
.filter(function (item, pos, self) {
return self.indexOf(item) == pos;
})
.join("");
// Initialize the plain text and digraphs array
var plainText = "";
var digraphs = [];
// Split the cipher text into digraphs (two-letter combinations)
for (var i = 0; i < cipherText.length; i += 2) {
digraphs.push(cipherText.charAt(i) + cipherText.charAt(i + 1));
}
// Decrypt each digraph
for (var j = 0; j < digraphs.length; j++) {
// Find the position of the letters in the key table
var letter1 = key.indexOf(digraphs[j].charAt(0));
var letter2 = key.indexOf(digraphs[j].charAt(1));
// Find the row and column of each letter in the key table
var row1 = Math.floor(letter1 / 5);
var col1 = letter1 % 5;
var row2 = Math.floor(letter2 / 5);
var col2 = letter2 % 5;
// Decrypt the digraph based on the row and column of each letter
if (row1 == row2) {
plainText += key.charAt(row1 * 5 + (col1 + 4) % 5);
plainText += key.charAt(row2 * 5 + (col2 + 4) % 5);
} else if (col1 == col2) {
plainText += key.charAt(((row1 + 4) % 5) * 5 + col1);
plainText += key.charAt(((row2 + 4) % 5) * 5 + col2);
} else {
plainText += key.charAt(row1 * 5 + col2);
plainText += key.charAt(row2 * 5 + col1);
}
}
// Return the decrypted plain text
document.write (plainText.toLowerCase());
}
playfairDecrypt("LFPELFMDEPPBSALOIOYB","almighty");
</script>
</div>
</body>
</html>
Output:

Conclusion:

From above lab we gained hands on experience on implementing Playfair cipher.


LAB 3: UNDERSTANDING AND IMPLEMENTING VIGENÈRE
CIPHER
Objectives:

The Vigenère cipher is relatively easy to use compared to other encryption techniques. It requires only a
keyword and a simple table of letters, making it accessible to individuals without advanced mathematical
or technical knowledge.

Theory:

The Vigenere cipher is a manual symmetric encryption technique that uses a keyword to encrypt and decrypt
messages. It was invented by the French cryptographer Blaise de Vigenere in the 16th century and was
considered unbreakable for several centuries.

The Vigenere cipher is based on the concept of polyalphabetic substitution, which means that each letter of
the plaintext message is substituted with a different letter based on a repeating keyword. This makes the
cipher more complex than simple substitution ciphers, which use a fixed substitution for each letter.

To use the Vigenere cipher, a keyword is chosen and repeated as many times as needed to match the length
of the plaintext message. Each letter of the keyword corresponds to a different letter substitution for each
letter of the plaintext. The resulting ciphertext is a series of letters that are difficult to decipher without
knowledge of the keyword.

While the Vigenere cipher was once considered unbreakable, advances in cryptanalysis have made it
relatively easy to break with modern computers. However, it remains a useful tool for learning about
encryption techniques and for basic encryption needs.

Source Code:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1">
<title>Vigenere CipherText</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-
alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-
GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<h4>Encryption in Vigenere CipherText</h4>
<script>
function vigenereEncrypt(plainText, key) {
// Convert the plain text and key to upper case
plainText = plainText.toUpperCase();
key = key.toUpperCase();
// Initialize the cipher text and key index variables
var cipherText = "";
var keyIndex = 0;
// Encrypt each letter in the plain text
for (var i = 0; i < plainText.length; i++) {
// Get the ASCII code of the current letter in the plain text
var plainTextCode = plainText.charCodeAt(i);
// Check if the current character is a letter
if (plainTextCode >= 65 && plainTextCode <= 90) {
// Get the ASCII code of the current letter in the key
var keyCode = key.charCodeAt(keyIndex % key.length) - 65;
// Encrypt the current letter using the Vigenere cipher formula
var cipherTextCode = ((plainTextCode - 65 + keyCode) % 26) + 65;
// Append the encrypted letter to the cipher text
cipherText += String.fromCharCode(cipherTextCode);
// Increment the key index variable
keyIndex++;
} else {
// Append the current character to the cipher text if it is not a
letter
cipherText += plainText.charAt(i);
}
}
// Return the encrypted cipher text
document.write(cipherText);
}
vigenereEncrypt("Attack from the SouthEast", "Point");
</script>
</div>
<div class="container">
<h4>Decryption in Vigenere CipherText</h4>
<script>
function vigenereDecrypt(cipherText, key) {
// Convert the cipher text and key to upper case
cipherText = cipherText.toUpperCase();
key = key.toUpperCase();
// Initialize the plain text and key index variables
var plainText = "";
var keyIndex = 0;
// Decrypt each letter in the cipher text
for (var i = 0; i < cipherText.length; i++) {
// Get the ASCII code of the current letter in the cipher text
var cipherTextCode = cipherText.charCodeAt(i);
// Check if the current character is a letter
if (cipherTextCode >= 65 && cipherTextCode <= 90) {
// Get the ASCII code of the current letter in the key
var keyCode = key.charCodeAt(keyIndex % key.length) - 65;
// Decrypt the current letter using the Vigenere cipher formula
var plainTextCode = ((cipherTextCode - 65 - keyCode + 26) % 26) +
65;
// Append the decrypted letter to the plain text
plainText += String.fromCharCode(plainTextCode);
// Increment the key index variable
keyIndex++;
} else {
// Append the current character to the plain text if it is not a
letter
plainText += cipherText.charAt(i);
}
}
// Return the decrypted plain text
document.write(plainText.toLowerCase());
}
vigenereDecrypt("PHBNVZ TZBF IVM FHJHPRTHH","Point");
</script>
</div>
</body>
</html>

Output:

Conclusion:

From above lab we gained hands on experience on implementing Vigenère cipher.


LAB 4: UNDERSTANDING AND IMPLEMENTING
TRANSPOSITION (RAIL FENCE) CIPHER
Objectives:

The Rail Fence cipher is relatively easy to use compared to other encryption techniques. It requires only a
set number of rows and a simple pattern of writing the plaintext in a zigzag across the rows, making it
accessible to individuals without advanced mathematical or technical knowledge.

Theory:

The Rail Fence cipher is a type of transposition cipher that rearranges the letters of a message to create the
ciphertext. It is named after the way the ciphertext is arranged in a zigzag pattern that resembles a rail fence.
This cipher was first used in ancient times, but it gained popularity during the American Civil War as a way
to encode messages.

The Rail Fence cipher is a relatively simple encryption technique that can be done by hand, without the use
of computers or specialized equipment. It works by writing the plaintext message diagonally across a set
number of rows, then reading the ciphertext row by row in a zigzag pattern. To decrypt the ciphertext, the
process is simply reversed by writing the letters diagonally across the rows again and reading the plaintext
message row by row.

The strength of the Rail Fence cipher comes from its simplicity and its ability to obscure the original
message by rearranging the letters. However, it is not a very secure method of encryption as it can be broken
with basic cryptanalysis techniques. As such, it is not typically used for sensitive or important
communications, but it can be a fun and educational tool for learning about cryptography.

Source code:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-
scale=1">
<title>Railfence Cipher</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-
alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-
GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<h4>RailFence Cipher</h4>
<script>
function railFenceCipher(str, numRails) {
// Create an array to hold each rail as a string
let rails = [];
for (let i = 0; i < numRails; i++) {
rails.push("");
}
// Initialize variables for keeping track of the current
rail and direction
let currentRail = 0;
let direction = 1; // 1 for down, -1 for up
// Iterate over each character in the string and place it on the
appropriate rail
for (let i = 0; i < str.length; i++) {
rails[currentRail] += str.charAt(i);
currentRail += direction;
// If we've reached the top or bottom rail, change direction
if (currentRail === 0 || currentRail === numRails - 1) {
direction = -direction;
}
}
// Concatenate all the rails into the final ciphertext string
let ciphertext = rails.join("");
document.write(ciphertext);
}
railFenceCipher("meetmeattogaparty",3);
</script>
</div>
<div class="container">
<h4>RailFence Decipher</h4>
<script>
function railFenceDecipher(ciphertext, numRails) {
// Create an array to hold each rail as a string
let rails = [];
for (let i = 0; i < numRails; i++) {
rails.push("");
}
// Create an array to hold the length of each rail
let railLengths = [];
for (let i = 0; i < numRails; i++) {
railLengths.push(0);
}
// Calculate the length of each rail
let currentRail = 0;
let direction = 1;
for (let i = 0; i < ciphertext.length; i++) {
railLengths[currentRail]++;
currentRail += direction;
if (currentRail === 0 || currentRail === numRails - 1) {
direction = -direction;
}
}
// Initialize variables for keeping track of the current rail and
direction
currentRail = 0;
direction = 1;
// Iterate over each character in the ciphertext and place it on the
appropriate rail
for (let i = 0; i < ciphertext.length; i++) {
rails[currentRail] += "*"; // use a placeholder character to make
the rail the right length
currentRail += direction;
if (currentRail === 0 || currentRail === numRails - 1) {
direction = -direction;
}
}
// Iterate over the ciphertext again, replacing each placeholder
character with the corresponding ciphertext character
let index = 0;
for (let i = 0; i < numRails; i++) {
for (let j = 0; j < railLengths[i]; j++) {
rails[i] = rails[i].substr(0, j) + ciphertext.charAt(index) +
rails[i].substr(j + 1);
index++;
}
}
// Read the plaintext off the rails in order
let plaintext = "";
currentRail = 0;
direction = 1;
for (let i = 0; i < ciphertext.length; i++) {
plaintext += rails[currentRail].charAt(0);
rails[currentRail] = rails[currentRail].substr(1);
currentRail += direction;
if (currentRail === 0 || currentRail === numRails - 1) {
direction = -direction;
}
}
document.write(plaintext);
}
railFenceDecipher("mmtpyetetoaateagr",3);
</script>
</div>
</body>
</html>
Output:

Conclusion:

From above lab we gained hands on experience on implementing Rail Fence Cipher

You might also like