0% found this document useful (0 votes)
2 views

kapil

The document outlines various practical exercises conducted by a student named Kapil Sharma at Krishna Engineering College, focusing on different encryption algorithms and techniques in programming. Each practical includes code snippets in C or Java, inputs, and expected outputs for algorithms such as XOR, Caesar Cipher, DES, Blowfish, AES, RSA, and SHA-1. The document serves as a comprehensive guide for understanding and implementing these cryptographic methods.

Uploaded by

shobhitcr708
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)
2 views

kapil

The document outlines various practical exercises conducted by a student named Kapil Sharma at Krishna Engineering College, focusing on different encryption algorithms and techniques in programming. Each practical includes code snippets in C or Java, inputs, and expected outputs for algorithms such as XOR, Caesar Cipher, DES, Blowfish, AES, RSA, and SHA-1. The document serves as a comprehensive guide for understanding and implementing these cryptographic methods.

Uploaded by

shobhitcr708
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/ 14

KRISHNA ENGINEERING COLLEGE

(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML

Practical no:- 01
Name of the practical:- XOR Each Character of a String in C.
Student Roll no:- 2101611530028
Student Name:- Kapil Sharma

#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello world";
int i;
for(i = 0; i < strlen(str); i++) {
str[i] = str[i] ^ 0; // XOR with 0
}
printf("Resultant String: %s\n", str);
return 0;
}

Input: "Hello world"


Output: Hello world
(Since XOR with 0 leaves the characters unchanged.)

Faculty remark &signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML

Practical no:- 02
Name of the practical:- AND & XOR Each Character of a String in C
Student Roll no:- 2101611530028
Student Name:- Kapil Sharma

#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello world";
int i;
for(i = 0; i < strlen(str); i++) {
str[i] = (str[i] & 127) ^ 127; // AND with 127 and XOR with 127
}
printf("Resultant String: %s\n", str);
return 0;
}

Input: "Hello world"


Output: Non-printable characters due to (str[i] & 127) ^ 127. The string will
appear scrambled
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML

Practical no:- 03
Name of the practical:- Java Program for Caesar, Substitution, and Hill Cipher.
Student Roll no:- 2101611530028
Student Name:- Kapil Sharma

a. Caesar Cipher

public class CaesarCipher {


public static String encrypt(String text, int shift) {
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isLetter(c)) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
result.append((char) ((c - base + shift) % 26 + base));
} else {
result.append(c);
}
}
return result.toString();
}

public static void main(String[] args) {


String text = "Hello World";
int shift = 3;
System.out.println("Encrypted Text: " + encrypt(text, shift));
}
}

Input: "Hello World", Shift = 3


Output: Khoor Zruog

b. Substitution Cipher

public class SubstitutionCipher {


public static String encrypt(String text, String key) {
StringBuilder result = new StringBuilder();
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
text = text.toUpperCase();

for (char c : text.toCharArray()) {


if (alphabet.contains(String.valueOf(c))) {
result.append(key.charAt(alphabet.indexOf(c)));
} else {
result.append(c);
}
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML
}
return result.toString();
}

public static void main(String[] args) {


String key = "QWERTYUIOPASDFGHJKLZXCVBNM";
String text = "HELLO";
System.out.println("Encrypted Text: " + encrypt(text, key));
}
}

Input: "HELLO" with key "QWERTYUIOPASDFGHJKLZXCVBNM"


Output: ITSSG

c.Hill Cipher (Matrix-based Cipher)

import java.util.Scanner;

public class HillCipher {


static int[][] keyMatrix;
static int[] messageVector;
static int[] cipherMatrix;
static int N;

public static void getKeyMatrix(String key, int N) {


int k = 0;
keyMatrix = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
keyMatrix[i][j] = key.charAt(k) % 65;
k++;
}
}
}

public static void encrypt(String message, int N) {


messageVector = new int[N];
cipherMatrix = new int[N];

for (int i = 0; i < N; i++) {


messageVector[i] = message.charAt(i) % 65;
}

for (int i = 0; i < N; i++) {


cipherMatrix[i] = 0;
for (int j = 0; j < N; j++) {
cipherMatrix[i] += keyMatrix[i][j] * messageVector[j];
}
cipherMatrix[i] %= 26;
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter 2x2 Key (4 uppercase letters): ");
String key = sc.next().toUpperCase();

System.out.print("Enter 2-letter message: ");


String message = sc.next().toUpperCase();

N = 2; // Matrix dimension
getKeyMatrix(key, N);
encrypt(message, N);

System.out.print("Encrypted Message: ");


for (int i = 0; i < N; i++) {
System.out.print((char) (cipherMatrix[i] + 65));
}
}
}

• Input:

• Key: GYBN
• Message: HI

• Output:
Encrypted Message: FN

Faculty remark &signature


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML

Practical no:- 04
Name of the practical:- DES Algorithm Logic (Java)
Student Roll no:- 2101611530028
Student Name:- Kapil Sharma

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class DESExample {


public static void main(String[] args) throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecretKey secretKey = keyGen.generateKey();

Cipher cipher = Cipher.getInstance("DES");


cipher.init(Cipher.ENCRYPT_MODE, secretKey);
String text = "Hello World";
byte[] encrypted = cipher.doFinal(text.getBytes());

System.out.println("Encrypted Data: " + new String(encrypted));


}
}

Input: "Hello World"


Output: Encrypted binary data (not human-readable).
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML

