我想base64编码一个字符串,并使用OpenSSL
api用C语言对其进行解码。
我按照php
编码:
<?php
echo base64_encode("aaa");
?>
result is "YWFh"
然后在C
中解码它:
int base64_decode(char *str,int str_len,char *decode,int decode_buffer_len){
int len=0;
BIO *b64,*bmem;
b64=BIO_new(BIO_f_base64());
bmem=BIO_new_mem_buf(str,str_len);
bmem=BIO_push(b64,bmem);
len=BIO_read(bmem,decode,str_len);
decode[len]=0;
BIO_free_all(bmem);
return 0;
}
result is NULL, nothing decoded.
无法解码之前编码的字符串。
我尝试使用这些命令行来测试:
$echo "aaa" | openssl enc -base64
output: YWFhCg==
$echo "YWFhCg==" | openssl enc -base64 -d
output: aaa
php
和openssl
中base64_encode之间的区别是什么?导致这种情况的原因是什么?
答案 0 :(得分:1)
在BIO_new()调用之后添加BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL)
告诉OpenSSL所有输入都出现在没有换行符的单行中。