• 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 

  • 相关阅读:
    转让malloc()该功能后,发生了什么事内核?附malloc()和free()实现源
    使用智能移动设备访问Ossim制
    POJ 3207 Ikki&#39;s Story IV
    AndroidMainifest标签说明2——&lt;activity&gt;
    POJ1149 PIGS 【最大流量】
    POJ3617 Best Cow Line 馋
    颜色(color)转换为三刺激值(r/g/b)(干股)
    关于SQL中的Update语句
    Java Script 正则表达式的使用示例
    Javascript 知识点简介
  • 原文地址:https://www.cnblogs.com/xuandi/p/11369415.html
Copyright © 2020-2023  润新知