环境:vc2003

.h

 /**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more details. You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**********/
// "liveMedia"
// Copyright (c) 1996-2010 Live Networks, Inc. All rights reserved.
// Base64 encoding and decoding
// C++ header #ifndef _BASE64_HH
#define _BASE64_HH //#ifndef _BOOLEAN_HH
//#include "Boolean.hh"
//#endif #ifdef __cplusplus
extern "C" {
#endif unsigned char* base64Decode(char* in, unsigned int& resultSize, bool trimTrailingZeros = true);
// returns a newly allocated array - of size "resultSize" - that
// the caller is responsible for delete[]ing. char* base64Encode(char const* orig, unsigned origLength);
// returns a 0-terminated string that
// the caller is responsible for delete[]ing. #ifdef __cplusplus
}
#endif #endif

.cpp

 #include <string.h>
#include "Base64New.h"
static char base64DecodeTable[]; ////////////////////////////////////////
char* strDup(char const* str)
{
if (str == NULL) return NULL;
size_t len = strlen(str) + ;
char* copy = new char[len]; if (copy != NULL)
{
memcpy(copy, str, len);
}
return copy;
} char* strDupSize(char const* str)
{
if (str == NULL) return NULL;
size_t len = strlen(str) + ;
char* copy = new char[len]; return copy;
} static void initBase64DecodeTable()
{
int i;
for (i = ; i < ; ++i) base64DecodeTable[i] = (char)0x80;
// default value: invalid for (i = 'A'; i <= 'Z'; ++i) base64DecodeTable[i] = + (i - 'A');
for (i = 'a'; i <= 'z'; ++i) base64DecodeTable[i] = + (i - 'a');
for (i = ''; i <= ''; ++i) base64DecodeTable[i] = + (i - '');
base64DecodeTable[(unsigned char)'+'] = ;
base64DecodeTable[(unsigned char)'/'] = ;
base64DecodeTable[(unsigned char)'='] = ;
} unsigned char* base64Decode(char* in, unsigned int& resultSize, bool trimTrailingZeros)
{
static bool haveInitedBase64DecodeTable = false;
if (!haveInitedBase64DecodeTable)
{
initBase64DecodeTable();
haveInitedBase64DecodeTable = true;
} unsigned char* out = (unsigned char*)strDupSize(in); // ensures we have enough space
int k = ;
int const jMax = strlen(in) - ;
// in case "in" is not a multiple of 4 bytes (although it should be)
for (int j = ; j < jMax; j += )
{
char inTmp[], outTmp[];
for (int i = ; i < ; ++i)
{
inTmp[i] = in[i+j];
outTmp[i] = base64DecodeTable[(unsigned char)inTmp[i]];
if ((outTmp[i]&0x80) != ) outTmp[i] = ; // pretend the input was 'A'
} out[k++] = (outTmp[]<<) | (outTmp[]>>);
out[k++] = (outTmp[]<<) | (outTmp[]>>);
out[k++] = (outTmp[]<<) | outTmp[];
} if (trimTrailingZeros)
{
while (k > && out[k-] == '/0') --k;
}
resultSize = k;
unsigned char* result = new unsigned char[resultSize];
memmove(result, out, resultSize);
delete[] out; return result;
} static const char base64Char[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; char* base64Encode(char const* origSigned, unsigned origLength)
{
unsigned char const* orig = (unsigned char const*)origSigned; // in case any input bytes have the MSB set //参数1转换成无符号的
if (orig == NULL) return NULL; unsigned const numOrig24BitValues = origLength/; //numOrig24BitValues保存整除3后的值
bool havePadding = origLength > numOrig24BitValues*;//判断源字符串长度,大于整除后,再乘3的长度,有大于或等于两种可能。
bool havePadding2 = origLength == numOrig24BitValues* + ; //整除3后,再乘3,再加2,与源字符串长度比较是否相等。
unsigned const numResultBytes = *(numOrig24BitValues + havePadding); //计算长度,判断整除3后,加0或1,乘4,赋给numResultBytes;
char* result = new char[numResultBytes+]; // allow for trailing '/0' memset(result,,numResultBytes+); //add by wjz20130813 // Map each full group of 3 input bytes into 4 output base-64 characters:
unsigned i;
for (i = ; i < numOrig24BitValues; ++i)
{
result[*i+] = base64Char[(orig[*i]>>)&0x3F];
result[*i+] = base64Char[(((orig[*i]&0x3)<<) | (orig[*i+]>>))&0x3F];
result[*i+] = base64Char[((orig[*i+]<<) | (orig[*i+]>>))&0x3F];
result[*i+] = base64Char[orig[*i+]&0x3F];
} // Now, take padding into account. (Note: i == numOrig24BitValues)
if (havePadding)
{
result[*i+] = base64Char[(orig[*i]>>)&0x3F];
if (havePadding2)
{
result[*i+] = base64Char[(((orig[*i]&0x3)<<) | (orig[*i+]>>))&0x3F];
result[*i+] = base64Char[(orig[*i+]<<)&0x3F];
}
else
{
result[*i+] = base64Char[((orig[*i]&0x3)<<)&0x3F];
result[*i+] = '=';
}
result[*i+] = '=';
} result[numResultBytes+] = '/0'; //alter by wjz 20130813 return result;
}

Encryption-基础:base64加解密的更多相关文章

  1. Pyton基础-base64加解密

    base64加密后是可逆的,所以url中传输参数一般用base64加密 import base64 s='username=lanxia&username2=zdd' new_s=base64 ...

  2. java base64加解密

    接上篇java Base64算法. 根据之前过程使用base64加解密,所以写成了工具类. 代码示例; public class Base64Util { private static Logger ...

  3. QuickBase64 - Android 下拉通知栏快捷base64加解密工具

    Android Quick Setting Tile Base64 Encode/Decode Tool Android 下拉通知栏快捷 base64 加解密,自动将剪切板的内容进行 base64 E ...

  4. java基础/数据加解密(Mooc)

    一.消息摘要算法 常用摘要算法: 以下 (HEX)内容:bc指Bouncy Castle  |  cc指:Apache commons Codec 1.消息摘要算法MD5及MD族(MD2,MD4) 消 ...

  5. JAVA加解密 -- Base64加解密

    Base64算法实现:可以将任意的字节数组数据,通过算法,生成只有(大小写英文.数字.+./)(一共64个字符)内容表示的字符串数据. private static final String str ...

  6. base64加解密字符串

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  7. oracle里面base64加解密

    1. base64 的解密函数select utl_raw.cast_to_varchar2(utl_encode.base64_decode(utl_raw.cast_to_raw('dGVzdA= ...

  8. java之BASE64加解密

    1.简介 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到. 注:位于jdk的java.util包中. 2. ...

  9. Python AES - base64 加解密

    首先python引用AES加密 from Crypto.Cipher import AES 需要先安装  Crypto  模块, 可以使用 easy_install 进行安装   会自动去官网进行搜索 ...

  10. 使用Apache的Base64类实现Base64加解密

    包名称:org.apache.commons.codec.binary 类名称:org.apache.commons.codec.binary.Base64 1.Base64加密 public sta ...

随机推荐

  1. 多行文字的垂直居中或高度不同的图片垂直居中---:after伪类+content

    如何让多行文字垂直居中?或者如何让图片垂直居中? 1.demo--css .box { width: 300px; height: 300px; background-color: #f5e79e; ...

  2. perl C/C++ 扩展(四)

    在前面三篇博客中,我们了解到如何使用c/c++ 扩展自己的perl 库,但是博主在学习过程中,对动态库或静态库的加载不是十分了解,后来自己又细挖一下.后来就有了这篇博文,再后来,没有再后来了,囧!! ...

  3. 机智云连接ESP8266--远程控制点亮RGB灯

    概述 智能灯,是一个简单常见的智能产品,硬件电路简单,程序本身也不复杂:下面我们使用esp8266开发板和机智云云端,实现如何将一个传统的灯泡,改造成可以远程控制开关的智能灯. 1.准备工作 硬件: ...

  4. 01 | Jewels and Stones

    Question You're given strings J representing the types of stones that are jewels, and S representing ...

  5. random 库

    random 是使用随机数的python 标准库 ——为随机数:采用梅森旋转算法生成的(伪)随机序列中的元素 —— import random 基本随机数函数:seed(),random() 扩展随机 ...

  6. Jquery | 基础 | html()

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. [題解/狀壓dp]POJ_2411_Mondriaan's dream

    关于“我读过很多书,到后来大部分都被我忘记了,那阅读的意义是什么?”的疑问,我看过最巧妙的一个回答:当我还是个孩子的时候,我吃过很多的食物,大部分已经一去不复返而且被我忘记了,但可以肯定的是,它们中的 ...

  8. AKOJ-1265-输出二叉树

    链接:https://oj.ahstu.cc/JudgeOnline/problem.php?id=1265 题意: 我们知道二叉树的先序序列和中序序列或者是中序和后序能够唯一确定一颗二叉树.现在给一 ...

  9. 洛谷1941(dp)

    常规的dp,当前有值且碰不到管子就转移,可以连跳的操作我就加了一维表示当前是不是连跳过来的.第二问前缀和即可得(不对啊边走边记录就行了吧我冗了Orz). #include <cstdio> ...

  10. 洛谷P2599||bzoj1413 [ZJOI2009]取石子游戏

    bzoj1413 洛谷P2599 根本不会啊... 看题解吧 #include<cstdio> #include<algorithm> #include<cstring& ...