23MIP10075_CSD3005_LABFILE
23MIP10075_CSD3005_LABFILE
1
INDEX
2
10 Write a 17/04/2025 21 to 22
program/script to
implement auto key
cipher (Vernum
cipher)
1. Blowfish
Algorithm Overview:
Code :
from Crypto.Cipher import Blowfish
from Crypto.Util.Padding import pad, unpad
key = b'secretkey'
cipher = Blowfish.new(key, Blowfish.MODE_ECB)
3
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)
Output:
2.
Algorithm Overview:
Code :
#include <stdio.h>
#include <stdint.h>
#define NUM_ROUNDS 8
4
if ((int32_t)p <= 0) p += 0x10001;
} else {
p = 1 - a - b;
}
return (uint16_t)p;
}
t1 = x1 ^ x3;
5
t2 = x2 ^ x4;
x1 ^= t2;
x4 ^= t1;
k = x2 ^ t1;
x2 = x3 ^ t2;
x3 = k;
}
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.
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>
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))));
}
// Initialize subkeys
S[0] = P;
for (int i = 1; i < T; i++) {
S[i] = S[i - 1] + Q;
}
9
B = L[j] = ROTL(L[j] + A + B, A + B);
i = (i + 1) % T;
j = (j + 1) % C;
}
}
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);
return 0;
}
Output:
11
4. DES (Data Encryption Standard)
Algorithm Overview:
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)
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)
12
output:
Algorithm Overview:
Code:
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
key = b'0123456789abcdef'
cipher = AES.new(key, AES.MODE_ECB)
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)
13
Output:
Algorithm Overview:
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 :
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
# 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:
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)
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:
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)
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:
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
# Example
21
text = "ATTACKATDAWN"
key = "QUEEN"
encrypted = autokey_encrypt(text, key)
decrypted = autokey_decrypt(encrypted, key)
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)
Output:
22