Respuesta :
Answer:
C code is given below
Explanation:
// Vigenere cipher
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* Reading key file.
*/
char *readFile(char *fileName) {
FILE *file = fopen(fileName, "r");
char *code;
size_t n = 0;
int c;
if (file == NULL) return NULL; //could not open file
code = (char *)malloc(513);
while ((c = fgetc(file)) != EOF) {
if( !isalpha(c) )
continue;
if( isupper(c) )
c = tolower(c);
code[n++] = (char)c;
}
code[n] = '\0';
fclose(file);
return code;
}
int main(int argc, char ** argv){
// Check if correct # of arguments given
if (argc != 3) {
printf("Wrong number of arguments. Please try again.\n");
return 1;
}
// try to read the key file
char *key = readFile(argv[1]);
if( !key ) {
printf( "Invalid file %s\n", argv[1] );
return 1;
}
char *data = readFile(argv[2]);
if( !data ) {
printf("Invalid file %s\n", argv[2] );
return 1;
}
// Store key as string and get length
int kLen = strlen(key);
int dataLen = strlen( data );
printf("%s\n", key );
printf("%s\n", data );
int paddingLength = dataLen % kLen;
if( kLen > dataLen ) {
paddingLength = kLen - dataLen;
}
for( int i = 0; i < paddingLength && dataLen + paddingLength <= 512; i++ ) {
data[ dataLen + i ] = 'x';
}
dataLen += paddingLength;
// Loop through text
for (int i = 0, j = 0, n = dataLen; i < n; i++) {
// Get key for this letter
int letterKey = tolower(key[j % kLen]) - 'a';
// Keep case of letter
if (isupper(data[i])) {
// Get modulo number and add to appropriate case
printf("%c", 'A' + (data[i] - 'A' + letterKey) % 26);
// Only increment j when used
j++;
}
else if (islower(data[i])) {
printf("%c", 'a' + (data[i] - 'a' + letterKey) % 26);
j++;
}
else {
// return unchanged
printf("%c", data[i]);
}
if( (i+1) % 80 == 0 ) {
printf("\n");
}
}
printf("\n");
return 0;
}
Answer:
code is written in c++ below
Explanation:
// C++ code to implement Vigenere Cipher
#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 str, string key)
{
int x = str.size();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.size() == str.size())
break;
key.push_back(key[i]);
}
return key;
}
// This function returns the encrypted text
// generated with the help of the key
string cipherText(string str, string key)
{
string cipher_text;
for (int i = 0; i < str.size(); i++)
{
// converting in range 0-25
int x = (str[i] + key[i]) %26;
// convert into alphabets(ASCII)
x += 'A';
cipher_text.push_back(x);
}
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";
string key = generateKey(str, keyword);
string cipher_text = cipherText(str, key);
cout << "Ciphertext : "
<< cipher_text << "\n";
cout << "Original/Decrypted Text : "
<< originalText(cipher_text, key);
return 0;
}