本文整理汇总了C++中BIO_f_base64函数的典型用法代码示例。如果您正苦于以下问题:C++ BIO_f_base64函数的具体用法?C++ BIO_f_base64怎么用?C++ BIO_f_base64使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了BIO_f_base64函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int
main(int argc, char** argv)
{
// get data to encrypt.
//
if (argc != 3) {
printf("Usage: %s <base64 enc key> <data to encryt>\n",
argv[0]);
return 0;
}
// base64 decode key
//
BIO *mbio = BIO_new_mem_buf(argv[1], strlen(argv[1]));
BIO *b64bio = BIO_new(BIO_f_base64());
BIO_set_flags(b64bio, BIO_FLAGS_BASE64_NO_NL);
BIO *bio = BIO_push(b64bio, mbio);
char key[256];
size_t keylen = 0;
keylen = BIO_read(bio, key, sizeof(key));
BIO_free(mbio);
BIO_free(b64bio);
// encrypt the data
//
char out[256];
int outlen = 0;
EVP_CIPHER_CTX ctx;
EVP_EncryptInit(&ctx, EVP_aes_256_ecb(), key, NULL);
EVP_EncryptUpdate(&ctx, out, &outlen, argv[2], strlen(argv[2]));
EVP_EncryptFinal(&ctx, out, &outlen);
EVP_CIPHER_CTX_cleanup(&ctx);
// base64 encode encrypted data
//
mbio = BIO_new(BIO_s_mem());
b64bio = BIO_new(BIO_f_base64());
BIO_set_flags(b64bio, BIO_FLAGS_BASE64_NO_NL);
bio = BIO_push(b64bio, mbio);
BIO_write(bio, out, outlen);
BIO_flush(bio);
char* data = NULL;
size_t datalen = 0;
datalen = BIO_get_mem_data(mbio, &data);
data[datalen] = '\0';
printf("encrypted data: [%s]\n", data);
BIO_free(mbio);
BIO_free(b64bio);
return 0;
}
示例2: BIO_new
char *base64_enc(const char *str, int len)
{
BIO *bio, *b64;
BUF_MEM *bptr;
char *buf;
int ret;
b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bio);
ret = BIO_write(b64, str, len);
if (ret <= 0) {
buf = NULL;
goto err;
}
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
buf = malloc(bptr->length);
if (buf) {
memcpy(buf, bptr->data, bptr->length-1);
buf[bptr->length - 1] = 0;
}
err:
BIO_free_all(b64);
return buf;
}
示例3: s3_base64_encode
gchar*
s3_base64_encode(const GByteArray *to_enc) {
BIO *bio_b64 = NULL, *bio_buff = NULL;
long bio_b64_len;
char *bio_b64_data = NULL, *ret = NULL;
if (!to_enc) return NULL;
/* Initialize base64 encoding filter */
bio_b64 = BIO_new(BIO_f_base64());
g_assert(bio_b64);
BIO_set_flags(bio_b64, BIO_FLAGS_BASE64_NO_NL);
/* Initialize memory buffer for the base64 encoding */
bio_buff = BIO_new(BIO_s_mem());
g_assert(bio_buff);
bio_buff = BIO_push(bio_b64, bio_buff);
/* Write the MD5 hash into the buffer to encode it in base64 */
BIO_write(bio_buff, to_enc->data, to_enc->len);
/* BIO_flush is a macro and GCC 4.1.2 complains without this cast*/
(void) BIO_flush(bio_buff);
/* Pull out the base64 encoding of the MD5 hash */
bio_b64_len = BIO_get_mem_data(bio_buff, &bio_b64_data);
g_assert(bio_b64_data);
ret = g_strndup(bio_b64_data, bio_b64_len);
/* If bio_b64 is freed separately, freeing bio_buff will
* invalidly free memory and potentially segfault.
*/
BIO_free_all(bio_buff);
return ret;
}
示例4: BIO_new
char *base64(const unsigned char *input, int length)
{
BIO *bmem, *b64;
BUF_MEM *bptr;
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, magic, sizeof(magic)-1);
BIO_write(b64, (char*)salt, sizeof(salt));
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);
int chars = 0;
for (unsigned int i = 0; i < bptr->length-1; i++) {
if (bptr->data[i] == '+') { buff[chars] = '-'; chars++;}
else if (bptr->data[i] == '/') { buff[chars] = '_'; chars++; }
else if (bptr->data[i] != '=') { buff[chars] = bptr->data[i]; chars++; }
}
buff[chars] = 0;
BIO_free_all(b64);
return buff;
}
示例5: we
/**
b64decode
Decodes a base64-encoded message. You provide an encoded message,
and a pointer to somewhere we (the editorial we) can store the length
of the decoded message. A dynamically allocated pointer is returned.
*/
char *b64decode(char *encoded, size_t *newlen)
{
BIO *b64, *bmem;
char *decoded = NULL;
if(encoded == NULL) {
print_loc(); io_debug("NULL data passed! Bad coder! Bad!\n");
return NULL;
}
safe_malloc(decoded,*newlen);
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new_mem_buf(encoded, -1);
if(bmem == NULL || b64 == NULL) {
print_loc(); io_debug("Calls to libssl failed!\n");
abort(); //I don't think this will ever happen.
}
bmem = BIO_push(b64, bmem);
*newlen = BIO_read(bmem, decoded, *newlen);
BIO_free_all(bmem);
decoded[*newlen] = '\0';
return decoded;
}
示例6: decodeBase64
char* decodeBase64(unsigned char *input, size_t length, size_t *out_length) {
BIO *b64, *bmem;
size_t decodedLen;
char *buffer = (char*)NFCD_MALLOC(length+1);
if (buffer == NULL) {
return NULL;
}
memset(buffer, 0, length);
b64 = BIO_new(BIO_f_base64());
if (b64 == NULL) {
return NULL;
}
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bmem = BIO_new_mem_buf(input, length);
bmem = BIO_push(b64, bmem);
decodedLen = BIO_read(bmem, buffer, length);
buffer[decodedLen] = 0;
*out_length = decodedLen;
BIO_free_all(bmem);
return buffer;
}
示例7: base64Decode
static Buffer::Ptr base64Decode(T t) {
if (!t) return Buffer::null();
if (!t->length()) return Buffer::create();
std::string src = t->toStdString();
Size len = src.length();
if (len != t->length()) return Buffer::null();
BIO* bio = BIO_new(BIO_f_base64());
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
BIO* bioMem = BIO_new_mem_buf(const_cast<char*>(src.c_str()), len);
bio = BIO_push(bio, bioMem);
char* dst = new char[len + 1];
int readLen = BIO_read(bio, dst, len);
Buffer::Ptr decoded;
if (readLen > 0) {
decoded = Buffer::create(dst, readLen);
} else {
decoded = Buffer::null();
}
BIO_free_all(bio);
delete[] dst;
return decoded;
}
示例8: write_b64
int write_b64( const char *filename, unsigned char *out, int len )
{
int count;
BIO *b64, *file;
/* Create a buffered file BIO for writing */
file = BIO_new_file (filename, "w");
if (!file) {
/* FIXME - log an error */
return -1;
}
/* Create a base64 encoding filter BIO */
b64 = BIO_new (BIO_f_base64 ());
assert( b64 != NULL );
if( b64 == NULL ) {
/* FIXME - log an error */
return -1;
}
/* Assemble the BIO chain to be in the order b64-file */
BIO_push (b64, file );
count = write_to_BIO( b64, out, len );
assert( count == len );
BIO_free_all( b64 );
/* success */
return count;
}
示例9: clevis_buf_encode
json_t *
clevis_buf_encode(const clevis_buf_t *buf)
{
json_t *out = NULL;
BIO *mem = NULL;
BIO *b64 = NULL;
char *c = NULL;
int r = 0;
mem = BIO_new(BIO_s_mem());
if (!mem)
goto egress;
b64 = BIO_new(BIO_f_base64());
if (!b64)
goto egress;
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
if (!BIO_push(b64, mem))
goto egress;
r = BIO_write(b64, buf->buf, buf->len);
if (r != (int) buf->len)
goto egress;
BIO_flush(b64);
r = BIO_get_mem_data(mem, &c);
out = json_stringn(c, r);
egress:
BIO_free(b64);
BIO_free(mem);
return out;
}
示例10: base64Encode
static int base64Encode(char* output, const unsigned char* data, size_t length)
{
BIO* base64 = BIO_new(BIO_f_base64());
if (base64 == NULL) {
return -1;
}
BIO_set_flags(base64, BIO_FLAGS_BASE64_NO_NL);
BIO* memory = BIO_new(BIO_s_mem());
if (memory == NULL) {
BIO_free_all(base64);
return -1;
}
BIO* bio = BIO_push(base64, memory);
BIO_write(bio, data, length);
if (BIO_flush(bio) == -1) {
return -1;
}
const char* p;
size_t n = BIO_get_mem_data(memory, &p);
int i;
for (i = 0; i < n; ++i) {
if (*(p+i) == '+') {
*(output+i) = '-';
} else if (*(p+i) == '/') {
*(output+i) = '_';
} else if (*(p+i) == '=') {
break;
} else {
*(output+i) = *(p+i);
}
}
BIO_free_all(bio);
return n;
}
示例11: unbase64
static size_t unbase64(const char *bytes, char **unbase64)
{
size_t len;
BIO *memory, *b64;
char *buffer;
len = strlen(bytes);
if (!len)
goto error;
b64 = BIO_new(BIO_f_base64());
memory = BIO_new_mem_buf((char *)bytes, len);
if (!b64 || !memory)
goto error;
b64 = BIO_push(b64, memory);
if (!b64)
goto error;
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
buffer = xcalloc(len + 1, 1);
len = BIO_read(b64, buffer, len);
if ((int)len <= 0)
goto error;
buffer[len] = '\0';
BIO_free_all(b64);
*unbase64 = buffer;
return len;
error:
die("Could not unbase64 the given bytes.");
}
示例12: BIO_new
static char *base64(const char *bytes, size_t len)
{
BIO *memory, *b64;
BUF_MEM *buffer;
char *output;
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
memory = BIO_new(BIO_s_mem());
if (!b64 || !memory)
goto error;
b64 = BIO_push(b64, memory);
if (!b64)
goto error;
if (BIO_write(b64, bytes, len) < 0 || BIO_flush(b64) < 0)
goto error;
BIO_get_mem_ptr(b64, &buffer);
output = xmalloc(buffer->length + 1);
memcpy(output, buffer->data, buffer->length);
output[buffer->length] = '\0';
BIO_free_all(b64);
return output;
error:
die("Could not base64 the given bytes.");
}
示例13: base64_encode
/*
* des - base64编码,将二进制字符与64个可打印字符进行对应转化。 2^6 = 64,6bits对应一个字符,三个字节对应四个可见字符
* param - str : 需编码的数据
* str_len : str的长度
* encode : 编码后数据存储
* encode_len : encode缓冲区的长度,要求大小需大于str_len,建议为(str_len/3) * 4 + 8
* ret - success : 编码后数据实际长度
* fail : -1
*/
int base64_encode(char *str,int str_len,char *encode, u_int encode_len)
{
BIO *bmem,*b64;
BUF_MEM *bptr;
b64=BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bmem=BIO_new(BIO_s_mem());
b64=BIO_push(b64, bmem);
BIO_write(b64, str, str_len); //encode
BIO_flush(b64);
BIO_get_mem_ptr(b64,&bptr);
// printf("%d\n", bptr->length);
if(bptr->length > encode_len){
printf("encode_len too small\n");
return -1;
}
encode_len=bptr->length;
memcpy(encode,bptr->data,bptr->length);
encode[bptr->length] = '\0';
BIO_free_all(b64);
return encode_len;
}
示例14: decodeBase64
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;
}
示例15: memset
char *base64_dec(char *str, int len, int *result_len)
{
BIO *bio, *b64;
char *buf;
int ret;
if (!(buf = malloc(len)))
return NULL;
memset(buf, 0, len);
b64 = BIO_new(BIO_f_base64());
bio = BIO_new_mem_buf(str, len);
bio = BIO_push(b64, bio);
ret = BIO_read(bio, buf, len);
if (ret <= 0) {
free(buf);
buf = NULL;
}
BIO_free_all(bio);
if (result_len)
*result_len = ret;
return buf;
}