I am using openssl libraries for purpose of encoding and decoding to base64, This is my encryption code
#include <openssl/buffer.h>
#include <stdlib.h>
char *base64(const unsigned char *input, int length);
int main(int argc, char **argv)
{
char nonce[10];
srand(time(NULL));
printf("rand():%d\n", rand());
sprintf(nonce, "%d", rand());
char *output = base64(nonce, sizeof(nonce));
printf("Base64: *%s*\n", output);
free(output);
}
char *base64(const unsigned char *input, int length)
{
BIO *bmem, *b64;
BUF_MEM *bptr;
char *buff;
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, input, length);
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
buff = (char *) malloc(bptr->length);
memcpy(buff, bptr->data, bptr->length-1);
buff[bptr->length-1] = '\0';
BIO_free_all(b64);
return buff;
}
The o/p of this when was NjI0MjQ3MwAECA== the rand() output was 1308702736 (This is a an example for one instance of rand gen o/p), when i used the decode function to decode this value i get 6242473, which is totally diff, i have to get 1308702736 when decoded,
My decode function is as follows
#include <string.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
char *unbase64(unsigned char *input, int length);
int main(int argc, char **argv)
{
char *output = unbase64("NjI0MjQ3MwAECA==\n", strlen("NjI0MjQ3MwAECA==\n"));
printf("Unbase64: *%s*\n", output);
free(output);
}
char *unbase64(unsigned char *input, int length)
{
BIO *b64, *bmem;
char *buffer = (char *)malloc(length);
memset(buffer, 0, length);
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new_mem_buf(input, length);
bmem = BIO_push(b64, bmem);
BIO_read(bmem, buffer, length);
BIO_free_all(bmem);
return buffer;
}
NOTE: to compile this code v have to use -lcrypto
I need a help in solving this i am stuck here, googled for solutions but didn’t get any,and have one more doubt why input to base64 decoder b terminated by \n ? can one one help me out in this
You call
randtwice, giving you two different numbers.NjI0MjQ3MwAECA==decodes to624247.