整理下Base64相關的東西(OpenSSL/GO...)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Base64在不同語言對接時,其實是有些小坑的,之前有碰過。

首先Base64的是啥? 阮一峰有一篇寫得很贊的文章說得很明白。

然後是怎麼編碼?

我用OpenSSL寫了個Base64解編碼實現:

/* 用openssl來做Base64加解密. Author: xcl Date:2015-9-17*/#include <cstdio>#include <cstdlib>#include <iostream>#include "openssl/ssl.h"  #include <openssl/bio.h>#include <openssl/evp.h>#if defined(WIN32) || defined(_WIN64)#pragma comment(lib, "libeay32.lib")#pragma comment(lib, "ssleay32.lib")#endif/*NO_PADDING : 略去加密字串最後的”=”NO_WRAP : 略去所有的分行符號Android中的Base64.DEFAULT會在每超過76個字元後,自動加分行符號,並且在字串最後也會加一個分行符號.所以與其對接時(比如IOS)要注意這點.可建議其選Base64.NO_WRAP類型.而OpenSSL的命令列進行Base64時,則每超過64個字元就自動加分行符號,並且也在最末尾自動加換行.解編碼命令列如下:echo "Hello"|openssl enc -base64 echo "SGVsbG8K"|openssl enc -base64 -d*/enum Base64{ NO_PADDING = 0, NO_WRAP};std::string  Decode(std::string data,const int mode) { printf("[Decode] 解密前:%s\r\n", data.c_str());size_t length = data.length();if (length == 0)return "";BIO *b64 = BIO_new(BIO_f_base64());if (mode == Base64::NO_WRAP) {BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);} BIO *mem = nullptr;mem = BIO_new_mem_buf(const_cast<char *>(data.c_str()), -1);mem = BIO_push(b64, mem);//char inbuf[512];//int inlen;//while ((inlen = BIO_read(b64, inbuf, 512)) > 0) {//printf("解密後:%s \n\r\n", inbuf);//}char * buffer = (char *)malloc(length);memset(buffer, 0, length);BIO_read(mem, buffer, length);std::string ret(buffer);free(buffer);BIO_free_all(mem);printf("[Decode] 解密後:%s\r\n", ret.c_str());return ret;}std::string Encode(std::string data,const int mode) {printf("[Encode] 加密前:%s\r\n", data.c_str());if (data.length() == 0)return "";//寫入時,採用base64編碼 BIO *b64 = BIO_new(BIO_f_base64());// 如果開啟 BIO_FLAGS_BASE64_NO_NL ,則傳入時要帶"\n" 即"Hello\n",// 否則,還原後會丟失兩個字元.if (Base64::NO_WRAP == mode) {data.append("\n");BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);}// Create a memory BIOBIO *mem = BIO_new(BIO_s_mem());b64 = BIO_push(b64, mem);BIO_write(b64, data.c_str(),data.length());BIO_flush(b64);//BIO_puts(b64, data.c_str());//得到base64後的字串BUF_MEM *pBuf = nullptr;BIO_get_mem_ptr(mem, &pBuf);std::string ret(pBuf->data);free(pBuf);BIO_set_close(b64, BIO_NOCLOSE);BIO_free_all(b64);printf("[Encode] 加密後:%s\r\n", ret.c_str());return ret;}void main() {std::string s = "Hello";printf("//////////////////////////////\r\n");printf("NO_PADDING:\r\n");Base64 mode = Base64::NO_PADDING;s = Encode(s,mode);    Decode(s,mode);printf("//////////////////////////////\r\n");printf("NO_WRAP:\r\n");s = "Hello";mode = Base64::NO_WRAP;s = Encode(s, mode);Decode(s, mode);system("pause");}/*//////////////////////////////NO_PADDING:[Encode] 加密前:Hello[Encode] 加密後:SGVsbG8=[Decode] 解密前:SGVsbG8=[Decode] 解密後:Hello//////////////////////////////NO_WRAP:[Encode] 加密前:Hello[Encode] 加密後:SGVsbG8K[Decode] 解密前:SGVsbG8K[Decode] 解密後:Hello請按任意鍵繼續. . .*/

對於Golang而言,官網就提供了一個很好的例子,有標準的Base64和URL的編碼我在這貼下這個例子:

package mainimport b64 "encoding/base64"import "fmt"func main() {data := "abc123!?$*&()'-=@~"sEnc := b64.StdEncoding.EncodeToString([]byte(data))    fmt.Println(sEnc)sDec, _ := b64.StdEncoding.DecodeString(sEnc)    fmt.Println(string(sDec))    fmt.Println()   uEnc := b64.URLEncoding.EncodeToString([]byte(data))    fmt.Println(uEnc)    uDec, _ := b64.URLEncoding.DecodeString(uEnc)    fmt.Println(string(uDec))}$ go run base64-encoding.goYWJjMTIzIT8kKiYoKSctPUB+abc123!?$*&()'-=@~YWJjMTIzIT8kKiYoKSctPUB-abc123!?$*&()'-=@~

         如果對Go是怎麼實現的感興趣,這有源碼.

 嗯,大致就這些.


BLOG: http://blog.csdn.net/xcl168







著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.