Thread: Beginners Question concerning mcrypt lib and string encoding

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    1

    Beginners Question concerning mcrypt lib and string encoding

    Hi all, first let me say sorry for my noobish attempts in creating a few lines of C code, i'm used to scripting languages as pyhon, perl, java(script), node and also php etc. but I never did anything serious in C.

    I've got an application here which connects over the internet to a webserver and sends some json strings. This is all working already.

    Now I want to encode one string via mcrypt (because it seemed the easiest library of all) AES and send it over to the other server where it should get decrypted again and checked for validity.

    I'll be using this sample code as "starting base". i've found it on the internet:





    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    /*
     * MCrypt API available online:
     * http://linux.die.net/man/3/mcrypt
     */
    #include <mcrypt.h>
    
    
    #include <math.h>
    #include <stdint.h>
    #include <stdlib.h>
    
    
    int encrypt(
        void* buffer,
        int buffer_len, /* Because the plaintext could include null bytes*/
        char* IV,
        char* key,
        int key_len
    ){
      MCRYPT td = mcrypt_module_open("rijndael-256", NULL, "cbc", NULL);
      int blocksize = mcrypt_enc_get_block_size(td);
      if( buffer_len % blocksize != 0 ){return 1;}
    
    
      mcrypt_generic_init(td, key, key_len, IV);
      mcrypt_generic(td, buffer, buffer_len);
      mcrypt_generic_deinit (td);
      mcrypt_module_close(td);
    
    
      return 0;
    }
    
    
    int decrypt(
        void* buffer,
        int buffer_len,
        char* IV,
        char* key,
        int key_len
    ){
      MCRYPT td = mcrypt_module_open("rijndael-256", NULL, "cbc", NULL);
      int blocksize = mcrypt_enc_get_block_size(td);
      if( buffer_len % blocksize != 0 ){return 1;}
    
    
      mcrypt_generic_init(td, key, key_len, IV);
      mdecrypt_generic(td, buffer, buffer_len);
      mcrypt_generic_deinit (td);
      mcrypt_module_close(td);
    
    
      return 0;
    }
    
    
    void display(char* ciphertext, int len){
      int v;
      for (v=0; v<len; v++){
        printf("%d", ciphertext[v]);
      }
      printf("\n");
    }
    
    
    int main()
    {
      MCRYPT td, td2;
      char * plaintext = "my secret test string of max 32 chars";
      char* IV = "AAAAAAAAAAAAAAAA";
      char *key = "0123456789abcdef";
      int keysize = 32; /* 128 bits */
      char* buffer;
      int buffer_len = 32;
    
    
      buffer = calloc(1, buffer_len);
      strncpy(buffer, plaintext, buffer_len);
    
    
      printf("==C==\n");
      printf("plain:   %s\n", plaintext);
      encrypt(buffer, buffer_len, IV, key, keysize);
      printf("cipher:  "); display(buffer , buffer_len);
      decrypt(buffer, buffer_len, IV, key, keysize);
      printf("decrypt: %s\n", buffer);
    
    
      return 0;
    }


    The code works as it is, it encodes my string into a ciphered text and displays the text via the display function to stdout.

    From previous projects I know I usually used Base64 for transporting strings via json, but in this example the string is encoded into "%d" - so decimals.. It works too, and I dont care if I send a base64 encoded string or these decimals but how would I calculate back the %d encoded string ? So how would a "undisplay()" class look alike ?


    Or maybe there is an easier way to transport the string and re-decode it ?

    I'd expect something like:

    base64string = base64encode(ciphertext);
    ...
    send base64string to host2
    ...
    at host2:
    ciphertext = base64decode(base64string)
    ...
    and then mcrypt_decode that ciphertext...


    Thanks for your help !

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But if you print 12345, how are you supposed to know whether that is "123" "45" or "12" "34" "5"

    You need to arrange for your printable serialisation to be decodable in an unambiguous fashion.

    There are a number of ways to do this (fixed width, delimiters), but you need to make a choice.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encoding a data structure based on TLV encoding
    By Sajas K K in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2013, 10:39 PM
  2. How to convert string in url encoding to html encoding?
    By Jerel2k11 in forum C Programming
    Replies: 6
    Last Post: 11-06-2011, 09:05 AM
  3. mcrypt function
    By stevfletchcom in forum C Programming
    Replies: 4
    Last Post: 06-03-2010, 10:13 AM
  4. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  5. Replies: 5
    Last Post: 09-10-2004, 07:34 PM