Search code examples
cpasswordsdecodedecoding

How to convert binary to sentence


I've created a simple Program to store and Retrieve the Passwords whenever I want.

For example, I have one password which is Pass so its binary will be 01010000011000010111001101110011. I've created a program that will convert it to binary and will store it in the file. The Program is as given Below:

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
    FILE *fptr;
    char wname[100];
    char uname[100];
    char pass[100];
    char str[50];
    char bin[7];
    int c2n,i,ii;
    clrscr();
    printf("\nEnter website name: ");
    fflush(stdin);
    gets(wname);
    printf("\nEnter the username: ");
    fflush(stdin);
    gets(uname);
    printf("\nEnter password: ");
    fflush(stdin);
    gets(pass);
    fptr=fopen("DND","a");
    printf("Binary of entered string is : ");
    fprintf(fptr,"\n");
    for(i=0;i<=strlen(wname)-1;i++)
    {
        c2n=wname[i];
        for(ii=7;ii>=0;ii--)
        {
            bin[ii]=c2n%2;
            c2n=c2n/2;
        }
        for(ii=0;ii<=7;ii++)
        {
            printf("%d",bin[ii]);
            fprintf(fptr,"%d",bin[ii]);
        }
    }
    printf(":");
    fprintf(fptr,":");
    for(i=0;i<=strlen(uname)-1;i++)
    {
        c2n=uname[i];
        for(ii=7;ii>=0;ii--)
        {
            bin[ii]=c2n%2;
            c2n=c2n/2;
        }
        for(ii=0;ii<=7;ii++)
        {
            printf("%d",bin[ii]);
            fprintf(fptr,"%d",bin[ii]);
        }
    }
    printf(":");
    fprintf(fptr,":");
    for(i=0;i<=strlen(pass)-1;i++)
    {
        c2n=pass[i];
        for(ii=7;ii>=0;ii--)
        {
            bin[ii]=c2n%2;
            c2n=c2n/2;
        }
        for(ii=0;ii<=7;ii++)
        {
            printf("%d",bin[ii]);
            fprintf(fptr,"%d",bin[ii]);
        }
    }
    fclose(fptr);
    printf("\n Your website name,user name and password are protected in binary....\nPress any key to close coder......");
    getch();
    return 0;
}

Now, I want to create Decoder for this.I want a program to convert all the binary to one sentence. For example.

01010000011000010111001101110011=Pass

I have no Idea how to create it.


Solution

  • Here's something that works for me.

    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
       int ch1 = 0;
       int ch2 = 0;
       int count = 0;
    
       // Pass the binary file to be read from as the first argument.
       char* file = argv[1];
    
       FILE* in = fopen(file, "r");
       if ( in == NULL )
       {
          printf("Unable to open file %s\n", file);
          return 1;
       }
    
       while ( (ch1 = fgetc(in)) != EOF )
       {
          if ( ch1 == '\n' )
          {
             // Reset counters.
             ch2 = 0;
             count = 0;
             fputc(ch1, stdout);
          }
          else if ( ch1 == ':' )
          {
             // Skip it for computing characters.
             fputc(ch1, stdout);
          }
          else
          {
             ch2 <<= 1;
             if ( ch1 == '1' )
             {
                ch2 += 1;
             }
             count++;
             if ( count == 8 )
             {
                fputc(ch2, stdout);
                count = 0;
             }
          }
       }
    
       fputc('\n', stdout);
       fclose(in);
    
       return 0;
    }