How to build a caesar cipher that uses 2D arrays (200 characters) to encrypt (re
ID: 3798203 • Letter: H
Question
How to build a caesar cipher that uses 2D arrays (200 characters) to encrypt (returns with a encrypted value) and decrypt (decrypted with a decrypted value) from row major and then prints out a 2D array?
Requirements for my lab is:
You are required to use a 2D array structure with either Row Major or Column Major ordering You need to make use of a few sub routines:
• Encrypt
• Decrypt:
• Print Array: It prints the 2D array out
• Store: Takes coordinates (Ri, Ci) and byte of data and stores into the 2D array
• Load: Takes coordinates (Ri, Ci) and loads a byte of data from the 2D array
Explanation / Answer
#include <stdio.h>
#include <string.h>
int main()
{
//To store original data
char originalData[1][200];
//To store encrypted data
char encData[1][200];
//To store decrypted data
char decData[1][200];
int r, c;
printf(" Enter the data to encrypt: ");
scanf("%[^' ']s", originalData);
printf(" Original data: %s", originalData);
//Encryption process
for(r = 0; r < 1; r++)
{
//Iterates till end of each column
for(c = 0; originalData[r][c]!=''; c++)
//Extract original character and add two to it
encData[r][c] = originalData[r][c] + 2;
}//end of for loop
encData[r][c] = '';
fflush(stdout);
//Displays the encrypted data
printf(" Encrypted data: %s", encData);
//Decryption process
for(r = 0; r < 1; r++)
{
//Loops till end of each column of encrypted data
for(c = 0; encData[r][c] != ''; c++)
//Subtract 2 from encrypted data
decData[r][c] = encData[r][c] - 2;
}
decData[r][c] = '';
fflush(stdout);
//Displays the decrypted data
printf(" Decrypted data: %s", decData);
}//End of main
Output:
Enter the data to encrypt: This is a process of encryption and decryption. Check it.
Original data: This is a process of encryption and decryption. Check it.
Encrypted data: Vjku"ku"c"rtqeguu"qh"gpet{rvkqp"cpf"fget{rvkqp0"Ejgem"kv
Decrypted data: This is a process of encryption and decryption. Check it.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.