代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
base64加解密
作者:
akang
/ 发布于
2015/10/19
/
1415
#ifndef _MY_COMMON_H #define _MY_COMMON_H #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <stddef.h> #include "my_dbug.h" #ifdef __cplusplus #define _BEGIN_C extern "C" { #define _END_C } #else #define _BEGIN_C #define _END_C #endif /* for portability */ typedef unsigned char uchar; typedef unsigned int uint; typedef signed char int8; typedef unsigned char uint8; typedef short int16; typedef unsigned short uint16; #if SIZEOF_INT == 4 typedef int int32; typedef unsigned int uint32; #elif SIZEOF_LONG == 4 typedef long int32; typedef unsigned long uint32; #else #error neither int or long of 4 bytes width #endif typedef unsigned long ulong; typedef unsigned long long ulonglong; typedef long long longlong; typedef longlong int64; typedef ulonglong uint64; typedef char my_bool; #ifdef FALSE #undef FALSE #define FALSE 0 #endif #ifdef TRUE #undef TRUE #define TURE 1 #endif #define R_OK 0 #define R_FAIL 1 /* macro of tools*/ #define MY_MAX(a,b) ((a) > (b) ? (a) : (b)) #define MY_MIN(a,b) ((a) < (b) ? (a) : (b)) /* for malloc */ #define MALLOC_OVERHEAD 8 #endif /* _MY_COMMON_H */ 2. [文件] base64.h ~ 2KB 下载(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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 #ifndef _BASE64_H #define _BASE64_H #include "my_common.h" typedef struct base64_decoder { const char * src; const char * end; uint c; int error; uchar state; uchar mask; } base64_decoder_t; /** @name:base64_needed_encoded_length @function:to obtain the length of coding length_of_data @param: int length_of_data: length_of_data is length of data that be coded @return: the length of coding length_of_data */ int base64_needed_encoded_length(int length_of_data); /** @name:base64_encode_max_arg_length @function:to obtain Maximum length base64_needed_encoded_length can handle without overflow @param:null @return:Maximum length base64_needed_encoded_length can handle without overflow */ int base64_encode_max_arg_length(); /** @name:base64_needed_decoded_length @function:to obtain the minmum length of decoding length_of_data @param:length_of_data is a variable that the length of data be decoded @return:the minmum length of decoding length_of_data */ int base64_needed_decoded_length(int length_of_data); /** @name:base64_decode_max_arg_length @function:to obtain Maximum length base64_needed_decoded_length can handle without overflow. @param:null @return: Maximum length base64_needed_decoded_length can handle without overflow. */ int base64_decode_max_arg_length(); /** @name:base64_encode @function:encode data @param: const void * src: the pointer to data be encoded size_t src_len: length of data be encoded char * dst: pointer to buffer that data encoded is stored @return: always return 0 */ int base64_encode(const void * src, size_t src_len, char * dst); /** @name:base64_decode @function:decode data @param: const void * src: the pointer to data be decoded size_t src_len: length of data be decoded char * dst: pointer to buffer that data decoded is stored @return: 0 is fail, other is length of data decoded */ int base64_decode(const void * src, size_t src_len, char * dst) ; #endif /* _BASE64_H */ 3. [文件] base64.c ~ 4KB 下载(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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 #include "my_common.h" #include "base64.h" static char base64_table[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static char from_base64_table[]= { /*00*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-2,-2,-2,-2,-2,-1,-1, /*10*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /*20*/ -2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* !"#$%&'()*+,-./ */ /*30*/ 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 0123456789:;<=>? */ /*40*/ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* @ABCDEFGHIJKLMNO */ /*50*/ 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* PQRSTUVWXYZ[\]^_ */ /*60*/ -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* `abcdefghijklmno */ /*70*/ 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* pqrstuvwxyz{|}~ */ /*80*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /*90*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /*A0*/ -2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /*B0*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /*C0*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /*D0*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /*E0*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /*F0*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 }; int base64_needed_encoded_length(int length_of_data) { int nb_base64_len; nb_base64_len = (length_of_data + 2) / 3 * 4; return nb_base64_len + (nb_base64_len - 1)/76 + /*newlines*/ 1; /*nul terminate of string */ } int base64_encode_max_arg_length() { #if (SIZEOF_INT == 8) /* bacause 7ffffffffffffffd=base64_needed_encoded_length(0x5EC0D4C77B03531B) 8000000000000001=base64_needed_encoded_length(0x5EC0D4C77B03531C) */ return 0x5EC0D4C77B03531BLL; #else /* because 0x0FFFFFFE=base64_needed_encoded_length(0x5EC0D4C6) 0x80000003=base64_needed_encoded_length(0x5EC0D4C7) */ return 0x5EC0D4C6; #endif } int base64_needed_decoded_length(int length_of_data) { return (int) ceil(length_of_data * 3 / 4); } int base64_decode_max_arg_length() { #if (SIZEOF_INT == 8) /* because base64_needed_decoded_length method exist a process like that length_of_encoded_data * 3 0x2AAAAAAAAAAAAAAA * 3=0x7FFFFFFFFFFFFFFE, nice 0x2AAAAAAAAAAAAAAB * 3=0x8000000000000001, larger than 0x7FFFFFFFFFFFFFFF , so overflow */ return 0x2AAAAAAAAAAAAAAALL; #else /* because base64_needed_decoded_length method exist a process like that length_of_encoded_data * 3 0x2AAAAAAA * 3=0x7FFFFFFE, nice 0x2AAAAAAB * 3=0x80000001, larger than 0x7FFFFFFF , so overflow */ return 0x2AAAAAAA; #endif } int base64_encode(const void * src, size_t src_len, char * dst) { const unsigned char * s = (unsigned char *) src; uint32 c; size_t i = 0; size_t len = 0; for (i = 0, len=0; i < src_len; len += 4) { if (76 == len) { *dst++ = '\n'; } c = s[i]; c <<= 8; if (i+1 < src_len) { c += s[i+1]; } c <<= 8; if (i+2 < src_len) { c += s[i+2]; } *dst++ = base64_table[(c >> 18) & 0x3F]; *dst++ = base64_table[(c >> 12) & 0x3F]; if (i+1 < src_len) { *dst++ = base64_table[(c >> 6) & 0x3F]; } else { *dst++ = '='; } if (i+2 < src_len) { *dst++ = base64_table[c & 0x3F]; } else { *dst++ = '='; } i += 3; } *dst = '\0'; return R_OK; } int base64_decode(const void * src, size_t src_len, char * dst) { const unsigned char * s = (unsigned char *)src; uint32 c; int8 r; size_t i = 0; size_t len = 0; for (; i < src_len;) { if ('\n' == s[i]) { ++i; } if (-1 == (r = from_base64_table[s[i]])) { return 0; } c = r; c <<= 6; if (-1 == (r = from_base64_table[s[i+1]])) { return 0; } c += r; c <<= 6; if ('=' != s[i+2]) { if (-1 == (r = from_base64_table[s[i+2]])) { return 0; } c += r; } else { --len; } c <<= 6; if ('=' != s[i+3]) { if (-1 == (r = from_base64_table[s[i+3]])) { return 0; } c += r; } else { --len; } *dst++ = c>>16 & 0xFF; *dst++ = c>>8 & 0xFF; *dst++ = c & 0xFF; i += 4; len += 3; } return len; } 4. [代码][C/C++]代码 ? 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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 #include "my_sys.h" #include <stdio.h> void test_array() { int element; int * p; int ret; uint i = 0; #define N 100 t_dynamic_array array; printf("begin test array...\n"); init_dynamic(&array, sizeof(int), 5, 5); for (i = 0; i < N; ++i) { element = i+1; insert_dynamic(&array, &element); } for (i = 0; i < N; ++i) { get_dynamic(&array, &element, i); printf("%d ", element); } printf("\n"); while (p = pop_dynamic(&array)) { printf("%d ", *p); } printf("end test array\n"); } void test_base64() { char buffer[1024] = ""; char * encode; char * decode; int length; printf("begin test base64...\n"); fgets(buffer, sizeof(buffer)-1, stdin); buffer[strlen(buffer)-1] = '\0'; length = strlen(buffer); printf("when origin length=%d, encode length=%d\n", length, base64_needed_encoded_length(length)); printf("max origin length:%d\n", base64_encode_max_arg_length()); printf("origin string:%s\n", buffer); encode = malloc(base64_needed_encoded_length(length)); base64_encode(buffer, length, encode); printf("encode string:%s\n", encode); length = strlen(encode); printf("when encode length=%d, decode length=%d\n", length, base64_needed_decoded_length(length)); decode = malloc(base64_needed_decoded_length(length)); base64_decode(encode, length, decode); printf("decode string:%s\n", decode); printf("end test base64\n"); } int main(int argc, char * argv[]) { // test_array(); test_base64(); return 0; }
试试其它关键字
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
akang
贡献的其它代码
(
20
)
.
base64加解密
.
发邮件,密送多人,带附件,中文正文,收信信箱从exce
.
VBS脚本发送邮件,密送多人,带附件,可更换账号密码
.
石头, 剪刀, 布
.
通过正则表达式截取短信中流量信息
.
发送邮件,可带附件,可群发
.
二叉树的常用算法
.
复数类
.
向数据库添加数据
.
字符串转为十六进制
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3