Beruflich Dokumente
Kultur Dokumente
Lab report
Problem description
b. Vigenere Cipher
Program:
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int choice;
string text, key;
cout << "Enter the text: ";
getline(cin, text);
cout << "\nEnter the key: ";
cin >> key;
cout << "\nEnter 0 for Encrypt and 1 for Decrypt: " << endl;
cin >> choice;
switch(choice){
case 0:
cout << encrypt(key, text) << endl;
break;
case 1:
cout << decrypt(key, text) << endl;
break;
default:
cout << "Wrong choice" << endl;
}
}
Sample runs:
Problem Statement:
Program:
def pTen(s):
return s[2] + s[4] + s[1] + s[6] + s[3] + s[9] + s[0] + s[8] + s[7] + s[5]
def pEight(s):
return s[5] + s[2] + s[6] + s[3] + s[7] + s[4] + s[9] + s[8]
def pFour(s):
return s[1] + s[3] + s[2] + s[0]
def leftShift1(s):
return(s[1:] + s[0])
def leftShift2(s):
return(s[2:] + s[0] + s[1])
def IP(s):
return(s[1] + s[5] + s[2] + s[0] + s[3] + s[7] + s[4] + s[6])
def invIP(s):
return(s[3] + s[0] + s[2] + s[4] + s[6] + s[1] + s[7] + s[5])
def EP(s):
return(s[3] + s[0] + s[1] + s[2] + s[1] + s[2] + s[3] + s[0])
def xor(s1, s2):
s = ''
for i in range(len(s1)):
s = s + str(int(s1[i]) ^ int(s2[i]))
return s
def s0(s):
l = [[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]]
row = int(s[0] + s[3], 2)
col = int(s[1] + s[2], 2)
temp = l[row][col]
temp = bin(temp)[2:]
if len(temp) == 1:
temp = '0' + temp
return temp
def s1(s):
l = [[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]]
row = int(s[0] + s[3], 2)
col = int(s[1] + s[2], 2)
temp = l[row][col]
temp = bin(temp)[2:]
if len(temp) == 1:
temp = '0' + temp
return temp
def swap(s):
return(s[4:] + s[:4])
def keyGeneration(tenBitKey):
tenBitKey = pTen(tenBitKey)
temp = leftShift1(tenBitKey[:5]) + leftShift1(tenBitKey[5:])
key1 = pEight(temp)
temp = leftShift2(temp[:5]) + leftShift2(temp[5:])
key2 = pEight(temp)
return([key1, key2])
Sereen shots:
(Worked out example in theory class)