Base64解码,编码
2015-05-05 14:46
357 查看
// // // CrossAppx // // Created by kevin on 15-1-29. // Copyright (c) 2014 http://9miao.com All rights reserved. // #pragma once #include "CrossApp.h" #include <iostream> #include <stdio.h> #include <string> using namespace std; class MyBase64 { public: MyBase64(void); ~MyBase64(void); static inline bool is_Base64(unsigned char c) { return (isalnum(c) || (c == '+') || (c == '/')); } static std::string Base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len); static std::string Base64_decode(std::string const& encoded_string); };
// // // CrossAppx // // Created by kevin on 15-1-29. // Copyright (c) 2014 http://9miao.com All rights reserved. // #include "MyBase64.h" MyBase64::MyBase64(void) { } MyBase64::~MyBase64(void) { } std::string MyBase64::Base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { std::string MyBase64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; std::string ret; int i = 0; int j = 0; unsigned char char_array_3[3]; unsigned char char_array_4[4]; while (in_len--) { char_array_3[i++] = *(bytes_to_encode++); if (i == 3) { char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f; for (i = 0; (i < 4); i++) ret += MyBase64_chars[char_array_4[i]]; i = 0; } } if (i) { for (j = i; j < 3; j++) char_array_3[j] = '\0'; char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[3] = char_array_3[2] & 0x3f; for (j = 0; (j < i + 1); j++) ret += MyBase64_chars[char_array_4[j]]; while ((i++ < 3)) ret += '='; } return ret; } std::string MyBase64::Base64_decode(std::string const& encoded_string) { std::string MyBase64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int in_len = encoded_string.size(); int i = 0; int j = 0; int in_ = 0; unsigned char char_array_4[4], char_array_3[3]; std::string ret; while (in_len-- && (encoded_string[in_] != '=') && is_Base64(encoded_string[in_])) { char_array_4[i++] = encoded_string[in_]; in_++; if (i == 4) { for (i = 0; i < 4; i++) char_array_4[i] = MyBase64_chars.find(char_array_4[i]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (i = 0; (i < 3); i++) ret += char_array_3[i]; i = 0; } } if (i) { for (j = i; j < 4; j++) char_array_4[j] = 0; for (j = 0; j < 4; j++) char_array_4[j] = MyBase64_chars.find(char_array_4[j]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; } return ret; }
相关文章推荐
- C#中Base64之编码,解码方法
- JAVA开发_Base64编码与解码
- base64编码、解码
- Java如何进行Base64的编码(Encode)与解码(Decode)?
- VC中BASE64编码和解码使用详解
- 编码格式转换--base64 格式的解码和编码
- C#实现Base64编码与解码
- 用C++ 4000 实现Base64编码解码
- 使用 apache 对 字符串进行base64编码解码
- Base64编码、解码的基本方法,及byteArray = base64.toByteArray();抛出A partial block (2 of 4 bytes) was dropped. Dec
- 利用openssl进行BASE64编码解码、md5/sha1摘要、AES/DES3加密解密
- JavaScript实现的Base64编码和解码
- 利用OpenSSL进行BASE64的编码与解码
- 利用 XML 实现 BASE64 编码和解码
- BASE64编码和解码(VC源代码) 并 内存加载 CImage 图像
- Base64编码与解码 分类: 中文信息处理 2014-11-03 21:58 505人阅读 评论(0) 收藏
- C# 实现Base64的编码解码
- base64 encoder/decoder for objective-c编码及解码(转)
- 利用base64对图片进行编码及解码
- [C语言]Base64编码解码