0% found this document useful (0 votes)
126 views3 pages

Practical: 4: Aim: Implement Poly Alphabetic Cipher Encryption Decryption

This document describes an implementation of a polyalphabetic cipher for encryption and decryption. It includes functions to generate a cyclic key based on the plaintext and given key, encrypt the plaintext into ciphertext using the generated key, and decrypt the ciphertext back to the original plaintext. The code takes a plaintext and key as input, generates a new key of the same length as the plaintext, encrypts it using a Caesar cipher-like encryption method with the key, and prints the ciphertext. It then decrypts the ciphertext back to the original plaintext.

Uploaded by

hello
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)
126 views3 pages

Practical: 4: Aim: Implement Poly Alphabetic Cipher Encryption Decryption

This document describes an implementation of a polyalphabetic cipher for encryption and decryption. It includes functions to generate a cyclic key based on the plaintext and given key, encrypt the plaintext into ciphertext using the generated key, and decrypt the ciphertext back to the original plaintext. The code takes a plaintext and key as input, generates a new key of the same length as the plaintext, encrypts it using a Caesar cipher-like encryption method with the key, and prints the ciphertext. It then decrypts the ciphertext back to the original plaintext.

Uploaded by

hello
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/ 3

[Information and Network Security | 2170709]

Practical: 4
Aim: Implement poly alphabetic cipher Encryption Decryption.

CODE:

#include<bits/stdc++.h>
using namespace std;

// This function generates the key in


// a cyclic manner until it's length isi'nt
// equal to the length of original text
string generateKey(string pt, string key)
{
int x = pt.size();
int y = key.size();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
//if (key.size() == pt.size())
if (x == y)
break;
key.push_back(key[i]);
}
return key;
}
// This function returns the encrypted text
// generated with the help of the key
string cipherText(string pt, string key)
{
string cipher_text;
for (int i = 0; i < pt.size(); i++)
{
// converting in range 0-25
int x = (pt[i] + key[i]) %26;
// convert into alphabets(ASCII)
x += 'U';
cipher_text.push_back(x);
}

AMAN PATEL (161040107001) 1


[Information and Network Security | 2170709]

return cipher_text;
}

// This function decrypts the encrypted text


// and returns the original text

string originalText(string cipher_text, string key)


{
string orig_text;
for (int i = 0 ; i < cipher_text.size(); i++)
{
// converting in range 0-25
int x = (cipher_text[i] - key[i] + 26) %26;
// convert into alphabets(ASCII)
x += 'a';
orig_text.push_back(x);
}
return orig_text;
}

// Driver program to test the above function


int main()
{
//string str = "GEEKSFORGEEKS";
//string keyword = "AYUSH";
int dtln,kln,sum;
char plaintext[100],key[100],cipher[100];
printf("Enter String : ");
gets(plaintext);
printf("Enter Key : ");
gets(key);
//dtln=strlen(plaintext);
//kln=strlen(key);
string key_new = generateKey(plaintext, key);
string cipher_text = cipherText(plaintext, key_new);

cout << "Ciphertext : "


<< cipher_text << "\n";
string orig_text = originalText(cipher_text, key_new);
cout << "Original/Decrypted Text : "<< orig_text;
return 0;
}

AMAN PATEL (161040107001) 2


[Information and Network Security | 2170709]

OUTPUT:

[Fig: 4.1]

[Fig: 4.2]

AMAN PATEL (161040107001) 3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy