Substitution Cipher Program Problem...

This is a discussion on Substitution Cipher Program Problem... within the C++ Programming forums, part of the General Programming Boards category; << split from 4 year old thread - read the rules http://cboard.cprogramming.com/cplus...m-problem.html >> I am writing a simple substitution cipher ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    19

    Substitution Cipher Program Problem...

    << split from 4 year old thread - read the rules Substitution Cipher Program Problem... >>

    I am writing a simple substitution cipher also, though this is much more complex than mine. I built it so that it would follow a couple rules and generate it's own key, which it sent to a text file.

    Here is what I wrote... not nearly as elegant, in my opinion. Works very simply, and correctly, though I think that the handling method is inefficient... it changes things to ascii, adds or subtracts to/from that ASCII value, then resets it as a (new) letter.

    It does offer decryption capability, also... which, for the record, took MUCH longer to figure out than the initial encryption (weak though it is.)

    Code:
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    #include <vector.h>
    #include <string>
    #include <time.h>
    #include <fstream.h>
    
    using namespace::std;
    
    void encrypt (void);
    void decrypt (void);
    
    
    int main()
    {
        int choice;
        while (choice != 3)
        {
              cout<<"What operation would you like to perform? \n";
              cout<<"1 - Encrypt \n";
              cout<<"2 - Decrypt \n";
              cout<<"3 - Quit \n";
              cin>>choice;
              system("CLS");
              switch (choice)
              {
              case 1:
                   encrypt();
                   break;
              case 2:
                   decrypt();
                   break;
              case 3:
                   break;
              default: cout<<"Please enter a valid response. \n";
              break;
              }
        }
              
        return 0;
    }
    
    void encrypt ()
    {
             ofstream fout;
        string plaintext;
        string ciphertext;
        string cipherletter;
        string shift_string_1;
        string shift_string_2;
        char shift_letter;
        signed int cipher_num;
        signed int cipher_num_2;
        signed int shifter;
        signed int shift_num;
        char letter;
        cout<<"Enter your word here: \n";
        cin>>plaintext;
        signed int slength = plaintext.size();
        signed int loops = 0;
        signed int seed;
        fout.open("key.txt");
        fout.close();
        fout.open("key.txt",ios::app);
        fout<< "PLAINTEXT: " << plaintext << endl;
        fout<<"KEY:";
        while (loops < slength)
        {
              shifter = loops;
              if (loops == 0)
              {
                        shifter = slength;
              }
              if (loops % 2 < 1)
              {
                        shifter = shifter * -1;
              }
              else
              {
                        shifter = loops;
              }
              fout<<shifter;
              cipher_num = int(plaintext[loops]);
              cipher_num_2 = cipher_num + shifter;
              letter = char(cipher_num_2);
              cipherletter = letter;
              ciphertext.append(cipherletter);
              loops++;      
    }
        fout<< endl <<"CIPHERTEXT: " << ciphertext <<endl;
        fout.close();
        system("PAUSE");
    } 
    
    void decrypt ()
    {
    string encrypted;
    string key;
    cout<<"Please enter the encrypted text: \n";
    cin>> encrypted;
    system("CLS");
    cout<<"Please enter the key, without signs: \n";
    cin>>key;
    signed short int shifter;
    char letter;
    char new_letter;
    int cipher_num;
    int new_num;
    string deciphered;
    string holder;
    int slength;
    int shifted_letter_num;
    slength = encrypted.size();
    int loops = 0;
    int temp_num;
    while (loops < slength)
    {
          temp_num = key[loops];
          cipher_num = temp_num-48;
          cout<<cipher_num;
          if (loops % 2 < 1)
          {
                    cipher_num = cipher_num * -1;
          }
          shifted_letter_num = int(encrypted[loops]);  
          cout<<"ASCII of the encrypted text: " << shifted_letter_num << endl;
          new_num =shifted_letter_num - cipher_num;
          letter = char(new_num);
          holder = letter;
          cout<<"Decrypted letter: " << holder << endl;
          deciphered.append(holder);
          loops++;
    }
    cout<<"Decrytpion done. Decrypted text: " << deciphered << endl;
    system("PAUSE");
    }
    I'd be interested in feedback on the process... the code is functional, but inefficient, I know... I am teaching myself, and this was an interest of mine that I decided to use to further my capabilities. I'm writing a new one that uses a more complex cipher system, multiple words, etc; it also uses the ASCII method, since it is what I know :P

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    19
    Sorry about the bump... I was tired and missed the last post date.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 10:10 AM
  2. problem w/ doubles in friend's program
    By mkylman in forum C Programming
    Replies: 16
    Last Post: 11-22-2008, 09:45 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Problem with Program not Quitting
    By Unregistered in forum Windows Programming
    Replies: 20
    Last Post: 06-11-2002, 11:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21