C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 23rd, 2006, 09:43 AM
tuck00 tuck00 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 196 tuck00 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 14 h 37 m 9 sec
Reputation Power: 9
C Cipher decoding program -- All I get out is gibberish!

I'm doing this project for Math 1, and so far it only creates a key that will be used for decoding (I'm doing the decoding part first, then adding on the encoding becuase it's basically the same...just backwards.) It's of the playfair cipher (you can get some information here.) I just started learning C++, but I haven't gotten to the object orientated part yet, so it may not be as efficient as it could be, and I also notice that the functions I already have could be merged into one, but I need to focus on finishing first. When I output the 2D array (of the key,) all I get is gibberish. Could you tell me what I've got wrong?

PHP Code:
#include <iostream>
#include <string.h>
using namespace std;

void shift(int ychar arr[])
{
  for (
int x=yx<strlen(arr)-yx++)
  {
    
arr[x] = arr[x+1];
  }
}

void copy(char arr1[], char arr2[])
{
  for(
int x=0x<strlen(arr2); x++)
  {
    
arr2[x] = arr1[x];
  }
}

void prepare(char arr1[], char arr2[])
{
  for(
int x=0x<strlen(arr2); x++)
  {
    
arr1[x] = arr2[x];
  }
}


int main ()
{
  
int ini;

  
cout<<"Tucker Connelly\nMath 1\nSept.18, 2006\n\n\n";
  
cout<<"Welcome to my Math 1 project.  In my program (written in C++ using Dev-C++ 4\nfor compiling)";
  
cout<<"I'll use simple algorithms to encode and decode a famous \ncipher called the Playfair Cipher developed in Britian in the mid 19th century\n\n";
  
cout<<"Menu:  1 -- Encode\n";
  
cout<<"       2 -- Decode\n";
  
cout<<"       3 -- Info on Playfair Cipher\n";
  
cout<<"       4 -- Challenges\n" ;
  
cout<<"       0 -- Exit\n";
  
cin>> ini;
    switch (
ini)
    {
      case 
0:
        
cout<<"Thank you for using my playfair cipher program.";
        
cin.get();
        break;
      case 
1:
        break;
      case 
2:
        
char code[100];
        
char ikey[50];
        
char key[50];
        
char nalpha[25];
        
char alpha[25];
        
alpha[0] = 'a';
        
alpha[1] = 'b';
        
alpha[2] = 'c';
        
alpha[3] = 'd';
        
alpha[4] = 'e';
        
alpha[5] = 'f';
        
alpha[6] = 'g';
        
alpha[7] = 'h';
        
alpha[8] = 'i';
        
alpha[9] = 'k';
        
alpha[10] = 'l';
        
alpha[11] = 'm';
        
alpha[12] = 'n';
        
alpha[13] = 'o';
        
alpha[14] = 'p';
        
alpha[15] = 'q';
        
alpha[16] = 'r';
        
alpha[17] = 's';
        
alpha[18] = 't';
        
alpha[19] = 'u';
        
alpha[20] = 'v';
        
alpha[21] = 'w';
        
alpha[22] = 'x';
        
alpha[23] = 'y';
        
alpha[24] = 'z';
        
char alphad[5][5];
        
cout<<"\nEnter the key phrase(50 characters or less please):\n";
        
cin>>ikey;
        for(
int n=0n<strlen(ikey); n++)  //initiates first for loop -- goes as many loops as the size of ikey array
        
{
          for(
int m=n+1m<strlen(ikey); m++)  //initiates second for loop -- loops comparison between all letters
          
{
            if(
ikey[n] == ikey[m])  //comparison of first letter to second letter
            
{
              
shift(m,ikey);
              
char key[strlen(ikey)-1];  //creates second key array with one less slot than first key array
              
copy(ikey,key);
              
char ikey[strlen(key)];   //prepares ikey for next loop through
              
prepare(ikey,key);
            }
          }
        }
        for(
int a=0c=0c<25a++)   //for loop initiating creation of 2D array (first dimension -- a)
        
{
          for(
int b=0;b<4b++, c++) //assigns values for 0 - 4 for dimension a
          
{
            if(
c<=strlen(key))  //checks if there are any letters left in key
            
{
              
alphad[a][b] = key[c];
              for(
int e=ce<25e++) //loops a check of duplicates
              
{
                if (
key[c] == alpha[e])   //deletes if a value in alpha matches alphad
                
{
                  
shift(calpha);
                  
char nalpha[strlen(alpha-1)];
                  
copy(alphanalpha);
                  
char alpha[strlen(nalpha)];
                  
prepare(alphanalpha);
                }
                else if(
c>strlen(key))
                {
                  
alphad[a][b] = nalpha[c];
                }
              }
            }
          }
        }
        for(
int a=0a<5a++)
        {
          for(
int b=0b<5b++)
          {
            
cout<<alphad[a][b];
          }
        }
        
cout<<"\nEnter the text you'd like to be decoded (100 characters or less please):\n";
        
cin>>code;

        break;
      case 
3:
        break;
      case 
4:
        break;
      default:
        
cout<<"Invalid input, please select from the menu above";
        
cin.get();
        break;
    }
  
cin.get();
  return 
0;


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C Cipher decoding program -- All I get out is gibberish!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap