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; }
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; }
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; }
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; }
/** 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; }
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; }
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; }
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; }
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; }
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; }
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."); }
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."); }
/* * 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; }
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; }
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; }
/* caller must free the returned string */ char *base64_dec(unsigned char *in, int size) { BIO *bio64, *biomem; char *buf=NULL; buf = malloc(sizeof(char) * size); bzero(buf, size); if ((bio64 = BIO_new(BIO_f_base64())) == NULL) { logprintfl(EUCAERROR, "BIO_new(BIO_f_base64()) failed\n"); } else { BIO_set_flags (bio64, BIO_FLAGS_BASE64_NO_NL); /* no long-line wrapping */ if ((biomem = BIO_new_mem_buf(in, size)) == NULL) { logprintfl(EUCAERROR, "BIO_new_mem_buf() failed\n"); } else { biomem = BIO_push(bio64, biomem); if ((BIO_read(biomem, buf, size)) <= 0) { logprintfl(EUCAERROR, "BIO_read() read failed\n"); } // BIO_free_all(biomem); } BIO_free_all(bio64); } return buf; }
int alloc_cipher_BIOs( BIO **buffer, BIO **b64, BIO **cipher ) { *buffer = *b64 = *cipher = NULL; /* Create a buffering filter BIO to buffer writes to the file */ *buffer = BIO_new (BIO_f_buffer ()); if( buffer == NULL ) { return -1; } /* Create a base64 encoding filter BIO */ *b64 = BIO_new (BIO_f_base64 ()); if( *b64 == NULL ) { BIO_free( *buffer ); *buffer = NULL; return -1; } *cipher = BIO_new (BIO_f_cipher ()); if( *cipher == NULL ) { BIO_free( *buffer ); BIO_free( *b64 ); *buffer = *b64 = NULL; return -1; } /* success */ return 0; }
char *EstEID_base64Encode(const char *input, int length) { BIO *memBio; BIO *b64Bio; char *b; int len; char *result; LOG_LOCATION; memBio = BIO_new(BIO_s_mem()); b64Bio = BIO_new(BIO_f_base64()); b64Bio = BIO_push(b64Bio, memBio); BIO_write(b64Bio, input, length); (void)BIO_flush(b64Bio); len = BIO_get_mem_data(memBio, &b); result = (char *)malloc(len + 1); strncpy(result, b, len); result[len] = 0; BIO_free_all(b64Bio); while (result[--len] == '\n') result[len] = 0; return result; }
int read_b64( const char *filename, unsigned char *out, int len ) { int count; BIO *b64, *buffer, *file; /* Create a buffered file BIO for reading */ file = BIO_new_file (filename, "r"); if (!file) { /* FIXME - log an error */ return -1; } /* Create a base64 encoding filter BIO */ b64 = BIO_new (BIO_f_base64 ()); if( b64 == NULL ) { /* FIXME - log an error */ return -1; } buffer = BIO_new (BIO_f_buffer ()); /* Assemble the BIO chain to be in the order b64-file */ BIO_push (b64, buffer ); BIO_push( buffer, file ); /* load the data */ count = read_from_BIO( b64, out, len ); assert( count == len ); BIO_free_all( b64 ); /* success */ return count; }
/* * Base64 encode a buffer passed in. Return a talloc allocated string. */ static char *base64_enc(void *ctx, const char *buf, size_t size) { BIO *b64_filter = BIO_new(BIO_f_base64()); BIO *mem_ssink = BIO_new(BIO_s_mem()); BUF_MEM *bio_mem = NULL; int ret = 0; char *ret_str; b64_filter = BIO_push(b64_filter, mem_ssink); ret = BIO_write(b64_filter, buf, size); if (ret < 0 || ret != size) { DEBUG(0, ("Unable to write all data to b64_filter: %s\n", strerror(errno))); } ret = BIO_flush(b64_filter); BIO_get_mem_ptr(b64_filter, &bio_mem); /* This should append a terminating null to turn it into a string */ ret_str = talloc_strndup(ctx, bio_mem->data, bio_mem->length); BIO_free_all(b64_filter); return ret_str; }
char* encodeBase64(const char *input, size_t length) { if(length > 0) { 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, input, length); BIO_flush(b64); BIO_get_mem_ptr(b64, &bptr); char *buff = (char *)NFCD_MALLOC(bptr->length+1); memcpy(buff, bptr->data, bptr->length); buff[bptr->length] = 0; BIO_free_all(b64); return buff; } else { return ""; } }
uint8_t *base64_dec(char *input, int *outlen) { BIO *bmem, *b64; int inlen = strlen(input); 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); // Apple cut the padding off their challenges; restore it BIO_write(bmem, input, inlen); while (inlen++ & 3) BIO_write(bmem, "=", 1); BIO_flush(bmem); int bufsize = strlen(input) * 3 / 4 + 1; uint8_t *buf = malloc(bufsize); int nread; nread = BIO_read(b64, buf, bufsize); BIO_free_all(bmem); *outlen = nread; return buf; }
char *StringEncodeBase64(const char *str, size_t len) { assert(str); if (!str) { return NULL; } if (len == 0) { return xcalloc(1, sizeof(char)); } BIO *b64 = BIO_new(BIO_f_base64()); BIO *bio = BIO_new(BIO_s_mem()); b64 = BIO_push(b64, bio); BIO_write(b64, str, len); if (!BIO_flush(b64)) { assert(false && "Unable to encode string to base64" && str); return NULL; } BUF_MEM *buffer = NULL; BIO_get_mem_ptr(b64, &buffer); char *out = xcalloc(1, buffer->length); memcpy(out, buffer->data, buffer->length - 1); out[buffer->length - 1] = '\0'; BIO_free_all(b64); return out; }
/** * @brief Base64 decode input string \p in of size \p insize. * @returns -1 on invalid Base64, or 0 on successes in which case a * newly allocated binary string is set in out (and size). */ static int rd_base64_decode (const rd_chariov_t *in, rd_chariov_t *out) { size_t asize; BIO *b64, *bmem; if (in->size == 0 || (in->size % 4) != 0) return -1; asize = (in->size * 3) / 4; /* allocation size */ out->ptr = rd_malloc(asize+1); b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bmem = BIO_new_mem_buf(in->ptr, (int)in->size); bmem = BIO_push(b64, bmem); out->size = BIO_read(bmem, out->ptr, (int)asize+1); assert(out->size <= asize); BIO_free_all(bmem); #if ENABLE_DEVEL /* Verify that decode==encode */ { char *encoded = rd_base64_encode(out); assert(strlen(encoded) == in->size); assert(!strncmp(encoded, in->ptr, in->size)); rd_free(encoded); } #endif return 0; }
/** b64encode Encodes raw data in base64. Pass the data and the length of the data. b64encode returns the base64-encoded data as a dynamically allocated char *. */ char *b64encode(char *data, size_t datalen) { BIO *b64, *bmem; BUF_MEM *bptr; char *encoded = NULL; b64 = BIO_new(BIO_f_base64()); bmem = BIO_new(BIO_s_mem()); 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); BIO_write(bmem, data, datalen); BIO_flush(bmem); BIO_get_mem_ptr(bmem, &bptr); BIO_set_close(bmem, BIO_NOCLOSE); safe_malloc(encoded, bptr->length); memcpy(encoded, bptr->data, bptr->length); encoded[bptr->length] = '\0'; BIO_free_all(bmem); BUF_MEM_free(bptr); return encoded; }
uint8_t* OT_base64_decode(const char *input, size_t* out_len, int bLineBreaks) { BIO *bmem = NULL, *b64 = NULL; OT_ASSERT(NULL != input); int in_len = strlen(input); int out_max_len=(in_len*6+7)/8; unsigned char *buf = new unsigned char [out_max_len]; OT_ASSERT(NULL != buf); memset(buf, 0, out_max_len); b64 = BIO_new(BIO_f_base64()); if (b64) { if (!bLineBreaks) { BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); } bmem = BIO_new_mem_buf((char*)input, in_len); b64 = BIO_push(b64, bmem); *out_len = BIO_read(b64, buf, out_max_len); BIO_free_all(b64); } else { OT_ASSERT_MSG(false, "Failed creating new Bio in base64_decode.\n"); } return buf; }
int fillSeedSIV(void *siv, size_t sivSize, void *content, size_t contentLength, size_t start) { int read = 0, i; void *skip = NULL; BIO *b64 = NULL, *bio = BIO_new_mem_buf(content, contentLength); if(bio == NULL) goto fill_end; b64 = BIO_new(BIO_f_base64()); bio = BIO_push(b64, bio); if(start != 0) { skip = malloc(start); read = BIO_read(bio, skip, start); if(read != start) { read = 0; goto fill_end; } } read = BIO_read(bio, siv, sivSize); for(i = 0; i < read; ++i) { if((((unsigned char*)siv)[i] & 0xf0) == 0) ((unsigned char*)siv)[i] |= 0x30; } fill_end: if(skip != NULL) free(skip); if(bio != NULL) BIO_free_all(bio); return read; }
Impl() : m_base64(BIO_new(BIO_f_base64())) , m_sink(BIO_new(BIO_s_mem())) { // connect base64 transform to the data sink. BIO_push(m_base64, m_sink); }
char *PICA_id_to_base64(const unsigned char *id, char *buf) { BIO *biomem, *b64; static char localbuf[PICA_ID_SIZE * 2]; char *sourcebuf, *outputbuf = buf; long b64len; b64 = BIO_new(BIO_f_base64()); biomem = BIO_new(BIO_s_mem()); biomem = BIO_push(b64, biomem); BIO_write(biomem, id, PICA_ID_SIZE); BIO_flush(biomem); b64len = BIO_get_mem_data(biomem, &sourcebuf); if (outputbuf == NULL) outputbuf = localbuf; memcpy(outputbuf, sourcebuf, b64len); *strchr(outputbuf, '\n') = '\0'; outputbuf[b64len] = '\0'; BIO_free_all(biomem); return outputbuf; }
/* caller must free the returned string */ char * base64_enc (unsigned char * in, int size) { char * out_str = NULL; BIO * biomem, * bio64; if ( (bio64 = BIO_new (BIO_f_base64 ())) == NULL) { logprintfl (EUCAERROR, "error: BIO_new(BIO_f_base64()) failed\n"); } else { BIO_set_flags (bio64, BIO_FLAGS_BASE64_NO_NL); /* no long-line wrapping */ if ( (biomem = BIO_new (BIO_s_mem ())) == NULL) { logprintfl (EUCAERROR, "error: BIO_new(BIO_s_mem()) failed\n"); } else { bio64 = BIO_push (bio64, biomem); if ( BIO_write (bio64, in, size)!=size) { logprintfl (EUCAERROR, "error: BIO_write() failed\n"); } else { BUF_MEM * buf; (void) BIO_flush (bio64); BIO_get_mem_ptr (bio64, &buf); if ( (out_str = malloc(buf->length+1)) == NULL ) { logprintfl (EUCAERROR, "error: out of memory for Base64 buf\n"); } else { memcpy (out_str, buf->data, buf->length); out_str [buf->length] = '\0'; } } } BIO_free_all (bio64); /* frees both bio64 and biomem */ } return out_str; }