'programming'에 해당되는 글 387건

  1. 2017.11.23 openssl base64

openssl base64

programming/C_C++ 2017. 11. 23. 15:06
반응형

 

https://www.openssl.org/docs/man1.1.0/crypto/BIO_f_base64.html

BIO_f_base64

NAME

BIO_f_base64 - base64 BIO filter

SYNOPSIS

 #include <openssl/bio.h>
 #include <openssl/evp.h>

 const BIO_METHOD *BIO_f_base64(void);

DESCRIPTION

BIO_f_base64() returns the base64 BIO method. This is a filter BIO that base64 encodes any data written through it and decodes any data read through it.

Base64 BIOs do not support BIO_gets() or BIO_puts().

BIO_flush() on a base64 BIO that is being written through is used to signal that no more data is to be encoded: this is used to flush the final block through the BIO.

The flag BIO_FLAGS_BASE64_NO_NL can be set with BIO_set_flags() to encode the data all on one line or expect the data to be all on one line.

NOTES

Because of the format of base64 encoding the end of the encoded block cannot always be reliably determined.

RETURN VALUES

BIO_f_base64() returns the base64 BIO method.

EXAMPLES

Base64 encode the string "Hello World\n" and write the result to standard output:

 BIO *bio, *b64;
 char message[] = "Hello World \n";

 b64 = BIO_new(BIO_f_base64());
 bio = BIO_new_fp(stdout, BIO_NOCLOSE);
 BIO_push(b64, bio);
 BIO_write(b64, message, strlen(message));
 BIO_flush(b64);

 BIO_free_all(b64);

Read Base64 encoded data from standard input and write the decoded data to standard output:

 BIO *bio, *b64, *bio_out;
 char inbuf[512];
 int inlen;

 b64 = BIO_new(BIO_f_base64());
 bio = BIO_new_fp(stdin, BIO_NOCLOSE);
 bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
 BIO_push(b64, bio);
 while((inlen = BIO_read(b64, inbuf, 512)) > 0)
        BIO_write(bio_out, inbuf, inlen);

 BIO_flush(bio_out);
 BIO_free_all(b64);

BUGS

The ambiguity of EOF in base64 encoded data can cause additional data following the base64 encoded block to be misinterpreted.

There should be some way of specifying a test that the BIO can perform to reliably determine EOF (for example a MIME boundary).

Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the OpenSSL license (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at https://www.openssl.org/source/license.html.

 


/*** http://onestone73.blog.me/110110773514 ***/

char *Base64Encode( char *input, int length)
{
  BIO *bmem, *b64;
  BUF_MEM *bptr;

  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);

  char *buff = (char *)malloc(bptr->length);

  memcpy(buff, bptr->data, bptr->length-1);
  buff[bptr->length-1] = 0;

  BIO_free_all(b64);

  return buff;
}

char *Base64Decode( 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;
}

 


http://blog.naver.com/websearch/70170040729

[openssl] base64 디코딩 예제

openssl 을 이용한 base64 문자열 디코딩 예제는 다음과 같습니다.

#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
 

int Base64Decode( char * pszInput, int iInputLen, char * pszOutput, int iOutputLen )
{
 BIO *b64, *bmem;
 
 b64 = BIO_new(BIO_f_base64());
 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
 bmem = BIO_new_mem_buf(pszInput, iInputLen);
 bmem = BIO_push(b64, bmem);
 
  int n = BIO_read(bmem, pszOutput, iOutputLen);
 
  BIO_free_all(bmem);

 return n;
}

 

 

반응형
Posted by 공간사랑
,