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

23MIP10075_CSD3005_LABFILE

The document is a lab assessment for an Integrated M-Tech program in Data Privacy, submitted by Bandi Naga Sandeep. It includes an index of various encryption algorithms such as Blowfish, IDEA, RC5, DES, AES, and SEAL, along with code implementations for each. Additionally, it features scripts for classic ciphers like Caesar, Vigenere, and Auto Key ciphers.

Uploaded by

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

23MIP10075_CSD3005_LABFILE

The document is a lab assessment for an Integrated M-Tech program in Data Privacy, submitted by Bandi Naga Sandeep. It includes an index of various encryption algorithms such as Blowfish, IDEA, RC5, DES, AES, and SEAL, along with code implementations for each. Additionally, it features scripts for classic ciphers like Caesar, Vigenere, and Auto Key ciphers.

Uploaded by

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

LAB-ASSESMENT

Programme: Integrated M-Tech


CSE(Computational and Data science)

Submitted by: Bandi Naga Sandeep.


Subject: Data Privacy.
Registration No: 23MIP10075.
Submitted to: Dr. Sajjad Ahmed.
Slot: B21 + E14
Class Number: BL202450500740

1
INDEX

S No. Name of Date of Date of Page Remarks


Experiment Experiment Submission Nos.
1 Blowfish 1/03/2025 17/04/2025 3
Algorithm
2 IDEA 5/03/2025 17/04/2025 4 to 7
Algorithm
3 RC5 9/03/2025 17/04/2025 8 to 11
Algorithm
4 DES 14/03/2025 17/04/2025 12
Algorithm
5 AES 19/03/2025 17/04/2025 13
Algorithm
6 SEAL 22/03/2025 17/04/2025 14
Algorithm
7 Write a 25/03/2025 17/04/2025 16
program/script to
implement Ceaser
cipher
8 Write a 17/04/2025 17 to 18
program/script to
implement simple
substitution cipher
9 Write a 17/04/2025 19 to 20
program/script to
implement
Venegere cipher

2
10 Write a 17/04/2025 21 to 22
program/script to
implement auto key
cipher (Vernum
cipher)

1. Blowfish

Algorithm Overview:

● Symmetric block cipher.

● Block size: 64 bits.

● Key length: 32 to 448 bits.

● Uses 16 rounds of Feistel structure.

Code :
from Crypto.Cipher import Blowfish
from Crypto.Util.Padding import pad, unpad

key = b'secretkey'
cipher = Blowfish.new(key, Blowfish.MODE_ECB)

data = b"Hello World!"


padded_data = pad(data, Blowfish.block_size)
encrypted = cipher.encrypt(padded_data)

decipher = Blowfish.new(key, Blowfish.MODE_ECB)


decrypted = unpad(decipher.decrypt(encrypted), Blowfish.block_size)

3
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)

Output:

2.

IDEA (International Data Encryption Algorithm)

Algorithm Overview:

● Block cipher using 64-bit blocks.

● Key size: 128 bits.

● 8.5 rounds of substitution and permutation.

Code :
#include <stdio.h>
#include <stdint.h>

#define NUM_ROUNDS 8

// IDEA requires modulo operations


uint16_t mul(uint16_t a, uint16_t b) {
uint32_t p;
if (a == 0) a = 0x10000;
if (b == 0) b = 0x10000;
p = (uint32_t)a * b;
if (p != 0) {
p = (p & 0xFFFF) - (p >> 16);

4
if ((int32_t)p <= 0) p += 0x10001;
} else {
p = 1 - a - b;
}
return (uint16_t)p;
}

uint16_t add(uint16_t a, uint16_t b) {


return (a + b) & 0xFFFF;
}

// Encrypt one 64-bit block


void idea_encrypt(uint16_t plaintext[4], uint16_t ciphertext[4], uint16_t key[6 *
NUM_ROUNDS + 4]) {
int i;
uint16_t x1 = plaintext[0];
uint16_t x2 = plaintext[1];
uint16_t x3 = plaintext[2];
uint16_t x4 = plaintext[3];
uint16_t t1, t2, k;

for (i = 0; i < NUM_ROUNDS; i++) {


x1 = mul(x1, key[i * 6 + 0]);
x2 = add(x2, key[i * 6 + 1]);
x3 = add(x3, key[i * 6 + 2]);
x4 = mul(x4, key[i * 6 + 3]);

t1 = x1 ^ x3;

5
t2 = x2 ^ x4;

t1 = mul(t1, key[i * 6 + 4]);


t2 = add(t2, t1);
t2 = mul(t2, key[i * 6 + 5]);
t1 = add(t1, t2);

x1 ^= t2;
x4 ^= t1;
k = x2 ^ t1;
x2 = x3 ^ t2;
x3 = k;
}

ciphertext[0] = mul(x1, key[48]);


ciphertext[1] = add(x3, key[49]);
ciphertext[2] = add(x2, key[50]);
ciphertext[3] = mul(x4, key[51]);
}