Practical no:- 05
Name of the practical:- Blowfish Algorithm Logic (C/Java)
Student Roll no:- 2101611530028
Student Name:- Kapil Sharma

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class BlowfishExample {


public static void main(String[] args) throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("Blowfish");
SecretKey key = keyGen.generateKey();
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, key);

String text = "Hello World";


byte[] encrypted = cipher.doFinal(text.getBytes());
System.out.println("Encrypted Data: " + new String(encrypted));
}
}

Input: "Hello World"


Output: Encrypted binary data (not human-readable).
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML
Practical no:- 06
Name of the practical:- Rijndael Algorithm (AES) Logic in Java.
Student Roll no:- 2101611530028
Student Name:- Kapil Sharma

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class AESExample {


public static void main(String[] args) throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
SecretKey key = keyGen.generateKey();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);

String text = "Hello World";


byte[] encrypted = cipher.doFinal(text.getBytes());
System.out.println("Encrypted Data: " + new String(encrypted));
}
}

Input: "Hello World"


Output: Encrypted binary data (not human-readable).
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML

Practical no:- 07
Name of the practical:- RC4 Logic in Java
Student Roll no:- 2101611530028
Student Name:- Kapil Sharma

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class RC4Example {


public static void main(String[] args) throws Exception {
String text = "Hello World";
String key = "secretkey";

SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "RC4");


Cipher cipher = Cipher.getInstance("RC4");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(text.getBytes());
System.out.println("Encrypted Data: " + new String(encrypted));
}
}

Input: "Hello World", Key = "secretkey"


Output: Encrypted binary data.
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML

Practical no:- 08
Name of the practical:- RSA Algorithm (Java)
Student Roll no:- 2101611530028
Student Name:- Kapil Sharma

import java.security.*;
import javax.crypto.Cipher;

public class RSAExample {


public static void main(String[] args) throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair pair = keyGen.generateKeyPair();

Cipher cipher = Cipher.getInstance("RSA");


cipher.init(Cipher.ENCRYPT_MODE, pair.getPublic());

String text = "Hello World";


byte[] encrypted = cipher.doFinal(text.getBytes());
System.out.println("Encrypted Data: " + new String(encrypted));
}
}

Input: "Hello World"


Output: Encrypted binary data (public key encryption).
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML
Practical no:- 09
Name of the practical:- Diffie-Hellman Key Exchange (HTML/JS)
Student Roll no:- 2101611530028
Student Name:- Kapil Sharma

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Diffie-Hellman Key Exchange</title>
</head>
<body>
<h1>Diffie-Hellman Key Exchange</h1>

<div>
<label for="prime">Prime number (p): </label>
<input type="number" id="prime" value="23">
</div>
<div>
<label for="base">Base (g): </label>
<input type="number" id="base" value="5">
</div>
<div>
<label for="private-key">Your private key (a/b): </label>
<input type="number" id="private-key" value="6">
</div>
<div>
<button onclick="performKeyExchange()">Compute Shared Key</button>
</div>

<div>
<h2>Results:</h2>
<p id="shared-key"></p>
</div>

<script>
function performKeyExchange() {
// Getting values from the inputs
const p = parseInt(document.getElementById("prime").value); // prime number
const g = parseInt(document.getElementById("base").value); // base
const privateKey = parseInt(document.getElementById("private-key").value); //
private key

// Step 1: Compute public key for A (Alice)


const publicKeyA = Math.pow(g, privateKey) % p;

// Step 2: Compute public key for B (Bob) with different private key
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML
const privateKeyB = 15; // You can set this manually or as a random number
const publicKeyB = Math.pow(g, privateKeyB) % p;

// Step 3: Compute shared keys using the other's public key and private key
const sharedKeyA = Math.pow(publicKeyB, privateKey) % p; // Alice computes using
Bob's public key
const sharedKeyB = Math.pow(publicKeyA, privateKeyB) % p; // Bob computes using
Alice's public key

// Display the results


if (sharedKeyA === sharedKeyB) {
document.getElementById("shared-key").innerHTML = `Shared key is:
${sharedKeyA}`;
} else {
document.getElementById("shared-key").innerHTML = "Something went wrong.";
}
}
</script>
</body>
</html>

Input:

• Prime number (p): 23


• Base (g): 5
• Private key (a for Alice): 6
• Private key (b for Bob): 15 (set in the script, but can also be user-input)

Output:

Shared key is: 2


KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML

Practical no:- 10
Name of the practical:- SHA-1 Message Digest in Java
Student Roll no:- 2101611530028
Student Name:- Kapil Sharma

import java.security.MessageDigest;

public class SHA1Example {


public static void main(String[] args) throws Exception {
String text = "Hello World";
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digest = md.digest(text.getBytes());

for (byte b : digest) {


System.out.printf("%02x", b);
}
}
}

Input: "Hello World"


Output: 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
KRISHNA ENGINEERING COLLEGE
(Approved by AICTE & Affiliated to Dr. APJ Abdul Kalam Technical University (Formerly UPTU), Lucknow)

Department of AI & ML

Practical no:- 11
Name of the practical:- MD5 Message Digest in Java
Student Roll no:- 2101611530028
Student Name:- Kapil Sharma

import java.security.MessageDigest;

public class MD5Example {


public static void main(String[] args) throws Exception {
String text = "Hello World";
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(text.getBytes());

for (byte b : digest) {


System.out.printf("%02x", b);
}
}
}

Input: "Hello World"


Output: 5eb63bbbe01eeed093cb22bb8f5acdc3

You might also like