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 ...
随机推荐
- HDU - 1715 - 大菲波数 - JAVA
http://acm.hdu.edu.cn/showproblem.php?pid=1715 import java.io.*; import java.util.*; import java.mat ...
- 玲珑学院1072 【DFS】
蛤蛤,略蠢. priority_queue 自定义优先级 和排序是反的 struct node { int x,y; friend bool operator< (node a,node b) ...
- 51nod1086(多重背包&二進制)
題目鏈接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1086 題意:中文題誒- 思路:很顯然這是一道多重背包題,不過這 ...
- 如何在Linux服务器上部署禅道
最近换了新的项目团队,由于新团队比较年轻化,没有实行正规的项目管理,于是我自告奋勇要为团队管理出一份力,帮助团队建立敏捷化的项目管理,经过多方考究和对比后,选择了目前较受欢迎的开源项目管理软件:禅道. ...
- bzoj3196:Tyvj1730二逼平衡树
传送门 暴力啊,直接树套树上啊 线段树套splay,卡卡常就直接A了 代码: #include<cstdio> #include<iostream> #include<a ...
- 关于idea中使用lamb表达式报错:ambda expressions are not supported at this language level
我使用的是jdk1.8,使用lamb表达式的时候,报错 ambda expressions are not supported at this language level, 后来,设置了 接着重启了 ...
- 集合框架Collection<E>接口
- jmeter diff测试
1.准备接口数据(对比字段,即json数据中需要提取的key对应的值进行对比) 2.配置获取EXCEL数据 3.新建线程,并建两个http请求,分别用于请求新旧接口 4.提取需要对比的内容 5.赋值变 ...
- CF #546div2D
题目本质:只有能做到一路过关斩将的勇者才能冒泡过来救出女主. 主要代码: ; int n, m, a[maxn], ans; vector<int> edge[maxn]; set< ...
- Codeforces Round #396 (Div. 2) A
While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common s ...