Code to encrypt and decrypt

#include<iostream>
#include<string.h>

using namespace std;


void encryptyfunc()
   {
     char text[100];
     char temp;
  int i, key;
  cout <<"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"<<endl;
  cout <<"Enter a message to encrypt: ";
  cin >>text;
  cout <<"Enter key: ";
  cin >> key;
  cout <<"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"<<endl;
  for(i = 0; text[i] != '\0'; ++i){
    temp = text[i];
    if(temp >= 'a' && temp <= 'z'){
      temp = temp + key;

      if(temp > 'z'){
        temp = temp - 'z' + 'a' - 1;
      }

      text[i] = temp;
    }
    else if(temp >= 'A' && temp <= 'Z'){
      temp = temp + key;

      if(temp > 'Z'){
        temp = temp - 'Z' + 'A' - 1;
      }

      text[i] = temp;
    }
  }
  cout <<"============================================================"<<endl;
  cout << "Your Encrypted message:" << text <<endl;
  cout <<"============================================================"<<endl;
}

void decryptfunc()
{
    char text[100];
    char temp;
  int i, key;
  cout <<"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"<<endl;
  cout <<"Enter a message to decrypt: " <<endl;
  cin >>text;
  cout <<"Enter key: " <<endl;
  cin >> key;
  for(i = 0; text[i] != '\0'; ++i){
    temp = text[i];
    if(temp >= 'a' && temp <= 'z'){
      temp = temp - key;

      if(temp < 'a'){
        temp = temp + 'z' - 'a' + 1;
      }

      text[i] = temp;
    }
    else if(temp >= 'A' && temp <= 'Z'){
      temp = temp - key;

      if(temp < 'A'){
        temp = temp + 'Z' - 'A' + 1;
      }

      text[i] = temp;
    }
  }
  cout <<"============================================================"<<endl;
  cout << "Your Decrypted message:" <<text << endl;
  cout <<"============================================================"<<endl;
}

int main()
{
  int choice;
  cout <<"============================================================"<<endl;
  cout << "Welcome to encryption and decryption fro MLH Localhackday" <<endl;
  cout <<"============================================================"<<endl;
  cout << "1.Encryption \t 2.Decryption"<<endl;
  cout <<"---------------------------------------------"<<endl;
  cin >> choice;
  switch(choice)
  {
    case 1: encryptyfunc();
            break;
    case 2: decryptfunc();
            break;
    default:cout << "Select a valid option: ";
            break;
  }


  return 0;
}

Built With

Share this project:

Updates