0

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.

Adarsh Sojitra
  • 2,059
  • 1
  • 27
  • 50

3 Answers3

1

I think a good way to do this is to make use of the bitwise operations provided by the language. Check out this bit of code:

#include <stdio.h>

int main(){
  char buf = 0;
  int bufi = 0;
  int bit;
  FILE *fp;
  fp = fopen("bin", "r");
  if (!fp){
    printf("%s","input file failed to open\n");
    return -1;
  }
  while ((bit = getc(fp)) != EOF){
    if (bit == '0')
      bit = 0;
    else
      bit = 1;
    buf = buf | (bit << (7 - bufi));
    bufi++;
    if (bufi == 8){
      printf("%c", buf);
      bufi = buf = 0;
      int i;
    }
  }
  printf("%s","\n");
  return 0;
}

Hope this helps. Also check this out for more about bitwise operations:

http://en.wikipedia.org/wiki/Bitwise_operation

  • The question is tagged as "c"; however the code is in C++. As an observation, I think equivalently saying `'0'` expresses the meaning more clearly than `48`. – FooF May 26 '14 at 06:09
  • I would add a test to skip the `:`s. – R Sahu May 26 '14 at 06:25
1

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;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

Everything inside your computer is already binary. There is usually no reason to convert the binary to strings. If you want to print in binary format, something simple as this will do:

void putc_bin (char ch)
{
  for(uint8_t i=0; i<8; i++)
  {
    printf("%d", (ch & 0x80) ? '1' : '0');
    ch <<= 1;
  }
}
Lundin
  • 195,001
  • 40
  • 254
  • 396