Beispiel #1
0
gchar* decodeBase64(gchar *data, gint length, gint *actualLength) {
    gchar *input = data;
    gint correctedLength = getCorrectedEncodeSize(length);
    if(length != correctedLength) {
        input = g_malloc(correctedLength * sizeof(gchar));
        memset(input, 0, correctedLength);
        memcpy(input, data, length);
        memset(input + length, '=', correctedLength - length);
    }
    gchar *buffer = g_malloc(correctedLength);
    memset(buffer, 0, correctedLength);

    BIO *b64 = BIO_new(BIO_f_base64());
    BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

    BIO *bmem = BIO_new_mem_buf(input, correctedLength);
    BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL);
    bmem = BIO_push(b64, bmem);

    BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL);

    *actualLength = BIO_read(bmem, buffer, correctedLength);

    BIO_free_all(bmem);

    if(length != correctedLength) {
        g_free(input);
    }
    return buffer;
}
Beispiel #2
0
char *decode_base64(unsigned char *pInput, int pLength, int *pActualLength)
{
  // Needs All NO_NL flags for proper RSA AES KEY Descrypt
  BIO *b64, *bmem;
  unsigned char *input = pInput;
  int length = getCorrectedEncodeSize(pLength);
  if(pLength != length)
  {
    input = malloc(length * sizeof(unsigned char));
    memset(input, 0, length);
    memcpy(input, pInput, pLength);
    memset(input+pLength, '=', length-pLength);
    printf("Fixed value: [%.*s]\n", length, input);
  }
  char *buffer = (char *)malloc(length);
  memset(buffer, 0, length);

  b64 = BIO_new(BIO_f_base64());
  BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);

  bmem = BIO_new_mem_buf(input, length);
  BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL);
  bmem = BIO_push(b64, bmem);

  BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL);

  *pActualLength = BIO_read(bmem, buffer, length);

  BIO_free_all(bmem);

  if(pLength != length)
  {
    free(input);
  }
  return buffer;
}