dream02008 2013-09-25 14:15
浏览 190
已采纳

为什么openssl无法解码php的base64_encode函数编码的字符串?

I want to base64 encode a string, and decode it in C language with OpenSSL api. I encoded in php like that:

<?php 
echo base64_encode("aaa");
?>

result is "YWFh"

Then decode it in C like that:

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.

Can't decode the string encoded before.

I tried to use these command lines to test:

$echo "aaa" | openssl enc -base64 
output: YWFhCg==

$echo "YWFhCg==" | openssl enc -base64 -d
output: aaa

What's the difference between base64_encode in php and openssl?What causes this?

  • 写回答

1条回答 默认 最新

  • doushua7737 2013-09-25 18:28
    关注

    Add BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL) after the BIO_new() call to tell OpenSSL that all the input appears in a single line without newline.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?