0% found this document useful (0 votes)
28 views8 pages

Practical: 1

The document outlines practical programming assignments for a Network Security course, including C programs for XOR and AND operations on strings, Java implementations of various encryption methods (Caesar Cipher, Substitution Cipher, Hill Cipher), and a Blowfish algorithm demonstration. Each practical includes code snippets and aims to teach fundamental concepts in encryption and data security. The assignments emphasize hands-on coding experience in both C and Java.

Uploaded by

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

Practical: 1

The document outlines practical programming assignments for a Network Security course, including C programs for XOR and AND operations on strings, Java implementations of various encryption methods (Caesar Cipher, Substitution Cipher, Hill Cipher), and a Blowfish algorithm demonstration. Each practical includes code snippets and aims to teach fundamental concepts in encryption and data security. The assignments emphasize hands-on coding experience in both C and Java.

Uploaded by

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

Network Security (CY5009) Enrollment No: 202203103510223

Practical: 1
Aim: Write a C program that contains a string(char pointer) with a value \Hello
World’. The programs should XOR each character in this string with 0 and display the
result. Code: Practical1.c

Code:
#include <stdio.h>

int main() {
char *str = "Hello World";
char result[12]; // Array to store the result
int i;

for (i = 0; str[i] != '\0'; i++) {


result[i] = str[i] ^ 0; // XOR each character with 0
}
result[i] = '\0'; // Null-terminate the result string

printf("Original string: %s\n", str);


printf("Result string after XOR with 0: %s\n", result);

return 0;
}

Output:
Network Security (CY5009) Enrollment No: 202203103510223

Practical: 2
Aim: Write a C program that contains a string (char pointer) with a value \Hello
World’. The program should AND or and XOR each character in this string with
127 and display the result.

Code:
#include <stdio.h>

int main() {
// Declare a string with the value "Hello World"
char *str = "Hello World";

// Iterate over each character in the string


for (int i = 0; str[i] != '\0'; i++) {
// XOR the character with 127
char xor_result = str[i] ^ 127;

// AND the character with 127


char and_result = str[i] & 127;

// Display the results


printf("Original: %c, XOR with 127: %c, AND with 127: %c\n", str[i],
xor_result,
and_result);
}

return 0;
}

Output:
Network Security (CY5009) Enrollment No: 202203103510223

Practical: 3
Aim: Write a Java program to perform encryption and decryption using Ceaser Cipher,
Substitution Cipher and Hill Cipher.
Code:
import java.util.*;

public class practical3 {

// Caesar Cipher
public static String caesarEncrypt(String text, int shift) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (Character.isLetter(c)) {
char base = Character.isLowerCase(c) ? 'a' : 'A';
c = (char) ((c - base + shift) % 26 + base);
}
result.append(c);
}
return result.toString();
}

public static String caesarDecrypt(String text, int shift) {


return caesarEncrypt(text, 26 - shift);
}

// Substitution Cipher
public static String substitutionEncrypt(String text, String key) {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
text = text.toUpperCase();
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (Character.isLetter(c)) {
int index = alphabet.indexOf(c);
result.append(key.charAt(index));
} else {
result.append(c);
}
}
return result.toString();
}

public static String substitutionDecrypt(String text, String key) {


String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
text = text.toUpperCase();
Network Security (CY5009) Enrollment No: 202203103510223

StringBuilder result = new StringBuilder();


for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (Character.isLetter(c)) {
int index = key.indexOf(c);
result.append(alphabet.charAt(index));
} else {
result.append(c);
}
}
return result.toString();
}

// Hill Cipher
public static String hillEncrypt(String plaintext, int[][] keyMatrix) {
int[] plaintextVector = new int[2];
int[] ciphertextVector = new int[2];
StringBuilder ciphertext = new StringBuilder();

for (int i = 0; i < plaintext.length(); i += 2) {


plaintextVector[0] = plaintext.charAt(i) - 'A';
plaintextVector[1] = plaintext.charAt(i + 1) - 'A';

for (int row = 0; row < 2; row++) {


ciphertextVector[row] = 0;
for (int col = 0; col < 2; col++) {
ciphertextVector[row] += keyMatrix[row][col] *
plaintextVector[col];
}
ciphertextVector[row] %= 26;
}

ciphertext.append((char) (ciphertextVector[0] + 'A'));


ciphertext.append((char) (ciphertextVector[1] + 'A'));
}

return ciphertext.toString();
}

