C++ Decryptor

:D Hello,

I am making a C++ console application where the user can encrypt and decrypt text. So far, I have successfully made the encryption part, but am not quite sure how to make the decryption part. For example: abcb -> 128.70.88.70. but I can't make it go back to abcb. Here is my code so far:
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <conio.h>
#include <stdio.h>
#include <cstdlib>
#include "windows.h"
#include "winuser.h"
#include <cmath>
#include <iostream>
#include <iomanip>
#include <complex>
#include <string>


int main ()
{ 
    using namespace std;
    
    int f;
    char n;
    char b;
    char none;
    string password;
    string nonencr;
    string encr;
    system("color 1f");
    
cout <<"************************************************************************"; cout << endl;
cout <<"****************Welcome to Bashcode Encryptor© v1.0.0.0*****************"; cout << endl;
cout <<"************************************************************************"; cout << endl;
cout << endl;
cout << endl;

cout <<"Enter password: "; do{
     n=char(getch()); 
     
     if(int(n) !=13 && int(n) !=8){ 
                cout<<"x"; 
                password += n; 
                 
                }
                }while(int(n)!=13); 
                                    
cout << endl;
cout << endl;
if ( password == "bashcode"){
             cout <<"ACCESS GRANTED"; Sleep(1500); }
             else {system("CLS"); cout <<"ACCESS DENIED. The program will now shut down."; Sleep(2000) ; return 0;}
cout << endl;
cout << endl;
system("CLS");
cout <<"Select an option:" << endl;

cout << endl;
cout <<"Encrypt (1)" << endl;
cout << endl;
cout <<"Decrypt (2)" << endl;
cout << endl;
cout <<">>>>>>>> "; cin >> f;
if (f == 1){
      system("CLS"); cout <<"Input text to be encrypted: "; do{
     none=char(getch()); 
     
     if(int(none) == 97){ 
                cout<<"a"; 
                encr += "128.";  
                                 
                }
     if(int(none) == 98){ 
                cout<<"b"; 
                encr += "70.";}
     
     
     if(int(none) == 99){ 
                cout<<"c"; 
                encr += "88.";}
                
     if(int(none) == 100){ 
                cout<<"d"; 
                encr += "40.";}
                
     if(int(none) == 101){ 
                cout<<"e"; 
                encr += "54.";}    
     
     if(int(none) == 102){ 
                cout<<"f"; 
                encr += "36.";}                            
     
     if(int(none) == 103){ 
                cout<<"g"; 
                encr += "10.";}           
                }while(int(none)!=13);
     cout << endl;
     cout << endl;
     cout << "Encryped version: ";
     cout << endl;
     cout << endl;
     cout << encr;
}

if (f == 2){
      cout <<"Enter something to decrypt: "; 
     
    
            
               
     
    
      cout << endl;
      cout << endl; 
      cout << nonencr;
}      

cin.clear();
cin.ignore(255, '\n');
cin.get();

return 0; 
}


I would greatly appreciate any help :D
Do you understand the limitations of this method of encryption? This is a subsitution cipher, others call it a "Ceasar Shift" cipher and it is very easily broken with frequency analysis.
Yes, I started learning C++ a few days ago and I know how feeble my application is, but I'm learning :D
As coded, you just need to write the reverse function!

Use getch() to get i/p like your encoding function. But add the chars to an array until you reach a '.'. Then you'll need a function to map 128, 70, 88, 80 back to a, b, c, d

You'll also have to renember to handle the last char just after the loop exits.

Andy

P.S. I would split you encode loop out into it's own function before you move on.

Those IF statements are horribly redundant and unnecessary... just cast the char to an int. When you cast a char to an integer, the integer is the ASCII value of the character.
As coded, you just need to write the reverse function!


Thank you for your help, but I'm still not quite sure how I could achieve this because I don't know how I would implement a char array into my code.

Thanks again,

Muhasaresa
I think your problem might be in the encryption you are trying to use. Do you know what it is called? I believe I was wrong when I said this was a Ceasar Shift, it seems that encr is your actual encrypted message but I can't make heads or tails of it. Where did you read up on this?
You are right, "encr" is the encrypted message and it works just fine. I wrote the code myself and the letters are substituted to a number of my choosing followed by a dot. I'm just trying to get my decryptor working but I have no idea how :(

I can get "abcca" (for example) to go to 128.70.88.88.128.

But I can't get 128.70.88.88.128. to go back to "abcca" :(

Muhasaresa.
Last edited on
Perhaps you may want to do something like this:

1
2
3
4
5
6
7
8
unsigned char cryptTable[255];
for (int a = 0; a < sizeof(cryptTable); a++){
    cryptTable[a] = a;
}
//now set up your swaps
cryptTable['a'] = 'b'; //needs the single quotes
cryptTable['b'] = 'a';
//etc. 


(I would put all of this in a class, if I were you, if you know how)

this way you can simply write a function to swap characters directly.
This in conjunction with xor encryption and some simple algorithms (including salting the data) would make a somewhat difficult to break encryption.
Encryption/decryption is a very big topic. We have so many algorithm, RSA,Blowfish,AES etc. Seems the current latest is AES.
Thanks everyone for your help. I appreciate it!

Muhasaresa
Topic archived. No new replies allowed.