Image

Imagepyro01 wrote in Imagecpp

any idea why this won't work correctly?

the problems: characters with values of lower case i and higher (ijklmnopqrstuvwxyz)
 do not decrypt correctly. characters with values of upper case L until lower case a are not
 regular alphabetical charactes, in my code what should happen is after tilda (~) it should
 wrap back to A, but that doesn't happen.

#include <fstream>
#include <iostream>

using namespace std;

void menu();
void inc();
void dec();
void passchange();
char pass[9], msg[100];
const int maxchar = 126;
//126

int main() {
  FILE* myFile;
  if ((myFile=fopen("passwd.incript","r+b")) == NULL) {
    if ((myFile=fopen("passwd.incript","w+")) == NULL) cout << "Error creating \"passwd.incript\" file\n";
    else {
      fclose(myFile);
      myFile=fopen("passwd.incript","r+b");
      do {
        cout << "\"passwd.incript\" file created\nPlease enter a password to incript and decrypt your messages: ";
        cin >> msg;
        if (msg[9]||!msg[2]) cout << "Password must be at least 3 characters and no less than 9 characters long.\n";
      } while(msg[9]||!msg[2]);
      for(int i=0;i<9;i++) {
        pass[i]=msg[i];
        msg[i]='\0';
      }    
      for (int i=0;i<9;i++) {
        if(!pass[i]) pass[i]='g';
      }
      fwrite(&pass,sizeof(pass),1,myFile);
      fclose(myFile);
    }    
  }
  else {
    fread(&pass,sizeof(pass),1,myFile);
    cout << "Password retrieved from memory\n";
    fclose(myFile);
  }
  menu();
  cout << "Goodbye.\n";
  return 1;
}  

void menu() {
  int choice=0;
  do {
    do {
      cout << "What would you like to do?\n1 - Incript\n2 - Decrypt\n3 - Change password\n4 - Quit\n\nChoice: ";
      cin >> choice;
    } while(choice<1||choice>4);
    if (choice == 1) inc();
    if (choice == 2) dec();
    if (choice == 3) passchange();
  } while(choice!=4);
}

void inc() {
  FILE* myFile;
  int num=(((pass[0]*pass[3]*pass[8]+(pass[1]*pass[2]*pass[5]+pass[4]/pass[6])*7)*(2*pass[7]))%((maxchar-80)/2));
  cout << "Please enter your message to incript: ";
  cin.getline(msg, 2);
  cin.getline(msg, 99);
  cout << "Your message: \"";
    for (int i=0;msg[i];i++) {
      if(msg[i]>'Z') msg[i]-=32;
      else msg[i]+=32;
      msg[i]+=num;
      cout << msg[i];
    }
  cout << "\"\n";
  fstream file_op("incription.txt",ios::out);
  file_op << msg;
  file_op.close();
  return;
}

void dec() {
  FILE* myFile;
  int num=(((pass[0]*pass[3]*pass[8]+(pass[1]*pass[2]*pass[5]+pass[4]/pass[6])*7)*(2*pass[7]))%((maxchar-80)/2));   //23
  cout << "Please enter your message to decrypt: ";
  cin.getline(msg, 2);
  cin.getline(msg, 99);
  cout << "Your message: \"";
    for (int i=0;msg[i];i++) {
      if(msg[i]>'Z') msg[i]-=32;
      else msg[i]+=32;
      msg[i]-=num;
      cout << msg[i];
    }
  cout << "\"\n";
  fstream file_op("incription.txt",ios::out);
  file_op << msg;
  file_op.close();
  return;
}            

void passchange() {
  FILE* myFile=fopen("passwd.incript","w+");
  do {
    cout << "Enter your new password: ";
    for(int i=0;i<9;i++) {
      msg[i]='\0';
    }   
    cin >> msg;
    if (msg[9]||!msg[2]) cout << "Password must be at least 3 characters and no more than 9 characters long.\n";
  } while(msg[9]||!msg[2]);
  for(int i=0;i<9;i++) {
    pass[i]=msg[i];
     msg[i]='\0';
  }    
  for (int i=0;i<9;i++) {
    if(!pass[i]) pass[i]='g';
  }
  fwrite(&pass,sizeof(pass),1,myFile);
  fclose(myFile);
  return;
}