环境: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. c#重写 重载

    重写:当一个子类继承一父类,而子类中的方法与父类中的方法的名称,参数个数.类型都完全一致时,就称子类中的这个方法重写了父类中的方法. 重写:通常,派生类继承基类的方法.因此,在调用对象继承方法的时候, ...

  2. bootstrap的tab中,echarts 图表宽度设为100%之后,会出现图表宽带变为100px的情况。只有第一个正常

    1.原因 echarts官方解释是 Tip: 有时候图表会放在多个标签页里,那些初始隐藏的标签在初始化图表的时候因为获取不到容器的实际高宽,可能会绘制失败,因此在切换到该标签页时需要手动调用resiz ...

  3. visualstudio Team Foundation Server 使用教程

    一.前沿 Team Foundation Server 是我们开发者使用最多的源代码管理工具.由于自己服务器搭建拉取工作慢的缘故,我使用了微软的 TFS.使用非常方便.快捷.免费.且不公开私有的项目. ...

  4. [WIP]C语言 realloc的坑

    创建: 2019/01/07 题外话,不知不觉又一年过去了,2019也要好好努力. 回到主题,在用动态循环数组实现queue的时候, 由于realloc的原因出现了一些莫名其妙的错误. 先开个题,晚点 ...

  5. IT兄弟连 JavaWeb教程 EL表达式中的运算

    EL语言支持算符运算符.关系运算符和逻辑运算符等,以完成常见的数据处理操作.所有的运算符说明见表7.2. 表2  El表达式中的运算符

  6. sublime text 插件的删除方法

    1.ctr+shift+P,输入Package Control: Remove Package 2.回车, 3.出现一个弹出框,输入你要删除的package 4.回车,OK!!!

  7. 管理现有数据库-web系统

    1 需求 现有的业务数据需要经常被展示,所以选择django作为展示工具.只需要使用django自带的admin app,然后对现有数据库进行建模就可以搞定. 2 代码 settings: DATAB ...

  8. Python开发 第01课 Python 简介

    一.Python 介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为AB ...

  9. centOS+uwsgi+nginx 部署flask项目,问题记录

    用flask做的项目想要部署到centOS系统上,填了一些坑,终于成功了,记录一下遇到的问题: 此次部署主要是按照这个博客进行的 https://www.cnblogs.com/Ray-liang/p ...

  10. 神奇的VIM

    1. di'.di".di`.di( .di{ .dt 'abc' ==> '' di' "abc"==> "" di" `ab ...