// Example 128-bit key schedule (real implementation requires full subkey


generation)
void dummy_key_schedule(uint16_t key[52]) {
for (int i = 0; i < 52; i++) {
key[i] = (uint16_t)(0x100 + i); // Dummy values
}
}

6
int main() {
uint16_t plaintext[4] = {0x1234, 0x5678, 0x9ABC, 0xDEF0};
uint16_t ciphertext[4];
uint16_t key[52];

dummy_key_schedule(key);
idea_encrypt(plaintext, ciphertext, key);

printf("Encrypted: ");
for (int i = 0; i < 4; i++) {
printf("%04X ", ciphertext[i]);
}
printf("\n");

return 0;
}Output:

7
3. RC5

Algorithm Overview:

● Variable block size (usually 64 bits), key size, and number of rounds.

● Uses data-dependent rotations and modular addition.

Python Code: Python doesn't have built-in RC5 support. You can use the rc5
module if available or third-party libraries.

Code :
#include <stdio.h>
#include <stdint.h>

#define W 32 // Word size in bits


#define R 12 // Number of rounds
#define KEYLEN 16 // Key length in bytes
#define C (KEYLEN / 4) // Number of words in the key
#define T (2 * (R + 1)) // Number of subkeys

uint32_t S[T]; // Subkeys array

// Constants for RC5


const uint32_t P = 0xB7E15163;

8
const uint32_t Q = 0x9E3779B9;

// Left rotation
uint32_t ROTL(uint32_t x, uint32_t y) {
return (x << (y & (W - 1))) | (x >> (W - (y & (W - 1))));
}

// RC5 key schedule


void rc5_key_schedule(uint8_t K[KEYLEN]) {
uint32_t L[C] = {0};

// Convert key to words L[0..C-1]


for (int i = KEYLEN - 1; i >= 0; i--) {
L[i / 4] = (L[i / 4] << 8) + K[i];
}

// Initialize subkeys
S[0] = P;
for (int i = 1; i < T; i++) {
S[i] = S[i - 1] + Q;
}

// Mix key into S[]


uint32_t A = 0, B = 0;
int i = 0, j = 0;
for (int k = 0; k < 3 * T; k++) {
A = S[i] = ROTL(S[i] + A + B, 3);

9
B = L[j] = ROTL(L[j] + A + B, A + B);
i = (i + 1) % T;
j = (j + 1) % C;
}
}

// Encrypt one 64-bit block (two 32-bit words)


void rc5_encrypt(uint32_t *pt, uint32_t *ct) {
uint32_t A = pt[0] + S[0];
uint32_t B = pt[1] + S[1];

for (int i = 1; i <= R; i++) {


A = ROTL(A ^ B, B) + S[2 * i];
B = ROTL(B ^ A, A) + S[2 * i + 1];
}

ct[0] = A;
ct[1] = B;
}

int main() {
// Sample 128-bit key
uint8_t key[KEYLEN] = {
0x91, 0x5F, 0x46, 0x19, 0xBE, 0x41, 0xB2, 0x51,
0x63, 0x55, 0xA5, 0x01, 0x10, 0xA9, 0xCE, 0x91
};

10
uint32_t plaintext[2] = {0x12345678, 0x9ABCDEF0};
uint32_t ciphertext[2];

rc5_key_schedule(key);
rc5_encrypt(plaintext, ciphertext);

printf("Plaintext : %08X %08X\n", plaintext[0], plaintext[1]);


printf("Encrypted : %08X %08X\n", ciphertext[0], ciphertext[1]);

return 0;
}
Output:

11
4. DES (Data Encryption Standard)

Algorithm Overview:

● 64-bit block cipher with 56-bit key.

● 16 rounds of Feistel structure.

Code:
from Crypto.Cipher import DES
from Crypto.Util.Padding import pad, unpad

key = b'8bytekey'
cipher = DES.new(key, DES.MODE_ECB)

data = b"SecretMsg"
padded_data = pad(data, DES.block_size)
encrypted = cipher.encrypt(padded_data)

decipher = DES.new(key, DES.MODE_ECB)


decrypted = unpad(decipher.decrypt(encrypted), DES.block_size)

print("Encrypted:", encrypted)
print("Decrypted:", decrypted)

12
output:

5. AES (Advanced Encryption Standard)

Algorithm Overview:

● Block size: 128 bits.

● Key sizes: 128, 192, or 256 bits.

● 10/12/14 rounds depending on key size.

Code:
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

key = b'0123456789abcdef'
cipher = AES.new(key, AES.MODE_ECB)

data = b"Secret AES Data"


padded_data = pad(data, AES.block_size)
encrypted = cipher.encrypt(padded_data)

decipher = AES.new(key, AES.MODE_ECB)


decrypted = unpad(decipher.decrypt(encrypted), AES.block_size)

print("Encrypted:", encrypted)
print("Decrypted:", decrypted)
13
Output:

6. SEAL (Software-Optimized Encryption Algorithm)

Algorithm Overview:

● Stream cipher designed for high-speed software encryption.

● Key size: 160 bits.

● Used mainly for encrypting large volumes of data.

# SEAL is rarely implemented; here's a conceptual placeholder


def seal_encrypt(data, key):
return bytes([b ^ key[i % len(key)] for i, b in enumerate(data)])

def seal_decrypt(encrypted, key):


return seal_encrypt(encrypted, key) # XOR reversibility

key = b'sealsecretkey123'
data = b'SEAL demo data'
encrypted = seal_encrypt(data, key)
decrypted = seal_decrypt(encrypted, key)

print("Encrypted:", encrypted)
print("Decrypted:", decrypted)

14
Output :

7.Write a program/script to implement Ceaser cipher.

15
Code:
def caesar_cipher_encrypt(plaintext, shift):
ciphertext = ""
for char in plaintext:
if char.isalpha():
shift_base = 65 if char.isupper() else 97
ciphertext += chr((ord(char) - shift_base + shift) % 26 + shift_base)
else:
ciphertext += char
return ciphertext

def caesar_cipher_decrypt(ciphertext, shift):


return caesar_cipher_encrypt(ciphertext, -shift)

# Example
text = "HELLO WORLD"
shift = 3
encrypted = caesar_cipher_encrypt(text, shift)
decrypted = caesar_cipher_decrypt(encrypted, shift)

print("Encrypted:", encrypted)
print("Decrypted:", decrypted)
Output:

8.Write a program/script to implement simple substitution cipher

16
Code:
def generate_vigenere_key(text, key):
key = list(key.upper())
if len(key) == len(text):
return key
else:
for i in range(len(text) - len(key)):
key.append(key[i % len(key)])
return "".join(key)

def vigenere_encrypt(plaintext, key):


key = generate_vigenere_key(plaintext, key)
ciphertext = ""
for p, k in zip(plaintext.upper(), key):
if p.isalpha():
c = chr(((ord(p) + ord(k)) % 26) + 65)
ciphertext += c
else:
ciphertext += p
return ciphertext

def vigenere_decrypt(ciphertext, key):


key = generate_vigenere_key(ciphertext, key)
plaintext = ""
for c, k in zip(ciphertext, key):
if c.isalpha():
p = chr(((ord(c) - ord(k) + 26) % 26) + 65)

17
plaintext += p
else:
plaintext += c
return plaintext

# Example
text = "ATTACKATDAWN"
key = "LEMON"
encrypted = vigenere_encrypt(text, key)
decrypted = vigenere_decrypt(encrypted, key)

print("Encrypted:", encrypted)
print("Decrypted:", decrypted)
Output:

9.Write a program/script to implement Venegere cipher

18
Code:
def generate_vigenere_key(text, key):
key = list(key.upper())
if len(key) == len(text):
return key
else:
for i in range(len(text) - len(key)):
key.append(key[i % len(key)])
return "".join(key)

def vigenere_encrypt(plaintext, key):


key = generate_vigenere_key(plaintext, key)
ciphertext = ""
for p, k in zip(plaintext.upper(), key):
if p.isalpha():
c = chr(((ord(p) + ord(k)) % 26) + 65)
ciphertext += c
else:
ciphertext += p
return ciphertext

def vigenere_decrypt(ciphertext, key):


key = generate_vigenere_key(ciphertext, key)
plaintext = ""
for c, k in zip(ciphertext, key):
if c.isalpha():
p = chr(((ord(c) - ord(k) + 26) % 26) + 65)

19
plaintext += p
else:
plaintext += c
return plaintext

# Example
text = "ATTACKATDAWN"
key = "LEMON"
encrypted = vigenere_encrypt(text, key)
decrypted = vigenere_decrypt(encrypted, key)

print("Encrypted:", encrypted)
print("Decrypted:", decrypted)
Output:

10.Write a program/script to implement auto key cipher (Vernum cipher)

20
Code:
def autokey_encrypt(plaintext, key):
key = key.upper() + plaintext.upper()
key = key[:len(plaintext)]
ciphertext = ""
for p, k in zip(plaintext.upper(), key):
if p.isalpha():
c = chr(((ord(p) + ord(k)) % 26) + 65)
ciphertext += c
else:
ciphertext += p
return ciphertext

def autokey_decrypt(ciphertext, key):


key = key.upper()
plaintext = ""
for c in ciphertext.upper():
if c.isalpha():
p = chr(((ord(c) - ord(key[0]) + 26) % 26) + 65)
plaintext += p
key += p # Autokey: append decrypted letter
key = key[1:] # Slide key
else:
plaintext += c
return plaintext

# Example

21
text = "ATTACKATDAWN"
key = "QUEEN"
encrypted = autokey_encrypt(text, key)
decrypted = autokey_decrypt(encrypted, key)

print("Encrypted:", encrypted)
print("Decrypted:", decrypted)

Output:

22

You might also like