Thread: binary string => text string?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    41

    binary string => text string?

    I have a program that encrypts a password. I want to store the encrypted password in a text file. How do I do that? Is there some function I can use to automatically convert it to a text-based format and then back again when I need the binary? Base64 or what is best and whre do I get the functions from?

    Language is C or maybe C++

    I use Visual studio 2005

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    You could just write to and read from the file in binary mode (set the std::ios::binary option when you open the file). Would that do?

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    The following code assumes that the encrypted password is stored as an array of 10 characters, whis more than likely isn't quite the same as what you are using. Otherwise, this should be sufficiently generic.

    Code:
    #include <stdio.h>
    #include <assert.h>
    
    #define PASS_SIZE 10
    
    int main (void) {
       // The password is an arbitrary array of characters.
       unsigned char orig_pass[PASS_SIZE] = {176, 255, 0, 10, 73, 42, 5, 200, 0, 255};
       unsigned char restored_pass[PASS_SIZE];
       FILE * input, * output;
       int i;
    
       // Save the pass to a binary file.
       assert(output=fopen("passwd.key", "wb"));
       fwrite(orig_pass, PASS_SIZE, 1, output);
       fclose(output);
    
       // Read in the password from the binary file.
       assert(input=fopen("passwd.key", "rb"));
       fread(restored_pass, PASS_SIZE, 1, input);
       fclose(input);
    
       // Verify that the restored is equivalent to the original.
       for (i = 0; i < PASS_SIZE; ++i) {
          printf ("%d: %4u %4u\n", i, orig_pass[i], restored_pass[i]);
       }
       
       return 0;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Sorry, I didn't read the original question closely.

    Here is a basic algorithm for converting between a binary array and a text array.

    Code:
    #include <stdio.h>
    #include <assert.h>
    
    // Convert a binary password to a text password.
    // The output will take 2x bytes as the input.
    bin2txt (size_t pass_size, unsigned char const * input, unsigned char * output) {
       int i;
    
       // For each element of the binary password, we're going to strip out the
       // lower and upper 4 bits, and make each a character.
       for (i = 0; i < pass_size; ++i) {
          unsigned char low_nibble;
          unsigned char high_nibble;
    
          low_nibble = input[i] & 0x0F;
          high_nibble = input[i] >> 4;
    
          output[i * 2] = low_nibble + 'A';
          output[i * 2 + 1] = high_nibble + 'A';
       }
    }
    
    // Convert a text password to a binary password.
    // The output will take 1/2 bytes as the input.
    txt2bin (size_t pass_size, unsigned char const * input, unsigned char * output) {
       int i;
    
       // Form each element of the binary password by merging two characters
       // from the input together.
       for (i = 0; i < pass_size; ++i) {
          unsigned char low_nibble;
          unsigned char high_nibble;
    
          low_nibble = input[i * 2] - 'A';
          high_nibble = input[i * 2 + 1] - 'A';
    
          output[i] = low_nibble | (high_nibble << 4);
       }
    }
    
    #define PASS_SIZE 10
    
    int main (void) {
       // The password is an arbitrary array of characters.
       unsigned char orig_pass[PASS_SIZE] = {176, 255, 0, 10, 73, 42, 5, 30, 0, 255};
       unsigned char restored_pass[PASS_SIZE];
       char txt_version[PASS_SIZE * 2];
       int i;
    
       // Convert the pass to txt.
       bin2txt (PASS_SIZE, orig_pass, txt_version);
    
       printf ("Txt version:\n"); 
       for (i = 0; i < sizeof(txt_version); ++i)
          putchar(txt_version[i]);
       putchar('\n');
       putchar('\n');
    
       // Restore the pass from txt.
       txt2bin (PASS_SIZE, txt_version, restored_pass);
    
       printf ("Verification test.  Columns should be equal.\n");
       // Verify that the restored is equivalent to the original.
       for (i = 0; i < PASS_SIZE; ++i) {
          printf ("%d: %4u %4u\n", i, orig_pass[i], restored_pass[i]);
       }
       
       return 0;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM