0

I'm trying to use a small function to encode a "secret word" into a binary ciphered "string" and then convert this into base64 send it over the internet and decrypt it on the other side.

This is my code:

Encode Function:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base64.h"


#include <math.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-128", 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;
}

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 = "CA7D22F5D7BBB3291487D3EFD935E";
  char* IV = "AAAAAAAAAAAAABBB";
  char *key = "0123456789abcdef";
  int keysize = 32; /* 256 bits */
  char* buffer;
  int buffer_len = 32;
  int* ret;

  buffer = calloc(1, buffer_len);
  strncpy(buffer, plaintext, buffer_len);

  printf("plain:   %s\n", plaintext);

  encrypt(buffer, buffer_len, IV, key, keysize);
  char * mybase64 = base64(buffer, buffer_len, ret);
  printf("b64   : %s\n", mybase64);
  printf("cipher: "); display(buffer , buffer_len);
  printf("buffer: %s\n", buffer);

  return 0;
}

Decode Function:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base64.h"

#include <mcrypt.h>

#include <math.h>
#include <stdint.h>
#include <stdlib.h>

int decrypt(
    void* buffer,
    int buffer_len,
    char* IV,
    char* key,
    int keysize
){
  MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
  int blocksize = mcrypt_enc_get_block_size(td);
  if( buffer_len % blocksize != 0 ){return 1;}

  mcrypt_generic_init(td, key, keysize, 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;
  char* IV = "AAAAAAAAAAAAABBB";
  char *key = "0123456789abcdef";
  int keysize = 32; /* 256 bits */
  int buffer_len = 32;
  int* ret;


  char * mybase64 = "0DMkJ3UozhLfYjNav5Ih6rLd+aJua5KNTPHs/m2flLo=";

  char * buffer = unbase64(mybase64, (int)strlen(mybase64)+1, ret);


  printf("cipher: "); display(buffer , buffer_len);
  printf("buffer: %s\n", buffer);
  decrypt(buffer, buffer_len, IV, key, keysize);

  printf("plain: %s\n", buffer);

  return 0;
}

My used base64 functions: https://github.com/superwills/NibbleAndAHalf/blob/master/NibbleAndAHalf/base64.h

The strange thing is, the encoded binary data "looks" the same on the console, also in hex, but it does not get decoded correctly, its just weird characters.

Anyone got an idea ?

EDIT: This is the result when running a test of the program:.

Encode:

user@0F5NV:~/aes$ ./aes 
plain:   CA7D22F5D7BBB3291487D3EFD935E
b64   : EWQ2NxzOGCB1XGARxdfC7kLMGzAGJZ8/sEQf6dThxfk=
cipher: 17100545528-502432117929617-59-41-62-1866-522748637-9763-806831-23-44-31-59-7
buffer: d67? u\`????B?%???D?????

Decode:

user@0F5NV:~/aes$ ./unaes
b64   : EWQ2NxzOGCB1XGARxdfC7kLMGzAGJZ8/sEQf6dThxfk=
cipher: 17100545528-502432117929617-59-41-62-1866-522748637-9763-806831-23-44-31-59-7
buffer: d67? u\`????B?%???D?????
plain: ?Q?X?P??Qa?t?@???wi ?qa?Z

As you see the cipher and buffer are the same in Encoding and Decoding but not the plaintext... Why ?!

biergardener
  • 185
  • 1
  • 1
  • 11
  • Stackoverflow questions must be completely self-contained; it should be possible to make sense out of the question without having to follow external links. Please put everything ***in the question***, otherwise it will be closed. – Mike Nakis Feb 12 '15 at 12:53
  • Wow but this is quite a lot of code, I'll edit the message and put that into the message. – biergardener Feb 12 '15 at 12:58
  • @biergardener: are you sure there is no issue with your base64 function? – Giorgi Moniava Feb 12 '15 at 12:59
  • @gio: I used it several times before and never had a problem with it, but I could try openssl's function if you think this could it be – biergardener Feb 12 '15 at 13:01
  • @biergardener: I am not saying that just to exclude that you could first perform encryption/decryption purely of binary data - no base64 encoding – Giorgi Moniava Feb 12 '15 at 13:03
  • @gio: Hmm, the problem is I'm sending that data over the internet via JSON, i'm not sure how to send a binary string over there and not use base64 :) I could try it within the same script probably.. – biergardener Feb 12 '15 at 13:04

0 Answers0