标签:des style blog http io ar color os 使用
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的详细规范。Base64编码可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码不仅比较简短,同时也具有不可读性,即所编码的数据不会被人用肉眼所直接看到。
RFC2045还规定每行位76个字符,每行末尾需添加一个回车换行符,即便是最后一行不够76个字符,也要加换行符。
实现原理
Base64实际上是对二进制码做分组转换操作
1.每3个8位二进制码位一组,转换为4个6位二进制码为一组(不足6位时地位补0)。3个8位二进制码和4个6位二进制码长度都是24位。
2.对获得的4个6位二进制码补位,每个6位二进制码添加两位高位0,组成4个8位二进制码。
3.将获得的4个8位二进制码转换为4个十进制码。
4.将获得的十进制码转换为Base64字符表中对应的字符。
1. Base64使用A--Z,a--z,0--9,+,/ 这64个字符.
2. 编码原理:将3个字节转换成4个字节( (3 X 8) = 24 = (4 X 6) )先读入3个字节,每读一个字节,左移8位,再右移四次,每次6位,这样就有4个字节了.
3. 解码原理:将4个字节转换成3个字节.先读入4个6位(用或运算),每次左移6位,再右移3次,每次8位.这样就还原了.
Base64是一种很常见的编码规范,其作用是将二进制序列转换为人类可读的ASCII字符序列,常用在需用通过文本协议(比如HTTP和SMTP)来传输二进制数据的情况下。Base64并不是一种用于安全领域的加密解密算法(这类算法有DES等),尽管我们有时也听到使用Base64来加密解密的说法,但这里所说的加密与解密实际是指编码(encode)和解码(decode)的过程,其变换是非常简单的,仅仅能够避免信息被直接识别。
Base64采用了一种很简单的编码转换:对于待编码数据,以3个字节为单位,依次取6位数据并在前面补上两个0形成新的8位编码,由于3*8=4*6,这样3个字节的输入会变成4个字节的输出,长度上增加了1/3。
上面的处理还不能保证得到的字符都是可见字符,为了达到此目的,Base64制定了一个编码表,进行统一的转换。码表的大小为2^6=64,这也是Base64名称的由来。
Base64编码表
Value Encoding Value Encoding Value Encoding Value Encoding
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v
14 O 31 f 48 w (pad) =
15 P 32 g 49 x
16 Q 33 h 50 y
Base64编解码算法都很简单,网上有很多源码,这里就不介绍了。
另外还有一点要注意的地方,前面提到编码是以3个字节为单位,当剩下的字符数量不足3个字节时,则应使用0进行填充,相应的,输出字符则使用‘=‘占位,因此编码后输出的文本末尾可能会出现1至2个‘=‘。
这是一种典型的编码转换的处理方法,类似的可能还有UTF16与UTF8之间的转换。
[cpp] view plaincopy
/**
* /file base64.h
*/
#ifndef XYSSL_BASE64_H
#define XYSSL_BASE64_H
#define XYSSL_ERR_BASE64_BUFFER_TOO_SMALL -0x0010
#define XYSSL_ERR_BASE64_INVALID_CHARACTER -0x0012
#ifdef __cplusplus
extern "C" {
#endif
/**
* /brief Encode a buffer into base64 format
*
* /param dst destination buffer
* /param dlen size of the buffer
* /param src source buffer
* /param slen amount of data to be encoded
*
* /return 0 if successful, or XYSSL_ERR_BASE64_BUFFER_TOO_SMALL.
* *dlen is always updated to reflect the amount
* of data that has (or would have) been written.
*
* /note Call this function with *dlen = 0 to obtain the
* required buffer size in *dlen
*/
int base64_encode(const unsigned char *src, int slen,unsigned char *dst, int *dlen);
/**
* /brief Decode a base64-formatted buffer
*
* /param dst destination buffer
* /param dlen size of the buffer
* /param src source buffer
* /param slen amount of data to be decoded
*
* /return 0 if successful, XYSSL_ERR_BASE64_BUFFER_TOO_SMALL, or
* XYSSL_ERR_BASE64_INVALID_DATA if the input data is not
* correct. *dlen is always updated to reflect the amount
* of data that has (or would have) been written.
*
* /note Call this function with *dlen = 0 to obtain the
* required buffer size in *dlen
*/
int base64_decode(const unsigned char *src, int slen,unsigned char *dst, int *dlen);
/**
* /brief Checkup routine
*
* /return 0 if successful, or 1 if the test failed
*/
int base64_self_test( int verbose );
#ifdef __cplusplus
}
#endif
class CBase64
{
public:
CBase64();
virtual ~CBase64();
static BOOL Encrypt(const unsigned char *pSrc,int iSlen,unsigned char *pDst,int *iDlen,CString &strErrorInfo);
static BOOL Decrypt(const unsigned char *pSrc,int iSlen,unsigned char *pDst,int *iDlen,CString &strErrorInfo);
private:
};
#endif /* base64.h */
[cpp] view plaincopy
//source file: base64.cpp
/*
* RFC 1521 base64 encoding/decoding
*
* Copyright (C) 2006-2007 Christophe Devine
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License, version 2.1 as published by the Free Software Foundation.
*
* 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
*/
#include "stdafx.h"
#include "base64.h"
static const unsigned char base64_enc_map[64] =
{
‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘,
‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘,
‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘,
‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘,
‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘,
‘y‘, ‘z‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘,
‘8‘, ‘9‘, ‘+‘, ‘/‘
};
static const unsigned char base64_dec_map[128] =
{
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 127, 127, 127, 127, 127
};
/*
* Encode a buffer into base64 format
*/
int base64_encode(const unsigned char *src, int slen,unsigned char *dst, int *dlen)
{
int i, n;
int C1, C2, C3;
unsigned char *p;
if( slen == 0 )
return( 0 );
n = (slen << 3) / 6;
switch( (slen << 3) - (n * 6) )
{
case 2: n += 3; break;
case 4: n += 2; break;
default: break;
}
if( *dlen < n + 1 )
{
*dlen = n + 1;
return( XYSSL_ERR_BASE64_BUFFER_TOO_SMALL );
}
n = (slen / 3) * 3;
for( i = 0, p = dst; i < n; i += 3 )
{
C1 = *src++;
C2 = *src++;
C3 = *src++;
*p++ = base64_enc_map[(C1 >> 2) & 0x3F];
*p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
*p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
*p++ = base64_enc_map[C3 & 0x3F];
}
if( i < slen )
{
C1 = *src++;
C2 = ((i + 1) < slen) ? *src++ : 0;
*p++ = base64_enc_map[(C1 >> 2) & 0x3F];
*p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
if( (i + 1) < slen )
*p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
else *p++ = ‘=‘;
*p++ = ‘=‘;
}
*dlen =static_cast<int>(p - dst);
*p = 0;
return( 0 );
}
/*
* Decode a base64-formatted buffer
*/
int base64_decode(const unsigned char *src, int slen,unsigned char *dst, int *dlen)
{
int i, j, n;
unsigned long x;
unsigned char *p;
for( i = j = n = 0; i < slen; i++ )
{
if( ( slen - i ) >= 2 &&
*(src+i) == ‘/r‘ && *(src+i+1) == ‘/n‘ )
continue;
if(*(src+i) == ‘/n‘ )
continue;
if(*(src+i) == ‘=‘ && ++j > 2 )
return( XYSSL_ERR_BASE64_INVALID_CHARACTER );
if(*(src+i) > 127 || base64_dec_map[*(src+i)] == 127 )
return( XYSSL_ERR_BASE64_INVALID_CHARACTER );
if( base64_dec_map[*(src+i)] < 64 && j != 0 )
return( XYSSL_ERR_BASE64_INVALID_CHARACTER );
n++;
}
if( n == 0 )
return( 0 );
n = ((n * 6) + 7) >> 3;
if( *dlen < n )
{
*dlen = n;
return( XYSSL_ERR_BASE64_BUFFER_TOO_SMALL );
}
for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
{
if( *src == ‘/r‘ || *src == ‘/n‘ )
continue;
j -= ( base64_dec_map[*src] == 64 );
x = (x << 6) | ( base64_dec_map[*src] & 0x3F );
if( ++n == 4 )
{
n = 0;
if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
if( j > 1 ) *p++ = (unsigned char)( x >> 8 );
if( j > 2 ) *p++ = (unsigned char)( x );
}
}
*dlen =static_cast<int>(p - dst);
return( 0 );
}
BOOL CBase64::Encrypt(const unsigned char *pSrc,int iSlen,unsigned char *pDst,int *iDlen,CString &strErrorInfo)
{
strErrorInfo=_T("");
int iRet=base64_encode(pSrc,iSlen,pDst,iDlen);
if(iRet==XYSSL_ERR_BASE64_BUFFER_TOO_SMALL)
strErrorInfo=_T("分配的缓冲区太小!");
else if(iRet==XYSSL_ERR_BASE64_INVALID_CHARACTER)
strErrorInfo=_T("无效数据!");
return iRet==0;
}
BOOL CBase64::Decrypt(const unsigned char *pSrc,int iSlen,unsigned char *pDst,int *iDlen,CString &strErrorInfo)
{
strErrorInfo=_T("");
int iRet=base64_decode(pSrc,iSlen,pDst,iDlen);
if(iRet==XYSSL_ERR_BASE64_BUFFER_TOO_SMALL)
strErrorInfo=_T("分配的缓冲区太小!");
else if(iRet==XYSSL_ERR_BASE64_INVALID_CHARACTER)
strErrorInfo=_T("无效数据!");
return iRet==0;
}
标签:des style blog http io ar color os 使用
原文地址:http://my.oschina.net/jackliang55/blog/356133