public static String hillDecrypt(String ciphertext, int[][] keyMatrix) {


int[][] inverseKeyMatrix = inverseMatrix(keyMatrix);

return hillEncrypt(ciphertext, inverseKeyMatrix);


}

private static int[][] inverseMatrix(int[][] matrix) {


int determinant = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
int inverseDet = modInverse(determinant, 26);
Network Security (CY5009) Enrollment No: 202203103510223

int[][] inverseMatrix = new int[2][2];


inverseMatrix[0][0] = matrix[1][1] * inverseDet % 26;
inverseMatrix[0][1] = -matrix[0][1] * inverseDet % 26;
inverseMatrix[1][0] = -matrix[1][0] * inverseDet % 26;
inverseMatrix[1][1] = matrix[0][0] * inverseDet % 26;

for (int row = 0; row < 2; row++) {


for (int col = 0; col < 2; col++) {
inverseMatrix[row][col] = (inverseMatrix[row][col] + 26) % 26;
}
}

return inverseMatrix;
}

private static int modInverse(int a, int m) {


a = a % m;
for (int x = 1; x < m; x++) {
if ((a * x) % m == 1) return x;
}
return 1;
}

public static void main(String[] args) {


String text = "HELLO";
int shift = 3;

// Caesar Cipher
System.out.println("Caesar Cipher:");
String caesarEncrypted = caesarEncrypt(text, shift);
System.out.println("Encrypted: " + caesarEncrypted);
String caesarDecrypted = caesarDecrypt(caesarEncrypted, shift);
System.out.println("Decrypted: " + caesarDecrypted);

// Substitution Cipher
String key = "QWERTYUIOPASDFGHJKLZXCVBNM";
System.out.println("\nSubstitution Cipher:");
String substitutionEncrypted = substitutionEncrypt(text, key);
System.out.println("Encrypted: " + substitutionEncrypted);
String substitutionDecrypted = substitutionDecrypt(substitutionEncrypted,
key);
System.out.println("Decrypted: " + substitutionDecrypted);

// Hill Cipher
int[][] hillKeyMatrix = {{3, 3}, {2, 5}};
String hillPlaintext = "HI";
System.out.println("\nHill Cipher:");
Network Security (CY5009) Enrollment No: 202203103510223

String hillEncrypted = hillEncrypt(hillPlaintext, hillKeyMatrix);


System.out.println("Encrypted: " + hillEncrypted);
String hillDecrypted = hillDecrypt(hillEncrypted, hillKeyMatrix);
System.out.println("Decrypted: " + hillDecrypted);
}
}

Output:
Network Security (CY5009) Enrollment No: 202203103510223

Practical: 4
Aim: Write a C/JAVA program to implement the Blowfish algorithm logic.
Code:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class practical4 {


public static String encrypt(String plainText, SecretKey secretKey) throws
Exception {
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}

public static String decrypt(String encryptedText, SecretKey secretKey) throws


Exception {
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes =
cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decryptedBytes);
}

public static void main(String[] args) {


try {
// Generate a Blowfish key
KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
SecretKey secretKey = keyGenerator.generateKey();

String plainText = "Hello World";


System.out.println("Original Text: " + plainText);

// Encrypt the text


String encryptedText = encrypt(plainText, secretKey);
System.out.println("Encrypted Text: " + encryptedText);

// Decrypt the text


String decryptedText = decrypt(encryptedText, secretKey);
System.out.println("Decrypted Text: " + decryptedText);

} catch (Exception e) {
e.printStackTrace();
Network Security (CY5009) Enrollment No: 202203103510223

}
}
}

Output:

You might also like