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 ...
随机推荐
- 查看python 3中的内置函数列表,以及函数功能描述
>>> dir(__builtins__)//查看内置函数(BIF)列表 ['ArithmeticError', 'AssertionError', 'AttributeError' ...
- 原创|高逼格企业级MySQL数据库备份方案,原来是这样....
很多人,这里说的是运维工程师们,一提到写某某方案,很是头疼.不是上某度一统搜索,就是同样一句话在N个群全部群发一遍:“有没有某某方案,可以共享一下的吗??求助,各位大佬们”,估计十有八九,全部石沉大海 ...
- Lightoj 1082【RMQ】
这里很low地写了个线段树... #include <bits/stdc++.h> using namespace std; typedef long long LL; const int ...
- 2017-7-22 NOIP模拟赛
Digits (digits.cpp/c/pas) Description给一个关于 的多项式,并给定一个 ,求该多项式在带入该 时的值最后 位数字.Input第一行两个整数 . :之后的 行,每行两 ...
- 怎么解决UIScrollView把uitableviewcell的点击事件屏蔽了
[self.contentView addSubview:self.scrollView]; self.scrollView.userInteractionEnabled = NO; [self.co ...
- RDS 导出Mysqlbinlog_二进制日志
1:首先进入RDS后台查看Mysqlbin_log日志,可以通过以下命令进行查看: show master logs; 或 show binary logs; 2:比如我们获取的是:mysql-bin ...
- 使用top观察一进程的cpu历史占用情况
#!/bin/shtop -b -n 1 -p 1975| tail -3 >>process1975.log 搞了时间节点,做个定时任务什么的就ok了
- C# 获取当前ip
1.获取局域网ip IPAddress ipAddr = Dns.Resolve(Dns.GetHostName()).AddressList[0];//获得当前IP地址 string ip=ipAd ...
- 最短路之SPFA(单源)HDU 2544
#include <iostream> #include <queue> #include <algorithm> #define MAXLEN 1005 #def ...
- monxin cms 任意文件删除漏洞
\program\diypage\receive\edit.php首先看到一个unlink($path);本来应该先看sql语句的,但知道是任意文件删除先跳过删除语句,看看$path怎么传入的倒推上去 ...