InterviewSolution
| 1. |
Solve : Crypt & UnCrypt - C++ Programming - Sharing Project? |
|
Answer» Decided to share a project I was playing with. During the cold winter months I like to dive into programming and had been thinking about playing around with scrambling and crypting info for a while. These Crypt and unCrypt programs are not complex and are very basic crypt with character substitution. This is pretty easy to crack to decrypt because its just substitution and repeat characters also show as repeats in the crypted form. Crypt Version 1.02 Quote unCrypt Version 1.02 Crypt Version 1.02 source code Code: [Select]#include <cstdlib> #include <iostream> #include <algorithm> #include <string> // 12/30/2015 - Added string random shuffle // 12/31/2015 - Added custom seed for random shuffle so seed acts a crypt key // 1/4/2016 - Added ability to enter string and have while loop process // 1/5/2016 - Added characters , and ; to supported characters which were missing from v1.00 // 1/8/2015 - Source code trash cleanup & offset band aid removed for IF statement logic +1 array offset using namespace std; int main(int argc, char *argv[]) { int seed1=0; int code1=0; int run=1; char ch1; cout<<" Crypt Version 1.02\n\n"; cout<<"XXXXXXXXXXXXXXXXXXXXX\n"; cout<<" Enter Integer Seed: \n"; // Asks user to input integer seed cout<<"XXXXXXXXXXXXXXXXXXXXX\n\n"; cin >> seed1; // Input user seed cout<<"\n\n"<<"Crypt String Key =\n\n"; //Initialize Valid Characters for String Shuffle Output //Note Bug corrected with blank space for \ by use of escape character proceeding string str="[emailprotected]#$%^&*()_-+=?<>:\\/~.,;"; srand(seed1); // Allows user custom seeded starting algorithm position for random random_shuffle(str.begin(), str.end()); // Shuffle the string cout << str << "\n\n\n"; // Output the shuffle sequence //Pass String Output into an array to pair up pointer value with associated character string tmp = str; //Pass str output to string tmp char tab2[128]; // Memory Allocation for array population strncpy(tab2, tmp.c_str(), sizeof(tab2)); //string copy tmp into tab2 array tab2[sizeof(tab2) - 1] = 0; cout<<"Enter Info to Crypt in correct case\n"; cout<<"To Exit Program Close Window or enter ` \n\n\n"; while(run==1){ cin>>ch1; if (ch1=='A'){ code1=0; } else if (ch1=='B'){ code1=1; } else if (ch1=='C'){ code1=2; } else if (ch1=='D'){ code1=3; } else if (ch1=='E'){ code1=4; } else if (ch1=='F'){ code1=5; } else if (ch1=='G'){ code1=6; } else if (ch1=='H'){ code1=7; } else if (ch1=='I'){ code1=8; } else if (ch1=='J'){ code1=9; } else if (ch1=='K'){ code1=10; } else if (ch1=='L'){ code1=11; } else if (ch1=='M'){ code1=12; } else if (ch1=='N'){ code1=13; } else if (ch1=='O'){ code1=14; } else if (ch1=='P'){ code1=15; } else if (ch1=='Q'){ code1=16; } else if (ch1=='R'){ code1=17; } else if (ch1=='S'){ code1=18; } else if (ch1=='T'){ code1=19; } else if (ch1=='U'){ code1=20; } else if (ch1=='V'){ code1=21; } else if (ch1=='W'){ code1=22; } else if (ch1=='X'){ code1=23; } else if (ch1=='Y'){ code1=24; } else if (ch1=='Z'){ code1=25; } else if (ch1=='a'){ code1=26; } else if (ch1=='b'){ code1=27; } else if (ch1=='c'){ code1=28; } else if (ch1=='d'){ code1=29; } else if (ch1=='e'){ code1=30; } else if (ch1=='f'){ code1=31; } else if (ch1=='g'){ code1=32; } else if (ch1=='h'){ code1=33; } else if (ch1=='i'){ code1=34; } else if (ch1=='j'){ code1=35; } else if (ch1=='k'){ code1=36; } else if (ch1=='l'){ code1=37; } else if (ch1=='m'){ code1=38; } else if (ch1=='n'){ code1=39; } else if (ch1=='o'){ code1=40; } else if (ch1=='p'){ code1=41; } else if (ch1=='q'){ code1=42; } else if (ch1=='r'){ code1=43; } else if (ch1=='s'){ code1=44; } else if (ch1=='t'){ code1=45; } else if (ch1=='u'){ code1=46; } else if (ch1=='v'){ code1=47; } else if (ch1=='w'){ code1=48; } else if (ch1=='x'){ code1=49; } else if (ch1=='y'){ code1=50; } else if (ch1=='z'){ code1=51; } else if (ch1=='1'){ code1=52; } else if (ch1=='2'){ code1=53; } else if (ch1=='3'){ code1=54; } else if (ch1=='4'){ code1=55; } else if (ch1=='5'){ code1=56; } else if (ch1=='6'){ code1=57; } else if (ch1=='7'){ code1=58; } else if (ch1=='8'){ code1=59; } else if (ch1=='9'){ code1=60; } else if (ch1=='0'){ code1=61; } else if (ch1=='!'){ code1=62; } else if (ch1=='@'){ code1=63; } else if (ch1=='#'){ code1=64; } else if (ch1=='$'){ code1=65; } else if (ch1=='%'){ code1=66; } else if (ch1=='^'){ code1=67; } else if (ch1=='&'){ code1=68; } else if (ch1=='*'){ code1=69; } else if (ch1=='('){ code1=70; } else if (ch1==')'){ code1=71; } else if (ch1=='_'){ code1=72; } else if (ch1=='-'){ code1=73; } else if (ch1=='+'){ code1=74; } else if (ch1=='='){ code1=75; } else if (ch1=='?'){ code1=76; } else if (ch1=='<'){ code1=77; } else if (ch1=='>'){ code1=78; } else if (ch1==':'){ code1=79; } else if (ch1=='\\'){ // Escape Character \ needed to allow \ check code1=80; } else if (ch1=='/'){ code1=81; } else if (ch1=='~'){ code1=82; } else if (ch1=='.'){ code1=83; } else if (ch1==','){ code1=84; } else if (ch1==';'){ code1=85; } else if (ch1=='\`'){ //Escape Character \ before ` to exit run=0; //Run = False at 0 and leaves while loop } else { cout<<"Invalid Input = No Match\n\n"; } cout<<tab2[code1]; } system("PAUSE"); return EXIT_SUCCESS; } unCrypt Version 1.02 source code Code: [Select]#include <cstdlib> #include <iostream> #include <algorithm> #include <string> // 12/30/2015 - Added string random shuffle // 12/31/2015 - Added custom seed for random shuffle so seed acts a crypt key // 1/4/2016 - Added ability to enter string and have while loop process // 1/5/2016 - Added inverse unCrypt array pairing between Alpha and Seeded Key // 1/6/2015 - Added characters , and ; to supported characters to crypt // 1/8/2015 - Source code trash cleanup & IF logic Array offset correction to remove -1 band aid using namespace std; int main(int argc, char *argv[]) { int seed1=0; int code1=0; int run=1; char ch1; cout<<" unCrypt Version 1.02\n\n"; cout<<"XXXXXXXXXXXXXXXXXXXXX\n"; cout<<" Enter Integer Seed: \n"; // Asks user to input integer seed cout<<"XXXXXXXXXXXXXXXXXXXXX\n\n"; cin >> seed1; // Input user seed cout<<"\n\n"<<"unCrypt String Key =\n\n"; //Initialize Valid Characters for String Shuffle Output //Note Bug corrected with blank space for \ by use of escape character proceeding string str="[emailprotected]#$%^&*()_-+=?<>:\\/~.,;"; //String random comparison 1 for 1 match //Pass static string list to array prior to shuffle string tmp1 = str; //Pass str output to string tmp char tab3[128]; // Memory Allocation for array population strncpy(tab3, tmp1.c_str(), sizeof(tab3)); //string copy tmp into tab3 array tab3[sizeof(tab3) - 1] = 0; srand(seed1); // Allows user custom seeded starting algorithm position for random random_shuffle(str.begin(), str.end()); // Shuffle the string cout << str << "\n\n\n"; // Output the shuffle sequence //Pass String Output into an array to pair up pointer value with associated character string tmp = str; //Pass str output to string tmp char tab2[128]; // Memory Allocation for array population strncpy(tab2, tmp.c_str(), sizeof(tab2)); //string copy tmp into tab2 array tab2[sizeof(tab2) - 1] = 0; cout<<"Enter Info to unCrypt in correct case\n"; cout<<"To Exit Program Close Window or enter ` \n\n\n"; while(run==1){ cin>>ch1; if (ch1==tab2[0]){ code1=0; } else if (ch1==tab2[1]){ code1=1; } else if (ch1==tab2[2]){ code1=2; } else if (ch1==tab2[3]){ code1=3; } else if (ch1==tab2[4]){ code1=4; } else if (ch1==tab2[5]){ code1=5; } else if (ch1==tab2[6]){ code1=6; } else if (ch1==tab2[7]){ code1=7; } else if (ch1==tab2[8]){ code1=8; } else if (ch1==tab2[9]){ code1=9; } else if (ch1==tab2[10]){ code1=10; } else if (ch1==tab2[11]){ code1=11; } else if (ch1==tab2[12]){ code1=12; } else if (ch1==tab2[13]){ code1=13; } else if (ch1==tab2[14]){ code1=14; } else if (ch1==tab2[15]){ code1=15; } else if (ch1==tab2[16]){ code1=16; } else if (ch1==tab2[17]){ code1=17; } else if (ch1==tab2[18]){ code1=18; } else if (ch1==tab2[19]){ code1=19; } else if (ch1==tab2[20]){ code1=20; } else if (ch1==tab2[21]){ code1=21; } else if (ch1==tab2[22]){ code1=22; } else if (ch1==tab2[23]){ code1=23; } else if (ch1==tab2[24]){ code1=24; } else if (ch1==tab2[25]){ code1=25; } else if (ch1==tab2[26]){ code1=26; } else if (ch1==tab2[27]){ code1=27; } else if (ch1==tab2[28]){ code1=28; } else if (ch1==tab2[29]){ code1=29; } else if (ch1==tab2[30]){ code1=30; } else if (ch1==tab2[31]){ code1=31; } else if (ch1==tab2[32]){ code1=32; } else if (ch1==tab2[33]){ code1=33; } else if (ch1==tab2[34]){ code1=34; } else if (ch1==tab2[35]){ code1=35; } else if (ch1==tab2[36]){ code1=36; } else if (ch1==tab2[37]){ code1=37; } else if (ch1==tab2[38]){ code1=38; } else if (ch1==tab2[39]){ code1=39; } else if (ch1==tab2[40]){ code1=40; } else if (ch1==tab2[41]){ code1=41; } else if (ch1==tab2[42]){ code1=42; } else if (ch1==tab2[43]){ code1=43; } else if (ch1==tab2[44]){ code1=44; } else if (ch1==tab2[45]){ code1=45; } else if (ch1==tab2[46]){ code1=46; } else if (ch1==tab2[47]){ code1=47; } else if (ch1==tab2[48]){ code1=48; } else if (ch1==tab2[49]){ code1=49; } else if (ch1==tab2[50]){ code1=50; } else if (ch1==tab2[51]){ code1=51; } else if (ch1==tab2[52]){ code1=52; } else if (ch1==tab2[53]){ code1=53; } else if (ch1==tab2[54]){ code1=54; } else if (ch1==tab2[55]){ code1=55; } else if (ch1==tab2[56]){ code1=56; } else if (ch1==tab2[57]){ code1=57; } else if (ch1==tab2[58]){ code1=58; } else if (ch1==tab2[59]){ code1=59; } else if (ch1==tab2[60]){ code1=60; } else if (ch1==tab2[61]){ code1=61; } else if (ch1==tab2[62]){ code1=62; } else if (ch1==tab2[63]){ code1=63; } else if (ch1==tab2[64]){ code1=64; } else if (ch1==tab2[65]){ code1=65; } else if (ch1==tab2[66]){ code1=66; } else if (ch1==tab2[67]){ code1=67; } else if (ch1==tab2[68]){ code1=68; } else if (ch1==tab2[69]){ code1=69; } else if (ch1==tab2[70]){ code1=70; } else if (ch1==tab2[71]){ code1=71; } else if (ch1==tab2[72]){ code1=72; } else if (ch1==tab2[73]){ code1=73; } else if (ch1==tab2[74]){ code1=74; } else if (ch1==tab2[75]){ code1=75; } else if (ch1==tab2[76]){ code1=76; } else if (ch1==tab2[77]){ code1=77; } else if (ch1==tab2[78]){ code1=78; } else if (ch1==tab2[79]){ code1=79; } else if (ch1==tab2[80]){ // Escape Character \ needed to allow \ check code1=80; } else if (ch1==tab2[81]){ code1=81; } else if (ch1==tab2[82]){ code1=82; } else if (ch1==tab2[83]){ code1=83; } else if (ch1==tab2[84]){ code1=84; } else if (ch1==tab2[85]){ code1=85; } else if (ch1=='\`'){ //Escape Character \ before ` to exit run=0; //Run = False at 0 and leaves while loop } else { cout<<"Invalid Input = No Match\n\n"; } cout<<tab3[code1]; // output } system("PAUSE"); return EXIT_SUCCESS; }Looks cool although you would be much better off using some sort of map data structure instead of that massive block of else ifs. Alternatively you may be able to get creative with the character's ASCII value. This would make the code a lot neater and more compact.Good suggestion ... Prior to coming up with what I came up with I was toying around with all sorts of dynamic methods of scrambled character substitution to try to make it work with less code, but while its really easy to scramble information with not many lines of code needed, being able to inverse that scramble and bring it back to its original form was tricky. So using 2 arrays one static and the other dynamic was the way to decrypt it back to its original form. I almost went with SWITCHES instead of IF THEN ELSE, but from years of IF THEN ELSE logic, that method flows easy from brain to keys. Tonight I was playing around with it writing a single lined entry to 2 files appended... a Key File and a Data File. And I thought all was well, BUT when looking further for some reason I am getting an additional write to file for the scrambled output so instead of an output of say 7*hY I am getting 7*hYY. My solution to fix this might be to have a counter that counts how many characters were entered and only allows the while loop to run equal to the character count. Or have a routine that reads in the last line and TRIMS off 1 character place. But these both seem like sloppy band aids. Its really strange that the output to display is correct without the extra character at the tail end, but the file write instruction processes writing the last character TWICE. This one has me slightly stumped because programmatically I dont see a flaw, but there is one. Code: [Select]#include <cstdlib> #include <iostream> #include <algorithm> #include <string> #include <fstream> // 12/30/2015 - Added string random shuffle // 12/31/2015 - Added custom seed for random shuffle so seed acts a crypt key // 1/4/2016 - Added ability to enter string and have while loop process v1.00 // 1/5/2016 - Added characters , and ; to supported characters which were missing from v1.01 // 1/8/2015 - Source code trash cleanup & offset band aid removed for IF statement logic +1 array offset v1.02 // 1/8/2015 - Added write to file for coded data storage eliminating need to copy/paste output to a text file v1.03 using namespace std; int main(int argc, char *argv[]) { int seed1=0; int code1=0; int run=1; int again=1; int test=1; char ch1; char ch2; while(again==1){ cout<<" Crypt Version 1.03\n\n"; cout<<"XXXXXXXXXXXXXXXXXXXXX\n"; cout<<" Enter Integer Seed: \n"; // Asks user to input integer seed cout<<"XXXXXXXXXXXXXXXXXXXXX\n\n"; cin >> seed1; // Input user seed cout<<"\n\n"<<"Crypt String Key =\n\n"; ofstream myfile; myfile.open ("Crypt103Key.txt", ios::out | ios::app); myfile <<seed1<<"\n"; myfile.close(); //Initialize Valid Characters for String Shuffle Output //Note Bug corrected with blank space for \ by use of escape character proceeding string str="[emailprotected]#$%^&*()_-+=?<>:\\/~.,;"; srand(seed1); // Allows user custom seeded starting algorithm position for random random_shuffle(str.begin(), str.end()); // Shuffle the string cout << str << "\n\n\n"; // Output the shuffle sequence //Pass String Output into an array to pair up pointer value with associated character string tmp = str; //Pass str output to string tmp char tab2[128]; // Memory Allocation for array population strncpy(tab2, tmp.c_str(), sizeof(tab2)); //string copy tmp into tab2 array tab2[sizeof(tab2) - 1] = 0; cout<<"Enter Info to Crypt in correct case\n"; cout<<"To Exit Inner Program enter ( ` ) \n\n\n"; while(run==1){ cin>>ch1; if (ch1=='A'){ code1=0; } else if (ch1=='B'){ code1=1; } else if (ch1=='C'){ code1=2; } else if (ch1=='D'){ code1=3; } else if (ch1=='E'){ code1=4; } else if (ch1=='F'){ code1=5; } else if (ch1=='G'){ code1=6; } else if (ch1=='H'){ code1=7; } else if (ch1=='I'){ code1=8; } else if (ch1=='J'){ code1=9; } else if (ch1=='K'){ code1=10; } else if (ch1=='L'){ code1=11; } else if (ch1=='M'){ code1=12; } else if (ch1=='N'){ code1=13; } else if (ch1=='O'){ code1=14; } else if (ch1=='P'){ code1=15; } else if (ch1=='Q'){ code1=16; } else if (ch1=='R'){ code1=17; } else if (ch1=='S'){ code1=18; } else if (ch1=='T'){ code1=19; } else if (ch1=='U'){ code1=20; } else if (ch1=='V'){ code1=21; } else if (ch1=='W'){ code1=22; } else if (ch1=='X'){ code1=23; } else if (ch1=='Y'){ code1=24; } else if (ch1=='Z'){ code1=25; } else if (ch1=='a'){ code1=26; } else if (ch1=='b'){ code1=27; } else if (ch1=='c'){ code1=28; } else if (ch1=='d'){ code1=29; } else if (ch1=='e'){ code1=30; } else if (ch1=='f'){ code1=31; } else if (ch1=='g'){ code1=32; } else if (ch1=='h'){ code1=33; } else if (ch1=='i'){ code1=34; } else if (ch1=='j'){ code1=35; } else if (ch1=='k'){ code1=36; } else if (ch1=='l'){ code1=37; } else if (ch1=='m'){ code1=38; } else if (ch1=='n'){ code1=39; } else if (ch1=='o'){ code1=40; } else if (ch1=='p'){ code1=41; } else if (ch1=='q'){ code1=42; } else if (ch1=='r'){ code1=43; } else if (ch1=='s'){ code1=44; } else if (ch1=='t'){ code1=45; } else if (ch1=='u'){ code1=46; } else if (ch1=='v'){ code1=47; } else if (ch1=='w'){ code1=48; } else if (ch1=='x'){ code1=49; } else if (ch1=='y'){ code1=50; } else if (ch1=='z'){ code1=51; } else if (ch1=='1'){ code1=52; } else if (ch1=='2'){ code1=53; } else if (ch1=='3'){ code1=54; } else if (ch1=='4'){ code1=55; } else if (ch1=='5'){ code1=56; } else if (ch1=='6'){ code1=57; } else if (ch1=='7'){ code1=58; } else if (ch1=='8'){ code1=59; } else if (ch1=='9'){ code1=60; } else if (ch1=='0'){ code1=61; } else if (ch1=='!'){ code1=62; } else if (ch1=='@'){ code1=63; } else if (ch1=='#'){ code1=64; } else if (ch1=='$'){ code1=65; } else if (ch1=='%'){ code1=66; } else if (ch1=='^'){ code1=67; } else if (ch1=='&'){ code1=68; } else if (ch1=='*'){ code1=69; } else if (ch1=='('){ code1=70; } else if (ch1==')'){ code1=71; } else if (ch1=='_'){ code1=72; } else if (ch1=='-'){ code1=73; } else if (ch1=='+'){ code1=74; } else if (ch1=='='){ code1=75; } else if (ch1=='?'){ code1=76; } else if (ch1=='<'){ code1=77; } else if (ch1=='>'){ code1=78; } else if (ch1==':'){ code1=79; } else if (ch1=='\\'){ // Escape Character \ needed to allow \ check code1=80; } else if (ch1=='/'){ code1=81; } else if (ch1=='~'){ code1=82; } else if (ch1=='.'){ code1=83; } else if (ch1==','){ code1=84; } else if (ch1==';'){ code1=85; } else if (ch1=='\`'){ //Escape Character \ before ` to exit run=0; //Run = False at 0 and leaves while loop } else { cout<<"Invalid Input = No Match\n\n"; } cout<<tab2[code1]; myfile.open ("Crypt103Data.txt", ios::out | ios::app); myfile <<tab2[code1]; myfile.close(); }// end inner while loop //Add line return in file to seperate the data per line myfile.open ("Crypt103Data.txt", ios::out | ios::app); myfile <<" "<<"\n"; myfile.close(); test=1; while(test==1){ system("CLS"); cout<<"Enter Y to continue or N to end \n\n\n"; cin>>ch2; if (ch2=='N'||ch2=='n'){ again=0; test=0; } else if (ch2=='Y'||ch2=='y'){ again=1; test=0; run=1; system("CLS"); } else { cout<<"Invalid Input, please choose Y or N \n\n"; test=1; } } } // end outter while loop system("CLS"); system("PAUSE"); return EXIT_SUCCESS; } It would be possible to replace the else if portions dealing with alphabetic characters with some math. Upper case characters are codes 65 to 90. Your codes start at 0, so all the blocks testing capital characters can be replaced: Code: [Select]if(ch >= 65 && ch <= 90) { code1 = ch-65; } for lowercase characters, they span from 97 through 122, and your cipher maps them at 26: Code: [Select]else if (ch1 >= 97 && ch1 <= 122) { code1 = ch1 - 71; } Thanks BC for your input on this... that got me thinking of back in the days many years ago with GW-Basic I used Chr$(ascii_value). http://www.antonis.de/qbebooks/gwbasman/chrs.html And I guess I am reinventing the wheel by assigning my own values to the characters, when I should just be using their already existing ascii identifier which I can get from casting char to an int. Found this info here that is a big help too, and will correct for the fact that spaces are not accounted for on entry of what is to get scrambled and character substitution. http://stackoverflow.com/questions/6405137/asc-and-chr-equivalent-in-c-c output is: Quote string = "Hello, World!" Also found more info here on the code example you gave me: http://stackoverflow.com/questions/5029840/convert-char-to-int-in-c-and-cFound and fixed the additional character write problem I had. I thought of suppressing the write to file until the start of the first iteration of the loop in which write enable is enabled for the write function. This fixes the issue where upon exit by use of the ( ` ) key it does not write the last character twice appended to the text file. Now that I have this fixed I can go back and clean up the IF logic mess. And make this program about 80% smaller in size. The double write bug was really driving me up the wall when it made no sense as to why it was happening. So I decided to force the write to file prior to the ( ` ) exit instruction so that it will exit the loop without passing the write to file instruction before leaving the while loop in which its writing the last input from the input buffer and that idea worked. However I had to suppress writing to file until the characters processed went through the logic otherwise I had a leading array[0] value write to file and it added the first character within the array at the beginning of the file appended. So by this write to file only being enabled after the input was processed through the IF logic, but not after the exit instruction, by use of a write enable flag to start the write enable for the write to file instruction. That fixed it. Prior to bug fix if you used a seed key value of 43 and entered Hello_World you would get AW\\^nY^H\22 and now with bug fix I get the correct information written to file of AW\\^nY^H\2 ( single last character appended vs double last character appended ) Quote //supress write to file until 1 loop run ( bug fix for double character write ) Code: [Select]#include <cstdlib> #include <iostream> #include <algorithm> #include <string> #include <fstream> // 12/30/2015 - Added string random shuffle // 12/31/2015 - Added custom seed for random shuffle so seed acts a crypt key // 1/4/2016 - Added ability to enter string and have while loop process v1.00 // 1/5/2016 - Added characters , and ; to supported characters which were missing from v1.01 // 1/8/2016 - Source code trash cleanup & offset band aid removed for IF statement logic +1 array offset v1.02 // 1/8/2016 - Added write to file for coded data storage eliminating need to copy/paste output to a text file v1.03 // 1/15/2016 - Fixed Loop writing duplicate character on ( ` ) loop exit with write delay until loop processed prior to write // - seed of 43 with Hello_World was producing AW\\^nY^H\22 prior when it should only be AW\\^nY^H\2 with a single 2 // - at tail end of write process. using namespace std; //int main(int argc, char *argv[]) int main () { int seed1=0; int code1=0; int run=1; int again=1; int test=1; int writeenable=0; char ch1; char ch2; while(again==1){ cout<<" Crypt Version 1.04\n\n"; cout<<"XXXXXXXXXXXXXXXXXXXXX\n"; cout<<" Enter Integer Seed: \n"; // Asks user to input integer seed cout<<"XXXXXXXXXXXXXXXXXXXXX\n\n"; cin >> seed1; // Input user seed cout<<"\n\n"<<"Crypt String Key =\n\n"; ofstream myfile; myfile.open ("Crypt104Key.txt", ios::out | ios::app); myfile <<seed1<<"\n"; myfile.close(); //Initialize Valid Characters for String Shuffle Output //Note Bug corrected with blank space for \ by use of escape character proceeding string str="[emailprotected]#$%^&*()_-+=?<>:\\/~.,;"; srand(seed1); // Allows user custom seeded starting algorithm position for random random_shuffle(str.begin(), str.end()); // Shuffle the string cout << str << "\n\n\n"; // Output the shuffle sequence //Pass String Output into an array to pair up pointer value with associated character string tmp = str; //Pass str output to string tmp char tab2[128]; // Memory Allocation for array population strncpy(tab2, tmp.c_str(), sizeof(tab2)); //string copy tmp into tab2 array tab2[sizeof(tab2) - 1] = 0; cout<<"Enter Info to Crypt in correct case\n"; cout<<"To Exit Inner Program enter ( ` ) \n\n\n"; while(run==1){ cin>>ch1; //supress write to file until 1 loop run ( bug fix for double character write ) if(writeenable==1){ myfile.open ("Crypt104Data.txt", ios::out | ios::app); myfile <<tab2[code1]; myfile.close(); } else{ //do nothing and continue } writeenable=1; //enable write to file if (ch1=='A'){ code1=0; } else if (ch1=='B'){ code1=1; } else if (ch1=='C'){ code1=2; } else if (ch1=='D'){ code1=3; } else if (ch1=='E'){ code1=4; } else if (ch1=='F'){ code1=5; } else if (ch1=='G'){ code1=6; } else if (ch1=='H'){ code1=7; } else if (ch1=='I'){ code1=8; } else if (ch1=='J'){ code1=9; } else if (ch1=='K'){ code1=10; } else if (ch1=='L'){ code1=11; } else if (ch1=='M'){ code1=12; } else if (ch1=='N'){ code1=13; } else if (ch1=='O'){ code1=14; } else if (ch1=='P'){ code1=15; } else if (ch1=='Q'){ code1=16; } else if (ch1=='R'){ code1=17; } else if (ch1=='S'){ code1=18; } else if (ch1=='T'){ code1=19; } else if (ch1=='U'){ code1=20; } else if (ch1=='V'){ code1=21; } else if (ch1=='W'){ code1=22; } else if (ch1=='X'){ code1=23; } else if (ch1=='Y'){ code1=24; } else if (ch1=='Z'){ code1=25; } else if (ch1=='a'){ code1=26; } else if (ch1=='b'){ code1=27; } else if (ch1=='c'){ code1=28; } else if (ch1=='d'){ code1=29; } else if (ch1=='e'){ code1=30; } else if (ch1=='f'){ code1=31; } else if (ch1=='g'){ code1=32; } else if (ch1=='h'){ code1=33; } else if (ch1=='i'){ code1=34; } else if (ch1=='j'){ code1=35; } else if (ch1=='k'){ code1=36; } else if (ch1=='l'){ code1=37; } else if (ch1=='m'){ code1=38; } else if (ch1=='n'){ code1=39; } else if (ch1=='o'){ code1=40; } else if (ch1=='p'){ code1=41; } else if (ch1=='q'){ code1=42; } else if (ch1=='r'){ code1=43; } else if (ch1=='s'){ code1=44; } else if (ch1=='t'){ code1=45; } else if (ch1=='u'){ code1=46; } else if (ch1=='v'){ code1=47; } else if (ch1=='w'){ code1=48; } else if (ch1=='x'){ code1=49; } else if (ch1=='y'){ code1=50; } else if (ch1=='z'){ code1=51; } else if (ch1=='1'){ code1=52; } else if (ch1=='2'){ code1=53; } else if (ch1=='3'){ code1=54; } else if (ch1=='4'){ code1=55; } else if (ch1=='5'){ code1=56; } else if (ch1=='6'){ code1=57; } else if (ch1=='7'){ code1=58; } else if (ch1=='8'){ code1=59; } else if (ch1=='9'){ code1=60; } else if (ch1=='0'){ code1=61; } else if (ch1=='!'){ code1=62; } else if (ch1=='@'){ code1=63; } else if (ch1=='#'){ code1=64; } else if (ch1=='$'){ code1=65; } else if (ch1=='%'){ code1=66; } else if (ch1=='^'){ code1=67; } else if (ch1=='&'){ code1=68; } else if (ch1=='*'){ code1=69; } else if (ch1=='('){ code1=70; } else if (ch1==')'){ code1=71; } else if (ch1=='_'){ code1=72; } else if (ch1=='-'){ code1=73; } else if (ch1=='+'){ code1=74; } else if (ch1=='='){ code1=75; } else if (ch1=='?'){ code1=76; } else if (ch1=='<'){ code1=77; } else if (ch1=='>'){ code1=78; } else if (ch1==':'){ code1=79; } else if (ch1=='\\'){ // Escape Character \ needed to allow \ check code1=80; } else if (ch1=='/'){ code1=81; } else if (ch1=='~'){ code1=82; } else if (ch1=='.'){ code1=83; } else if (ch1==','){ code1=84; } else if (ch1==';'){ code1=85; } else if (ch1=='\`'){ //Escape Character \ before ` to exit run=0; //Run = False at 0 and leaves while loop } else { cout<<"Invalid Input = No Match\n\n"; } // Display output cout<<tab2[code1]; }// end inner while loop test=1; while(test==1){ system("CLS"); cout<<"Enter Y to continue or N to end \n\n\n"; cin>>ch2; if (ch2=='N'||ch2=='n'){ // line return to file ( formatting purposes ) myfile.open ("Crypt104Data.txt", ios::out | ios::app); myfile <<"\n"; myfile.close(); again=0; test=0; } else if (ch2=='Y'||ch2=='y'){ // line return to file ( formatting purposes ) myfile.open ("Crypt104Data.txt", ios::out | ios::app); myfile <<"\n"; myfile.close(); again=1; test=0; run=1; writeenable=0; //reset write enable to start of 0 system("CLS"); } else { cout<<"Invalid Input, please choose Y or N \n\n"; test=1; } } } // end outter while loop system("CLS"); system("PAUSE"); //return EXIT_SUCCESS; return(0); } |
|