Question

C Programming I have this function to i want to return the cipher text, but its...

C Programming

I have this function to i want to return the cipher text, but its not working, can anyone try to see what i'm doing wrong.

I need it to return the cipher text.

char* vigenereCipher(char *plainText, char *k)

{

int i;

char cipher;

int cipherValue;

int len = strlen(k);

char *cipherText = (char *)malloc(sizeof(plainText) * sizeof(char));

//Loop through the length of the plain text string

for (i = 0; i < strlen(plainText); i++)

{

//if the character is lowercase, where range is [97 -122]

if (islower(plainText[i]))

{

cipherValue = ((int)plainText[i] - 97 + (int)tolower(k[i % len]) - 97) % 26 + 97;

cipher = (char)cipherValue;

}

else // Else it's upper case, where letter range is [65 - 90]

{

cipherValue = ((int)plainText[i] - 65 + (int)toupper(k[i % len]) - 65) % 26 + 65;

cipher = (char)cipherValue;

}

//Print the ciphered character if it is alphanumeric (a letter)

if (isalpha(plainText[i]))

{

// Assign the cipher to the cipherText instead of printing it.

*cipherText = cipher;

cipherText++;

}

else //if the character is not a letter then print the character (e.g. space)

{

// Assign the character to the cipherText instead of printing it.

*cipherText = plainText[i];

cipherText++;

}

}

printf("%s", cipherText); //<------TRY PRINTING IT HERE AND NOTHING!!!!!

//return cipherText;

return strdup(cipherText);

}

Homework Answers

Answer #1

Here, you can check my C++ code.

// 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 
        char 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 
        char 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; 
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
C Programming I am trying to also print the frequency and the occurrence of an input...
C Programming I am trying to also print the frequency and the occurrence of an input text file. I got the occurrence to but cant get the frequency. Formula for frequency is "Occurrence / total input count", this is the percentage of occurrence. Can someone please help me to get the frequency to work. Code: int freq[26] = {0}; fgets(input1, 10000, (FILE*)MyFile); for(i=0; i< strlen(input); i++) { freq[input[i]-'a']++; count++; } printf("Text count = %d", count); printf("\n"); printf("Frequency of plain text\n");...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...
Hi there, I've been asked to write a program in C which can read values from...
Hi there, I've been asked to write a program in C which can read values from a file then sort them, and then write to a binary file. I'm getting stuck when I write my binary file as the output is just spitting out garbage values and not the values that are being read in. When I print my input file reader everything is perfect but after sorting and then writing, the output is completely wrong. I have checked that...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
I've just finished typing my code in C, but I don't know how to draw a...
I've just finished typing my code in C, but I don't know how to draw a flowchart. Can somebody help me create a flow chart of this C program please? #include <stdio.h> int main(void){ char answer; // user's inputted single character int score = 0; // initialize score to 0 printf("-----------------------------------\n"); printf("\tMood Self Assessment\n"); printf("-----------------------------------\n"); printf("This Mood Self-Assessment program can help you determine and understand how\nyou are feeling recently."); printf("The user has to answer 3 questions honestly and\ntruthfully in order...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...