Jump to content

Substitution cipher

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Tumour

Tumour

    Newbie

  • Members
  • Pip
  • 9 posts
I am writing a substitution cipher which simply replaces any lower case letter with a predetermined number. But when I try to decode it using a decoder it gets stuck in an infinite loop. Can you figure out whats wrong with the decoder.

Encoder


	

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<conio.h>

 

int main(void)

{

    int i,j;

    char c[]="abcdefghijklmnopqrstuvwxyz ",t;

    char d[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};

    FILE *fp;

 

    fp=fopen("output.txt","wb+");

         

        if(fp==NULL)

        {

                puts("Cannot open file");

                exit(0);

         }

     

    while((t=getchar())!=EOF)

    {

         

        i=0;

        while(c[i]!=t)

            i++;

                 

        putc(d[i],fp);

         

    }

    fclose(fp);

}


Decoder

	

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<conio.h>

 

int main(void)

{

    int i,j;

    char c[]="abcdefghijklmnopqrstuvwxyz ",t;

    char d[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};

    FILE *fp;

 

    fp=fopen("output.txt","rb+");

    if(fp==NULL)

        {

            printf("Cannot open file");

                        exit(0);

         }

     

     

    while((t=getc(fp))!=EOF)

    {

         

        i=0;

        while(d[i]!=t)

            i++;

         

        fseek(fp,-1L,1);

         

        putc(c[i],fp);

         

    }

    fclose(fp);

} 


#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Try writing to a different output file and see if that changes anything.
sudo rm -rf /

#3
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Im not quite C programmer - more c++ but I think problem is :
i=0;

        while(d[i]!=t)

            i++;

In my opinion 'i' will increment untill it reaches integer range, that is : 4 294 967 295, then program should crash.
I have similar program, working well, on my computer but its written in c++.
Remebre about KISS & DRY

#4
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
If I'm reading it right, your substitution should have a fixed length. You have a single digit for some, and a double digit for others. I don't think you will be able to decrypt it, because something like 11, could be "l" or "aa".

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
No, the OP isn't writing number strings. Rather, they're single bytes with those numeric values.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users