Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Program for simple encryption and decryption.
- c0d3d by sm@rtw3ap0n
- */
- #include<iostream>
- #include<conio.h>
- #include<cctype>
- #include<cstring>
- #include<Windows.h>
- using namespace std;
- // read key from user
- int readkey()
- {
- int key=0;
- cout<<"\nEnter key: ";
- // input key through a loop to prevent invalid input
- char ch;
- do
- {
- ch = getche();
- if (isdigit(ch))
- {
- key = key*10 + atoi(&ch);
- }
- else if (ch != 13)
- {
- cout<<"\b";
- Beep(800, 250);
- }
- } while (ch != 13);
- return key;
- }
- // encrypt the message
- void encrypt(int key, char msg[])
- {
- int length = strlen(msg);
- int k = key;
- for (int i = length - 1; i >= 0; --i)
- {
- msg[i] += k%10;
- if (msg[i] > 126)
- {
- msg[i] -= 94;
- }
- else if (msg[i] < 32)
- {
- msg[i] += 95;
- }
- if (k%10 == k)
- {
- k = key;
- }
- else
- {
- k /= 10;
- }
- }
- }
- // decrypt the message
- void decrypt(int key, char msg[])
- {
- int length = strlen(msg);
- int k = key;
- for (int i = length - 1; i >= 0; --i)
- {
- msg[i] -= k%10;
- if (msg[i] > 126)
- {
- msg[i] -= 94;
- }
- else if (msg[i] < 32)
- {
- msg[i] += 95;
- }
- if (k%10 == k)
- {
- k = key;
- }
- else
- {
- k /= 10;
- }
- }
- }
- int main()
- {
- char msg[100];
- int key;
- cout<<"\nEnter message: ";
- cin.getline(msg, 99);
- key = readkey();
- // encrypt it!
- encrypt(key, msg);
- cout<<"\nEncrypted Message: ";
- puts(msg);
- // decrypt it!
- decrypt(key, msg);
- cout<<"\n\nDecrypted Message: ";
- puts(msg);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment