Sie sind auf Seite 1von 7

CSE1011: Cryptography Fundamentals, Winter 2017-18

Vellore Institute of Technology


Instructor: Prof MARIMUTHU K - SCOPE

Lab report

Title of Lab: Ciphers and sDES algorithms.


Experiment #: 1 and 2
Date: 29|08|2018

Author's name: Gagan Deep Singh


Registration ID: 17BCI0140
Lab section: Thursday L49+L50

Problem description

1. Write a program to implement Vigenere Cipher

b. Vigenere Cipher

Program:

#include <iostream>
#include <cstdio>
using namespace std;

string encrypt(string key, string text){


int keyLength = key.length(), countBlankspace = 0;
string cipher = "", temp = "";
text = text + ' ';
for(int i = 0; i < text.length(); i++){
if(text[i] != ' '){
temp = temp + (char)(((((int)text[i] - 97) + ((int)key[(i -
countBlankspace) % keyLength] - 97)) % 26) + 97);
}
else{
cipher = cipher + ' ' + temp;
temp = "";
countBlankspace++;
}
}
cipher = cipher.substr(1, cipher.length());
return cipher;
}

string decrypt(string key, string cipher){


int keyLength = key.length(), countBlankspace = 0;
string text = "", temp = "";
cipher = cipher + ' ';
for(int i = 0; i < cipher.length(); i++){
if(cipher[i] != ' '){
temp = temp + (char)(((((((int)cipher[i] - 97) - ((int)key[(i -
countBlankspace) % keyLength] - 97)) % 26) + 26) % 26) + 97);
}
else{
text = text + ' ' + temp;
temp = "";
countBlankspace++;
}
}
text = text.substr(1, text.length());
return text;

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:

2. Write a program to implement DES algorithm

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 fk(s, key):


temp = s[4:]
temp = EP(temp)
temp = xor(temp, key)
temp = s0(temp[:4]) + s1(temp[4:])
temp = pFour(temp)
temp = xor(s[:4], temp)
return temp + 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])

def encrypt(eightBitPlainText, key1, key2):


temp = IP(eightBitPlainText)
temp = fk(temp, key1)
temp = swap(temp)
temp = fk(temp, key2)
temp = invIP(temp)
return temp

def decrypt(eightBitCipherText, key1, key2):


temp = IP(eightBitCipherText)
#print(temp)
temp = fk(temp, key2)
#print(temp)
temp = swap(temp)
#print(temp)
temp = fk(temp, key1)
temp = invIP(temp)
return temp

eightBitPlainText = input("Enter the eight bit plain text: ")


tenBitKey = input("Enter the ten bit key: ")
key1, key2 = keyGeneration(tenBitKey)
print(" Encrypted text is: ", end = '')
cipher = encrypt(eightBitPlainText, key1, key2)
print(cipher)

print(" Decrypted text is: ", end = '')


print(decrypt(cipher, key1, key2))

Sereen shots:
(Worked out example in theory class)

Das könnte Ihnen auch gefallen