Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to decrypt a message from a file encrypted.txt using the affine cypher x=a^-1*(c-b)%26. Using this cypher I want to print the decrypted

I am trying to decrypt a message from a file "encrypted.txt" using the affine cypher x=a^-1*(c-b)%26. Using this cypher I want to print the decrypted message to the file "decrypted.txt". After this I ask the user if they would like to re encrypt the message using the cypher c=(a*x+b)%26. The decryption key is (5,7) which give values for (a,b) and x is the numerical value for c from 0-25. With this code I am getting a runtime error #3-T and I can not figure out why.

#define _CRT_SECURE_NO_WARNINGS #include #include

char encryptFun(int a, int b, char c) { int x; switch (c) //switch case to give c numerical value from 0-25 { case 'a': x = 0; c = (a * x + b) % 26; break; case 'b': x = 1; c = (a * x + b) % 26; break; case 'c': x = 2; c = (a * x + b) % 26; break; case 'd': x = 3; c = (a * x + b) % 26; break; case 'e': x = 4; c = (a * x + b) % 26; break; case 'f': x = 5; c = (a * x + b) % 26; break; case 'g': x = 6; c = (a * x + b) % 26; break; case 'h': x = 7; c = (a * x + b) % 26; break; case 'i': x = 8; c = (a * x + b) % 26; break; case 'j': x = 9; c = (a * x + b) % 26; break; case 'k': x = 10; c = (a * x + b) % 26; break; case 'l': x = 11; c = (a * x + b) % 26; break; case 'm': x = 12; c = (a * x + b) % 26; break; case 'n': x = 13; c = (a * x + b) % 26; break; case 'o': x = 14; c = (a * x + b) % 26; break; case 'p': x = 15; c = (a * x + b) % 26; break; case 'q': x = 16; c = (a * x + b) % 26; break; case 'r': x = 17; c = (a * x + b) % 26; break; case 's': x = 18; c = (a * x + b) % 26; break; case 't': x = 19; c = (a * x + b) % 26; break; case 'u': x = 20; c = (a * x + b) % 26; break; case 'v': x = 21; c = (a * x + b) % 26; break; case 'w': x = 22; c = (a * x + b) % 26; break; case 'x': x = 23; c = (a * x + b) % 26; break; case 'y': x = 24; c = (a * x + b) % 26; break; case 'z': x = 25; c = (a * x + b) % 26; break; default: c = c; break;

} return c; } char decryptFun(int decrypta, int decryptb, char decryptc) { int x; char b; b = decryptc; switch (decryptc) //switch case to give c numerical value from 0-25 { case 'a': x = 0; break; case 'b': x = 1; break; case 'c': x = 2; break; case 'd': x = 3; break; case 'e': x = 4; break; case 'f': x = 5; break; case 'g': x = 6; break; case 'h': x = 7; break; case 'i': x = 8; break; case 'j': x = 9; break; case 'k': x = 10; break; case 'l': x = 11; break; case 'm': x = 12; break; case 'n': x = 13; break; case 'o': x = 14; break; case 'p': x = 15; break; case 'q': x = 16; break; case 'r': x = 17; break; case 's': x = 18; break; case 't': x = 19; break; case 'u': x = 20; break; case 'v': x = 21; break; case 'w': x = 22; break; case 'x': x = 23; break; case 'y': x = 24; break; case 'z': x = 25; break; default: b = decryptc; break; } if (x >= 0 && x <= 25) { b = ((1 / decrypta)*(x - decryptb)) % 26; } return b; } int main() { int key1, key2; char letter,YorN; FILE *inp, *outp; inp = fopen("encrypted.txt", "r"); outp = fopen("decrypted.txt", "w"); if (inp == NULL) { printf("File does not exist "); } else { printf("Enter the decryption key: "); scanf("%d %d", &key1, &key2); while ((key1 != 5) || (key2 != 7)) { //check for proper key printf("Wrong key! "); printf("Enter the decryption key: "); scanf(" %d %d", &key1, &key2); } while (fscanf(inp, "%c", &letter) != EOF) { //call to decryption function fprintf(outp, "%c", decryptFun(key1, key2, letter)); } printf("Decryption complete. "); fclose(inp); fclose(outp); } printf("Would you like to re-encrypt the file (y or n) ? "); scanf(" %c", &YorN); if (YorN == 'n') { return 0; } else { inp = fopen("decrypted.txt", "r"); outp = fopen("encrypted.txt", "w"); while (fscanf(inp, "%c", &letter) != EOF) { fprintf(outp, "%c", decryptFun(key1, key2, letter)); } printf("Encryption complete. "); } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Administration The Essential Reference

Authors: Brian Laskey, David Kreines

1st Edition

1565925165, 978-1565925168

More Books

Students also viewed these Databases questions

Question

Explain the causes of indiscipline.

Answered: 1 week ago