Encryption-基础:base64加解密
环境: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加解密的更多相关文章
- Pyton基础-base64加解密
base64加密后是可逆的,所以url中传输参数一般用base64加密 import base64 s='username=lanxia&username2=zdd' new_s=base64 ...
- java base64加解密
接上篇java Base64算法. 根据之前过程使用base64加解密,所以写成了工具类. 代码示例; public class Base64Util { private static Logger ...
- QuickBase64 - Android 下拉通知栏快捷base64加解密工具
Android Quick Setting Tile Base64 Encode/Decode Tool Android 下拉通知栏快捷 base64 加解密,自动将剪切板的内容进行 base64 E ...
- java基础/数据加解密(Mooc)
一.消息摘要算法 常用摘要算法: 以下 (HEX)内容:bc指Bouncy Castle | cc指:Apache commons Codec 1.消息摘要算法MD5及MD族(MD2,MD4) 消 ...
- JAVA加解密 -- Base64加解密
Base64算法实现:可以将任意的字节数组数据,通过算法,生成只有(大小写英文.数字.+./)(一共64个字符)内容表示的字符串数据. private static final String str ...
- base64加解密字符串
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- oracle里面base64加解密
1. base64 的解密函数select utl_raw.cast_to_varchar2(utl_encode.base64_decode(utl_raw.cast_to_raw('dGVzdA= ...
- java之BASE64加解密
1.简介 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到. 注:位于jdk的java.util包中. 2. ...
- Python AES - base64 加解密
首先python引用AES加密 from Crypto.Cipher import AES 需要先安装 Crypto 模块, 可以使用 easy_install 进行安装 会自动去官网进行搜索 ...
- 使用Apache的Base64类实现Base64加解密
包名称:org.apache.commons.codec.binary 类名称:org.apache.commons.codec.binary.Base64 1.Base64加密 public sta ...
随机推荐
- 《剑指offer》面试题5—从尾到头打印链表
重要思路: 这个问题肯定要遍历链表,遍历链表的顺序是从头到尾,而要输出的顺序却是从尾到头,典型的“后进先出”,可以用栈实现. 注意stl栈的使用,遍历stack的方法. #include <io ...
- std::thread 在DLLMain 中会发生死锁 std::thread cause deadlock in DLLMain
注意不要再DLLMain中使用 std::thread 否则会发生死锁. 但是可以使用 _beginthreadex (此函数可以使用lambda) 或者直接使用windows的底层函数: Creat ...
- 初学Django框架知识
首先了解什么事HTTP协议; 1.浏览器往服务器发的 请求(request): 请求消息的格式: 请求方法 路径 HTTP/1.1\r\n k1:v1\r\n k2:v2\r\n \r\n 请求数据 ...
- PHP保留小数的相关方法
结合一下网上的例子 $num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 但是这个如果没有两位小数也不会"两位精度" echo round($num, ...
- Java的12个语法糖【转】
本文转载自公众号 Hollis 原创: 会反编译的 Hollis 侵权删 本文从 Java 编译原理角度,深入字节码及 class 文件,抽丝剥茧,了解 Java 中的语法糖原理及用法,帮助大家在学 ...
- jQuery取得/设置select的值
本来以为jQuery("#select1").val();是取得选中的值, 那么jQuery("#select1").text();就是取得的文本. 这是不正确 ...
- onpageshow 监听页面是否是缓存页面
需求:点击A页面跳转至B页面,在B页面点击手机物理回退键或者history.back回退时,需要在A页面判断当前页面是否是回退回来的页面,而不是新加载的.这里用到一个 onpageshow 事件. 定 ...
- 操作messageBox类
我们经常操作messagebox类,有时候我们又分不清一些参数,下面是一些操作messageBox的常用方法: public static class ClsMsg { public static v ...
- Autofac框架使用遇到的问题
1) 安全透明方法“Autofac.Integration.Mvc.RegistrationExtensions.RegisterControllers(Autofac.ContainerBuilde ...
- 洛谷 P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers
贪婪的送礼者Greedy Gift Givers 难度:☆ Code: #include <iostream> #include <cstdio> #include <c ...