Encodage et Décodage Base64
Un buffer sans saut de ligne final ne peut être décodé que si le flag BIO_FLAGS_BASE64_NO_NL est actif.
Pour le format base64 multi-lignes, la taille maximale de chaque ligne encodée est de 80 caractères.
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <string.h>
int main
(int argc,
char* argv
[])
{
char message
[]=
"Locis in iuris navigerum provinciae emergunt quoque Iudaeis sorte Iudaeis "
"ad suapte has multiplicium flumen visitur navigerum domitis his nusquam vi"
"situr nusquam et medelarum locis sorte.\nLocis suapte formavit sed quoque "
"provinciae locis Hierosolymis usus provinciae delata flumen visitur et sed"
" aptae medelarum verum regiones usus speciem pari suapte.\n";
char *buff=
NULL;
int bsize=
400;
char *data;
int i;
buff=
(char *
)OPENSSL_malloc
(EVP_ENCODE_LENGTH
(bsize
));
if (buff ==
NULL)
goto end;
BIO* b64=
NULL;
if ((b64=BIO_new
(BIO_f_base64
())) ==
NULL)
goto end;
BIO_set_flags
(b64, BIO_FLAGS_BASE64_NO_NL
);
// encode
BIO *in=BIO_new
(BIO_s_mem
());
in=BIO_push
(b64,in
);
BIO_write
(in,message,
sizeof(message
));
BIO_flush
(in
);
i = BIO_get_mem_data
(in,&data
);
memcpy
(buff,data,
(i>bsize?bsize:i
));
// copy data to buffer
buff
[(i>bsize?bsize:i
)]=
'\0';
printf("--encoded\n%s\n",buff
);
// decode
BIO *out=BIO_new
(BIO_s_mem
());
BIO_write
(out,buff,strlen
(buff
));
out=BIO_push
(b64,out
);
i=BIO_read
(out,
(char *
)buff,bsize
);
printf("--decoded\n%s\n",buff
);
end:
if (buff !=
NULL) OPENSSL_free
(buff
);
return 0;
}
sans BIO_FLAGS_BASE64_NO_NL
--encoded
TG9jaXMgaW4gaXVyaXMgbmF2aWdlcnVtIHByb3ZpbmNpYWUgZW1lcmd1bnQgcXVv
cXVlIEl1ZGFlaXMgc29ydGUgSXVkYWVpcyBhZCBzdWFwdGUgaGFzIG11bHRpcGxp
Y2l1bSBmbHVtZW4gdmlzaXR1ciBuYXZpZ2VydW0gZG9taXRpcyBoaXMgbnVzcXVh
bSB2aXNpdHVyIG51c3F1YW0gZXQgbWVkZWxhcnVtIGxvY2lzIHNvcnRlLgpMb2Np
cyBzdWFwdGUgZm9ybWF2aXQgc2VkIHF1b3F1ZSBwcm92aW5jaWFlIGxvY2lzIEhp
ZXJvc29seW1pcyB1c3VzIHByb3ZpbmNpYWUgZGVsYXRhIGZsdW1lbiB2aXNpdHVy
IGV0IHNlZC
--decoded
Locis in iuris navigerum provinciae emergunt quoque Iudaeis sorte Iudaeis ad suapte has multiplicium flumen visitur navigerum dom
itis his nusquam visitur nusquam et medelarum locis sorte.
Locis suapte formavit sed quoque provinciae locis Hierosolymis usus provinciae delata flumen visitur et sed aptae medelarum verum regiones usus speciem pari suapte.
output avec BIO_FLAGS_BASE64_NO_NL
--encoded
TG9jaXMgaW4gaXVyaXMgbmF2aWdlcnVtIHByb3ZpbmNpYWUgZW1lcmd1bnQgcXVvcXVlIEl1ZGFlaXMgc29ydGUgSXVkYWVpcyBhZCBzdWFwdGUgaGFzIG11bHRpcGxpY2
l1bSBmbHVtZW4gdmlzaXR1ciBuYXZpZ2VydW0gZG9taXRpcyBoaXMgbnVzcXVhbSB2aXNpdHVyIG51c3F1YW0gZXQgbWVkZWxhcnVtIGxvY2lzIHNvcnRlLgpMb2NpcyBz
dWFwdGUgZm9ybWF2aXQgc2VkIHF1b3F1ZSBwcm92aW5jaWFlIGxvY2lzIEhpZXJvc29seW1pcyB1c3VzIHByb3ZpbmNpYWUgZGVsYXRhIGZsdW1lbiB2aXNpdHVyIGV0IH
NlZC
--decoded
Locis in iuris navigerum provinciae emergunt quoque Iudaeis sorte Iudaeis ad suapte has multiplicium flumen visitur navigerum dom
itis his nusquam visitur nusquam et medelarum locis sorte.
Locis suapte formavit sed quoque provinciae locis Hierosolymis usus provinciae delata flumen visitur et sed aptae medelarum verum regiones usus speciem pari suapte.