MFC BASE64加解密 算法

Posted xuandi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MFC BASE64加解密 算法相关的知识,希望对你有一定的参考价值。

 1 unsigned char * base64 = (unsigned char *)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 2 
 3 CString ToolFiles::base64encode(CString src, int srclen)
 4 
 5 int n, buflen, i, j;
 6 static unsigned char *dst;
 7 CString buf = src;
 8 buflen = n = srclen;
 9 dst = (unsigned char*)malloc(buflen / 3 * 4 + 3);
10 memset(dst, 0, buflen / 3 * 4 + 3);
11 for (i = 0, j = 0; i <= buflen - 3; i += 3, j += 4) 
12 dst[j] = (buf[i] & 0xFC) >> 2;
13 dst[j + 1] = ((buf[i] & 0x03) << 4) + ((buf[i + 1] & 0xF0) >> 4);
14 dst[j + 2] = ((buf[i + 1] & 0x0F) << 2) + ((buf[i + 2] & 0xC0) >> 6);
15 dst[j + 3] = buf[i + 2] & 0x3F;
16 
17 if (n % 3 == 1) 
18 dst[j] = (buf[i] & 0xFC) >> 2;
19 dst[j + 1] = ((buf[i] & 0x03) << 4);
20 dst[j + 2] = 64;
21 dst[j + 3] = 64;
22 j += 4;
23 
24 else if (n % 3 == 2) 
25 dst[j] = (buf[i] & 0xFC) >> 2;
26 dst[j + 1] = ((buf[i] & 0x03) << 4) + ((buf[i + 1] & 0xF0) >> 4);
27 dst[j + 2] = ((buf[i + 1] & 0x0F) << 2);
28 dst[j + 3] = 64;
29 j += 4;
30 
31 for (i = 0; i < j; i++) 
32 dst[i] = base64[(int)dst[i]];
33 dst[j] = 0;
34 return CString(dst);
35 
36 
37 CString ToolFiles::base64decode(CString inpt, int * len)
38 
39 int n, i, j, pad;
40 unsigned char *p;
41 static unsigned char *dst;
42 unsigned char * src;
43 *len = 0;
44 pad = 0;
45 n = inpt.GetLength();
46 src = new unsigned char[n];
47 for (i = 0; i < n; i++)
48 src[i] = inpt[i];
49 
50 while (n>0 && src[n - 1] == =) 
51 src[n - 1] = 0;
52 pad++;
53 n--;
54 
55 for (i = 0; i < n; i++)  
56 p = (unsigned char *)strchr((const char *)base64, (int)src[i]);
57 if (!p)
58 break;
59 src[i] = p - (unsigned char *)base64;
60 
61 
62 dst = (unsigned char *)malloc(n * 3 / 4 + 1);
63 memset(dst, 0, n * 3 / 4 + 1);
64 for (i = 0, j = 0; i < n; i += 4, j += 3) 
65 dst[j] = (src[i] << 2) + ((src[i + 1] & 0x30) >> 4);
66 dst[j + 1] = ((src[i + 1] & 0x0F) << 4) + ((src[i + 2] & 0x3C) >> 2);
67 dst[j + 2] = ((src[i + 2] & 0x03) << 6) + src[i + 3];
68 *len += 3;
69 
70 *len -= pad;
71 return CString(dst);
72 

有问题 加我QQ:97184072 

以上是关于MFC BASE64加解密 算法的主要内容,如果未能解决你的问题,请参考以下文章

(转)base64编码(严格说来,base64不算作加解密算法)

PHP的aes加解密算法

JavaScript Base64加解密

在线加/解密

base64/62 加解密的实现。

1.Java 加解密技术系列之 BASE64