Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Create a new .cpp file .. name it encryption_table
- //+ source
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- const int NSYMS = 95;
- const int NSHUFFLES = 1000;
- const int kSTART = 0;
- const int kINCREMENT = 1;
- static int substitutions[] = {
- 'U', 'L', 'd', '{', 'p', 'Q', '@', 'M',
- '-', ']', 'O', 'r', 'c', '6', '^', '#',
- 'Y', 'w', 'W', 'v', '!', 'F', 'b', ')',
- '+', 'h', 'J', 'f', 'C', '8', 'B', '7',
- '.', 'k', '2', 'u', 'Z', '9', 'K', 'o',
- '3', 'x', ' ', '\'', '=', '&', 'N', '*',
- '1', 'z', '(', '`', 'R', 'P', ':', 'l',
- '4', '0', 'e', '$', '_', '}', 'j', 't',
- '?', 'S', 'q', '>', ';', 'T', 'y', 'i',
- '\\', 'A', 'D', 'V', '5', '|', '<', '/',
- 'E', 'g', 'm', ',', '[', 'H', '%', 'a',
- 's', 'n', 'I', 'X', '~', '"', 'G'
- };
- const int len = sizeof(substitutions) / sizeof(int);
- int main()
- {
- cout << "Enter seed for random number generator" << endl;
- int s;
- cin >> s;
- srand(s);
- char table[NSYMS];
- for(int i = 0; i < NSYMS; i++)
- table[i] = i + ' ';
- // shuffle
- for(int j = 0; j < NSHUFFLES; j++ ) {
- int ndx1 = rand() % NSYMS;
- int ndx2 = rand() % NSYMS;
- char temp = table[ndx1];
- table[ndx1] = table[ndx2];
- table[ndx2] = temp;
- }
- // Output
- int j = 0;
- for(int i = 0; i < NSYMS; i++) {
- cout << "'" << table[i] << "', ";
- j++;
- if(j == 8) { cout << endl; j = 0; }
- }
- cout << endl;
- return 0;
- }
- //+ Now create another .cpp file named encrytpion
- //+ source
- #include <iostream>
- #include <cstdlib>
- #include <fstream>
- #include <string.h>
- using namespace std;
- const int NSYMS = 95;
- const int NSHUFFLES = 1000;
- const int kSTART = 0;
- const int kINCREMENT = 1;
- static int substitutions[] = {
- 'U', 'L', 'd', '{', 'p', 'Q', '@', 'M',
- '-', ']', 'O', 'r', 'c', '6', '^', '#',
- 'Y', 'w', 'W', 'v', '!', 'F', 'b', ')',
- '+', 'h', 'J', 'f', 'C', '8', 'B', '7',
- '.', 'k', '2', 'u', 'Z', '9', 'K', 'o',
- '3', 'x', ' ', '\'', '=', '&', 'N', '*',
- '1', 'z', '(', '`', 'R', 'P', ':', 'l',
- '4', '0', 'e', '$', '_', '}', 'j', 't',
- '?', 'S', 'q', '>', ';', 'T', 'y', 'i',
- '\\', 'A', 'D', 'V', '5', '|', '<', '/',
- 'E', 'g', 'm', ',', '[', 'H', '%', 'a',
- 's', 'n', 'I', 'X', '~', '"', 'G'
- };
- const int len = sizeof(substitutions) / sizeof(int);
- const int kMSGSIZE = 200000;
- static ofstream outfile;
- void OpenOutfile(void);
- int GetAndCheckLine(char line[], int linelen);
- void GetMessage(char txt[]);
- void Encrypt(char m[]);
- void SaveToFile(const char m[]);
- void CloseFile(void);
- void OpenOutfile(void)
- {
- const int kNAMESIZE = 100;
- char filename[kNAMESIZE];
- cout <<"\t\t\t:] CODED BY XXLB::OREN [:"<<endl;
- cout << "\n\nEnter name of message file : ";
- cin.getline(filename, kNAMESIZE-1, '\n');
- outfile.open(filename, ios::out);
- if(!outfile.good()) {
- cout << "Can't open that file. Quitting." << endl;
- exit(1);
- }
- }
- int GetAndCheckLine(char line[], int linelen)
- {
- cin.getline(line, linelen - 1, '\n');
- // Check for a line with no characters
- int actuallength = strlen(line);
- if(actuallength == 0)
- return 0;
- // Check contents
- // all characters should be printable ASCII
- for(int i = 0; i< actuallength; i++)
- if(!isprint(line[i])) {
- cout << "Illegal characters,"
- " line discarded"; return -1; }
- return actuallength;
- }
- void GetMessage(char txt[])
- {
- int count = 0;
- txt[count] = '\0';
- const int ll = 120;
- char Line[ll];
- int n = GetAndCheckLine(Line,ll);
- while(n != 0) {
- if(n > 0) {
- count += n;
- if(count > kMSGSIZE-2) {
- cout << "\n\nSorry that message is"
- "too long. Quitting." << endl;
- exit(1);
- }
- strcat(txt, Line);
- // add the space to the end of the message
- txt[count] =' ';
- // update message length
- count++;
- // and replace the null just overwritten
- txt[count] = '\0';
- }
- n = GetAndCheckLine(Line,ll);
- }
- }
- void Encrypt(char m[])
- {
- int k = kSTART;
- for(int i=0; m[i] != '\0'; i++) {
- char ch = m[i];
- m[i] = substitutions[(ch-32 +k ) % len];
- k += kINCREMENT;
- }
- }
- void SaveToFile(const char m[])
- {
- outfile << m;
- }
- void CloseFile(void)
- {
- outfile.close();
- }
- int main()
- {
- char msg[kMSGSIZE];
- OpenOutfile();
- cout <<"\n\n--------------------------------------------------------------------------------"<<endl;
- cout << "Enter text of message:" << endl;
- GetMessage(msg);
- cout << "Enter seed for encrypting routine generator" << endl;
- int s;
- cin >> s;
- srand(s);
- char table[NSYMS];
- for(int i = 0; i < NSYMS; i++)
- table[i] = i + ' ';
- // shuffle
- for(int j = 0; j < NSHUFFLES; j++ ) {
- int ndx1 = rand() % NSYMS;
- int ndx2 = rand() % NSYMS;
- char temp = table[ndx1];
- table[ndx1] = table[ndx2];
- table[ndx2] = temp;
- }
- // Output
- int j = 0;
- for(int i = 0; i < NSYMS; i++) {
- cout << "'" << table[i] << "', ";
- j++;
- if(j == 8) { cout << endl; j = 0; }
- }
- cout <<"\n--------------------------------------------------------------------------------"<<endl;
- cout << "\n\nEncrypting and saving." << endl;
- Encrypt(msg);
- SaveToFile(msg);
- CloseFile();
- cout <<"\n:] ========== >>>>> DONE <<<<< =========="<<endl;
- system("pause");
- return 0;
- }
- //+ compile and run ....
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement