:) 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:

#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 :icon_cheesygrin:

That's an interesting way of doing it, I suppose.

You'll want to parse through your encr string, stopping at each period, and build a string from the numbers you find. Then use something like atoi to change it into an integer. Then you need to subtract the value you added to the original ascii value and you're back at your original character.

I didn't ponder it too much, but is there any rhyme or reason to the numbers that you're adding? If you don't know which number you've added, you won't be able to subtract the right number when you're decrypting.

Unless otherwise required, you might consider adding the same number to the ascii value each time or devise some other sort of pattern like adding one number to the first character you're working with, another number to the second character and so on and maybe repeat that pattern after you've seen X number of characters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.