Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include<cstring>
- using namespace std;
- char* encrypt(const char* message, const int count, char* cipher, const int cipher_count);
- char* decrypt(const char* cipher, const int count, char* plain, const int plain_count);
- const char alpha[28] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
- const char specialChars[28] = {
- '-', '!', '+', '\\', '~', '@', '^', '$', '[', '/',
- '>', ']', '#', '}', '(', '%', '*', '<', '}', '&',
- ')', ' ', ';', '|', ':', ',', '"','\0' // Space at index 21, quote at index 26
- };
- int main()
- {
- /*for (int i = 0; i < 28; i++)
- {
- if (alpha[i] == '\0')
- break;
- cout << alpha[i] << " - "<<specialChars[i]<<endl;
- }*/
- char message[10000] = { '\0' };
- char cipher[10000] = { '\0' };
- char plain[10000] = { '\0' };
- cout << "Enter the message to be encrypted: ";
- cin.getline(message, 10000);
- int count = strlen(message);
- encrypt(message, count,cipher,10000);
- cout << endl;
- for (int i = 0; i < count; i++)
- {
- cout << cipher[i];
- }
- cout << endl;
- decrypt(cipher, count, plain, 10000);
- for (int i = 0; i < count; i++)
- {
- cout << plain[i];
- }
- cout << endl;
- return 0;
- }
- char* encrypt(const char* message, const int count,char *cipher,const int cipher_count)
- {
- char msg = '\0';
- for (int i = 0; i < count; i++)
- {
- for (int j = 0; j < 28; j++)
- {
- if (message[i] == '\0')
- break;
- if (message[i] >96)
- {
- msg = message[i] - ('a' - 'A');
- }
- else
- {
- msg = message[i];
- }
- if (msg == alpha[j])
- {
- cipher[i] = specialChars[j];
- break;
- }
- }
- }
- return cipher;
- }
- char* decrypt(const char* cipher, const int count, char* plain, const int plain_count)
- {
- char cip = '\0';
- for (int i = 0; i < count; i++)
- {
- for (int j = 0; j < 28; j++)
- {
- if (cipher[i] == '\0')
- break;
- cip = cipher[i];
- if (cip == specialChars[j])
- {
- plain[i] = alpha[j];
- break;
- }
- }
- }
- return plain;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement