{"id":3569,"date":"2017-08-26T09:50:52","date_gmt":"2017-08-26T01:50:52","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3569"},"modified":"2017-08-26T09:50:52","modified_gmt":"2017-08-26T01:50:52","slug":"linux%e4%b8%8bc%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b0%e7%9a%84base64%e5%8a%a0%e8%a7%a3%e5%af%86","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3569.html","title":{"rendered":"Linux\u4e0bC\u8bed\u8a00\u5b9e\u73b0\u7684base64\u52a0\u89e3\u5bc6"},"content":{"rendered":"<p>=Start=<\/p>\n<h4>\u7f18\u7531\uff1a<\/h4>\n<p>\u5b66\u4e60\u3001\u63d0\u9ad8\u9700\u8981<\/p>\n<h4>\u6b63\u6587\uff1a<\/h4>\n<h5>\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<pre class=\"lang:default decode:true\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;unistd.h&gt;\r\n#include &lt;errno.h&gt;\r\n\r\n#include &lt;openssl\/ssl.h&gt;\r\n#include &lt;openssl\/bio.h&gt;\r\n#include &lt;openssl\/err.h&gt;\r\n\r\n\r\n\/\/base64_encode\r\nint base64_encode(const char *input, int input_size, int with_new_line, char *buff, int buff_size)\r\n{\r\n\tif (input == NULL || input_size &lt;= 0 || buff == NULL || buff_size &lt;= 0) {\r\n\t\treturn -1;\r\n\t}\r\n\r\n\tBIO *bmem = NULL;\r\n\tBUF_MEM *bptr = NULL;\r\n\tBIO *b64 = NULL;\r\n\tb64 = BIO_new(BIO_f_base64());  \r\n\tif(!with_new_line) {  \r\n\t\tBIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);  \r\n\t}  \r\n\tbmem = BIO_new(BIO_s_mem());  \r\n\tb64 = BIO_push(b64, bmem);  \r\n\tBIO_write(b64, input, input_size);  \r\n\tBIO_flush(b64);  \r\n\tBIO_get_mem_ptr(b64, &amp;bptr);  \r\n\r\n\tif (buff_size &lt; (bptr-&gt;length + 1)) {\r\n\t\tfprintf(stderr, \"base64_encode output buff too small, need %ld, now %d\\n\", bptr-&gt;length + 1, buff_size);\r\n\t\tBIO_free_all(b64);\r\n\t\treturn -1;\r\n\t}\r\n\t\/\/ char *buff = (char *)malloc(bptr-&gt;length + 1);  \r\n\tmemcpy(buff, bptr-&gt;data, bptr-&gt;length);  \r\n\tbuff[bptr-&gt;length] = 0;  \r\n\r\n\tBIO_free_all(b64);  \r\n\r\n\treturn 0;  \r\n}  \r\n\r\n\/\/base64_decode\r\nint base64_decode(char *input, int input_size, int with_new_line, char *buff, int buff_size)\r\n{\r\n\tif (input == NULL || input_size &lt;= 0 || buff == NULL || buff_size &lt;= 0) {\r\n\t\treturn -1;\r\n\t}\r\n\tmemset(buff, 0, buff_size);  \r\n\r\n\tBIO *bmem = NULL;\r\n\tBIO *b64 = NULL;\r\n\tb64 = BIO_new(BIO_f_base64());  \r\n\tif(!with_new_line) {  \r\n\t\tBIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);  \r\n\t}  \r\n\tbmem = BIO_new_mem_buf(input, input_size);  \r\n\tbmem = BIO_push(b64, bmem);  \r\n\tBIO_read(bmem, buff, input_size);  \r\n\r\n\tBIO_free_all(bmem);  \r\n\tCRYPTO_cleanup_all_ex_data();\t\/\/free crypto_malloc\r\n\r\n\treturn 0;  \r\n}\r\n\r\n\/\/ int base64_encode(const char *input, int input_size, int with_new_line, char *buff, int buff_size);\r\n\/\/ int base64_decode(char *input, int input_size, int with_new_line, char *buff, int buff_size);\r\n\/\/ $ gcc openssl_base64.c -lcrypto\r\nint main(int argc, char const *argv[])\r\n{\r\n\tif (argc &lt; 2) {\r\n\t\tprintf(\"Usage:\\n\\t%s str_to_be_base64_encode\\n\\n\", argv[0]);\r\n\t\treturn 0;\r\n\t}\r\n\r\n\tchar buff[1024] = \"\\0\";\r\n\tint ret = -1;\r\n\tret = base64_encode(argv[1], strlen(argv[1]), 0, buff, sizeof(buff));\r\n\tif (ret == 0) {\r\n\t\tprintf(\"input = %s\\n\", argv[1]);\r\n\t\tprintf(\"base64_encode = %s\\n\", buff);\r\n\t}\r\n\r\n\tchar buff_decode[1024] = \"\\0\";\r\n\tret = base64_decode(buff, strlen(buff), 0, buff_decode, sizeof(buff_decode));\r\n\tif (ret == 0) {\r\n\t\tprintf(\"input = %s\\n\", buff);\r\n\t\tprintf(\"buff_decode = %s\\n\", buff_decode);\r\n\t}\r\n\r\n    return 0;\r\n}<\/pre>\n<h5>\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a class=\"external-link\" href=\"http:\/\/www.cnblogs.com\/hancq\/p\/5535869.html\" rel=\"nofollow\">http:\/\/www.cnblogs.com\/hancq\/p\/5535869.html<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/shanetully.com\/2013\/08\/working-with-binary-data-in-c-and-openssl\/\" rel=\"nofollow\">https:\/\/shanetully.com\/2013\/08\/working-with-binary-data-in-c-and-openssl\/<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/gist.github.com\/barrysteyn\/7308212\" rel=\"nofollow\">https:\/\/gist.github.com\/barrysteyn\/7308212<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/gist.github.com\/barrysteyn\/4409525\" rel=\"nofollow\">https:\/\/gist.github.com\/barrysteyn\/4409525<\/a><\/li>\n<li><a class=\"external-link\" href=\"http:\/\/doctrina.org\/Base64-With-OpenSSL-C-API.html\" rel=\"nofollow\">http:\/\/doctrina.org\/Base64-With-OpenSSL-C-API.html<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/journal.missiondata.com\/how-to-base64-encode-with-c-c-and-openssl-acccb1045c42\" rel=\"nofollow\">https:\/\/journal.missiondata.com\/how-to-base64-encode-with-c-c-and-openssl-acccb1045c42<\/a><\/li>\n<li><a class=\"external-link\" href=\"https:\/\/devenix.wordpress.com\/2008\/01\/18\/howto-base64-encode-and-decode-with-c-and-openssl-2\/\" rel=\"nofollow\">https:\/\/devenix.wordpress.com\/2008\/01\/18\/howto-base64-encode-and-decode-with-c-and-openssl-2\/<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5b66\u4e60\u3001\u63d0\u9ad8\u9700\u8981 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a #include &lt;stdio.h&gt;  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7,12],"tags":[369,100,30,277],"class_list":["post-3569","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","category-tools","tag-base64","tag-c","tag-linux","tag-openssl"],"views":8180,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3569"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3569\/revisions"}],"predecessor-version":[{"id":3570,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3569\/revisions\/3570"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}