:) 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 -> 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{
     if(int(n) !=13 && int(n) !=8){ 
                password += n; 
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;
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{
     if(int(none) == 97){ 
                encr += "128.";  
     if(int(none) == 98){ 
                encr += "70.";}
     if(int(none) == 99){ 
                encr += "88.";}
     if(int(none) == 100){ 
                encr += "40.";}
     if(int(none) == 101){ 
                encr += "54.";}    
     if(int(none) == 102){ 
                encr += "36.";}                            
     if(int(none) == 103){ 
                encr += "10.";}           
     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.ignore(255, '\n');

return 0; 

I would greatly appreciate any help :icon_cheesygrin:

5 Years
Discussion Span
Last Post by donaldw

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.