Encryption/Decryption program not working
Hi, I tried to make an encrypting/decrypting program, and it isn't working as I planned, plus I can't find a solution for my problem. The problem is:
Lets take an example (the program isn't like this):
I encrypt the string "abc", it becomes "123".
I decrypt the string "123", it becomes "╛Ü╣", which is different from abc.
I'm not having any success on fixing this issue. Here is the program code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
using namespace std;
void Encrypt() {
char *string;
string = new char[];
char *newstring;
newstring = new char[];
cout << "Insert your string to encrypt" << endl;
cin >> string;
for (int i=0,n=1;newstring[i]!='\0';i++,n++) {
newstring[i]=string[i]>>n;
}
cout << "\nEncrypted string is:\n" << newstring << endl;
}
void Decrypt() {
char *string;
string = new char[];
char *newstring;
newstring = new char[];
cout << "Insert your string to decrypt" << endl;
cin >> string;
for (int i=0, n=1;newstring[i]!='\0';i++,n++) {
newstring[i]=string[i]<<n;
}
cout << "\nDecrypted string is:\n" << newstring << endl;
}
int main() {
int a;
cout << "Choose an option:\n\n1- Encrypt a string \n2- Decrypt a string" << endl;
cin >> a;
if (a==1) {
Encrypt();
}
else if (a==2) {
Decrypt();
}
else {
cout << "Invalid choice\n" << endl;
main();
}
}
| |
Can anyone assist me?
I guess because in ASCII, 1 - 3 isn't ABC? I suggest checking up a table online.
You should also free the memory you create using new
.
Topic archived. No new replies allowed.