In: Computer Science
Flow chart and IPO chart ( no handwritten please )
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
char vs[26][26];
void encrypt(char input[], char key[], char cipher[]) {
char inputWithoutSpaces[50], repeatedKeyWord[50], tempCipher[50];
int cnt = 0, i = 0, keyLen = strlen(key), len3 = strlen(input);
// loop to remove the spaces from input & store in other
char array inputWithoutSpaces
for (i = 0; i < len3; ++i) {
if (input[i] != ' ') {
inputWithoutSpaces[cnt++] = toupper(input[i]);
}
}
inputWithoutSpaces[cnt] = '\0';
int len1 = strlen(inputWithoutSpaces);
// loop to repeat the key until it becomes equal to the length
of input without spaces
i = 0;
while (i < len1) {
repeatedKeyWord[i] = toupper(key[i % keyLen]);
i++;
}
repeatedKeyWord[i] = '\0';
// form the temp cipher
for (i = 0; i < len1; ++i) {
tempCipher[i] = vs[inputWithoutSpaces[i] % 'A'][repeatedKeyWord[i]
% 'A'];
}
tempCipher[i] = '\0';
// modify the passed cipher by adding spaces in temp cipher
wherever needed
cnt = 0;
for (i = 0; i < len3; ++i) {
if (input[i] == ' ')
cipher[i] = ' ';
else
cipher[i] = tempCipher[cnt++];
}
cipher[i] = '\0';
}
void decrypt(char cipher[], char key[], char plaintext[]) {
printf("%s\n", key);
char cipherWithoutSpaces[50], repeatedKeyWord[50],
tempPlainText[50];
int cnt = 0, i = 0, keyLen = strlen(key), len3 = strlen(cipher);
// loop to remove the spaces from cipher & store in other
char array cipherWithoutSpaces
for (i = 0; i < len3; ++i) {
if (cipher[i] != ' ') {
cipherWithoutSpaces[cnt++] = toupper(cipher[i]);
}
}
cipherWithoutSpaces[cnt] = '\0';
int len1 = strlen(cipherWithoutSpaces);
// loop to repeat the key until it becomes equal to the length
of input without spaces
i = 0;
while (i < len1) {
repeatedKeyWord[i] = toupper(key[i % keyLen]);
i++;
}
repeatedKeyWord[i] = '\0';
// form the temp plaintext
cnt = 0;
for (i = 0; i < len1; ++i) {
for (int j = 0; j < 26; ++j) {
if (vs[repeatedKeyWord[i] % 'A'][j] ==
cipherWithoutSpaces[i])
tempPlainText[cnt++] = 'A' + j;
}
}
tempPlainText[cnt] = '\0';
// modify the passed plaintext by adding spaces in temp
plaintext wherever needed
cnt = 0;
for (i = 0; i < len3; ++i) {
if (cipher[i] == ' ')
plaintext[i] = ' ';
else
plaintext[i] = tempPlainText[cnt++];
}
plaintext[i] = '\0';
}
int main(void) {
char selection;
char plaintext[50], cipher[50], key[10];
// making the table
for (int i = 0; i < 26; ++i)
for (int j = 0; j < 26; ++j)
vs[i][j] = ('A' + ((i + j) % 26));
// taking first selection
fflush(stdin);
printf("Enter e for encryption, d for decryption, x for exit:
");
scanf("%c", &selection);
// run loop till user enters 'x'
while (selection != 'x') {
// condition for encryption
if (selection == 'e') {
// taking plaintext
fflush(stdin);
printf("Enter the plaintext: ");
scanf("%50[a-zA-Z ]", plaintext);
// taking key
fflush(stdin);
printf("Enter the key: ");
scanf("%50[a-zA-Z ]", key);
// encrypting and displaying cipher
encrypt(plaintext, key, cipher);
fflush(stdin);
printf("Cipher text: %s\n\n", cipher);
}
else {
// taking encrypted text
fflush(stdin);
printf("Enter the encrpted text: ");
scanf("%50[a-zA-Z ]", cipher);
// taking key
fflush(stdin);
printf("Enter the key: ");
scanf("%50[a-zA-Z ]", key);
// decrypting and displaying plain text
decrypt(cipher, key, plaintext);
fflush(stdin);
printf("Plain Text: %s\n\n", plaintext);
}
fflush(stdin);
printf("Enter e for encryption, d for decryption, x for exit:
");
scanf("%c", &selection);
}
return 0;
}
Encrypt: based on the encrypt logic we can encrypt the text.
Decrypt: based on the decrypt logic, decode the text.
working code is given below.
please reply if you need more information.
//Flow chart and IPO chart ( no handwritten please)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
char vs[26][26];
void encrypt(char input[], char key[], char cipher[])
{
char inputWithoutSpaces[50], repeatedKeyWord[50],
tempCipher[50];
int cnt = 0;
int i = 0, keyLen = strlen(key), len3 = strlen(input);
int len1;
// loop to remove the spaces from input & store in other
char array inputWithoutSpaces
for (i = 0; i < len3; ++i)
{
if (input[i] != ' ')
{
inputWithoutSpaces[cnt++] = toupper(input[i]);
}
}
inputWithoutSpaces[cnt] = '\0';
len1 = strlen(inputWithoutSpaces);
// loop to repeat the key until it becomes equal to the length of
input without spaces
i = 0;
while (i < len1)
{
repeatedKeyWord[i] = toupper(key[i % keyLen]);
i++;
}
repeatedKeyWord[i] = '\0';
// form the temp cipher
for (i = 0; i < len1; ++i)
{
tempCipher[i] = vs[inputWithoutSpaces[i] % 'A'][repeatedKeyWord[i]
% 'A'];
}
tempCipher[i] = '\0';
// modify the passed cipher by adding spaces in temp cipher
wherever needed
cnt = 0;
for (i = 0; i < len3; ++i)
{
if (input[i] == ' ')
cipher[i] = (' ');
else
cipher[i] = tempCipher[cnt++];
}
cipher[i] = '\0';
}//encrypt
void decrypt(char cipher[], char key[], char plaintext[])
{
char cipherWithoutSpaces[50], repeatedKeyWord[50],
tempPlainText[50];
int cnt = 0,i = 0,keyLen = strlen(key),len3 =
strlen(cipher),len1,j;
printf("%s\n", key);
// loop to remove the spaces from cipher & store in other char
array cipherWithoutSpaces
for (i = 0; i < len3; ++i)
{
if (cipher[i] != ' ')
{
cipherWithoutSpaces[cnt++] = toupper(cipher[i]);
}
}
cipherWithoutSpaces[cnt] = '\0';
len1 = strlen(cipherWithoutSpaces);
// loop to repeat the key until it becomes equal to the length
of input without spaces
i = 0;
while (i < len1)
{
repeatedKeyWord[i] = toupper(key[i % keyLen]);
i++;
}
repeatedKeyWord[i] = '\0';
// form the temp plaintext
cnt = 0;
for (i = 0; i < len1; ++i)
{
for (j = 0; j < 26; ++j)
{
if (vs[repeatedKeyWord[i] % 'A'][j] ==
cipherWithoutSpaces[i])
tempPlainText[cnt++] = ('A' + j);
}
}
tempPlainText[cnt] = '\0';
// modify the passed plaintext by adding spaces in temp
plaintext wherever needed
cnt = 0;
for (i = 0; i < len3; ++i)
{
if (cipher[i] == ' ')
plaintext[i] =(' ');
else
plaintext[i] = tempPlainText[cnt++];
}
plaintext[i] = '\0';
} //decrypt
int main(void)
{
char selection;
char plaintext[50], cipher[50], key[10];
int i,j;
//making the table
for (i = 0; i < 26; ++i)
{
for (j = 0; j < 26; ++j)
{
vs[i][j] = ('A' + ((i + j) % 26));
}
}
// taking first selection
fflush(stdin);
printf("Enter e for encryption, d for decryption, x for exit:
");
scanf("%c", &selection);
// run loop till user enters 'x'
while (selection != 'x')
{
// condition for encryption
if (selection == 'e')
{
// taking plaintext
fflush(stdin);
printf("Enter the plaintext: ");
scanf("%50[a-zA-Z ]", plaintext);
// taking key
fflush(stdin);
printf("Enter the key: ");
scanf("%50[a-zA-Z ]", key);
// encrypting and displaying cipher
encrypt(plaintext, key, cipher);
fflush(stdin);
printf("Cipher text: %s\n\n", cipher);
}
else
{
// taking encrypted text
fflush(stdin);
printf("Enter the encrpted text: ");
scanf("%50[a-zA-Z ]", cipher);
// taking key
fflush(stdin);
printf("Enter the key: ");
scanf("%50[a-zA-Z ]", key);
// decrypting and displaying plain text
decrypt(cipher, key, plaintext);
fflush(stdin);
printf("Plain Text: %s\n\n", plaintext);
}
fflush(stdin);
printf("Enter e for encryption, d for decryption, x for exit:
");
scanf("%c", &selection);
}
return 0;
}