求Base64代码

michaelsll 2008-01-23 02:57:19
求Base64的C语言代码,编解码都要~~
...全文
67 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
http://base64.sourceforge.net/b64.c


#include <stdio.h>
#include <stdlib.h>

/*
** Translation Table as described in RFC1113
*/
static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

/*
** Translation Table to decode (created by author)
*/
static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq";

/*
** encodeblock
**
** encode 3 8-bit binary bytes as 4 '6-bit' characters
*/
void encodeblock( unsigned char in[3], unsigned char out[4], int len )
{
out[0] = cb64[ in[0] >> 2 ];
out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ];
out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=');
out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '=');
}

/*
** encode
**
** base64 encode a stream adding padding and line breaks as per spec.
*/
void encode( FILE *infile, FILE *outfile, int linesize )
{
unsigned char in[3], out[4];
int i, len, blocksout = 0;

while( !feof( infile ) ) {
len = 0;
for( i = 0; i < 3; i++ ) {
in[i] = (unsigned char) getc( infile );
if( !feof( infile ) ) {
len++;
}
else {
in[i] = 0;
}
}
if( len ) {
encodeblock( in, out, len );
for( i = 0; i < 4; i++ ) {
putc( out[i], outfile );
}
blocksout++;
}
if( blocksout >= (linesize/4) || feof( infile ) ) {
if( blocksout ) {
fprintf( outfile, "\r\n" );
}
blocksout = 0;
}
}
}

/*
** decodeblock
**
** decode 4 '6-bit' characters into 3 8-bit binary bytes
*/
void decodeblock( unsigned char in[4], unsigned char out[3] )
{
out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4);
out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2);
out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]);
}

/*
** decode
**
** decode a base64 encoded stream discarding padding, line breaks and noise
*/
void decode( FILE *infile, FILE *outfile )
{
unsigned char in[4], out[3], v;
int i, len;

while( !feof( infile ) ) {
for( len = 0, i = 0; i < 4 && !feof( infile ); i++ ) {
v = 0;
while( !feof( infile ) && v == 0 ) {
v = (unsigned char) getc( infile );
v = (unsigned char) ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]);
if( v ) {
v = (unsigned char) ((v == '$') ? 0 : v - 61);
}
}
if( !feof( infile ) ) {
len++;
if( v ) {
in[ i ] = (unsigned char) (v - 1);
}
}
else {
in[i] = 0;
}
}
if( len ) {
decodeblock( in, out );
for( i = 0; i < len - 1; i++ ) {
putc( out[i], outfile );
}
}
}
}

/*
** returnable errors
**
** Error codes returned to the operating system.
**
*/
#define B64_SYNTAX_ERROR 1
#define B64_FILE_ERROR 2
#define B64_FILE_IO_ERROR 3
#define B64_ERROR_OUT_CLOSE 4
#define B64_LINE_SIZE_TO_MIN 5

/*
** b64_message
**
** Gather text messages in one place.
**
*/
char *b64_message( int errcode )
{
#define B64_MAX_MESSAGES 6
char *msgs[ B64_MAX_MESSAGES ] = {
"b64:000:Invalid Message Code.",
"b64:001:Syntax Error -- check help for usage.",
"b64:002:File Error Opening/Creating Files.",
"b64:003:File I/O Error -- Note: output file not removed.",
"b64:004:Error on output file close.",
"b64:004:linesize set to minimum."
};
char *msg = msgs[ 0 ];

if( errcode > 0 && errcode < B64_MAX_MESSAGES ) {
msg = msgs[ errcode ];
}

return( msg );
}

/*
** b64
**
** 'engine' that opens streams and calls encode/decode
*/

int b64( int opt, char *infilename, char *outfilename, int linesize )
{
FILE *infile;
int retcode = B64_FILE_ERROR;

if( !infilename ) {
infile = stdin;
}
else {
infile = fopen( infilename, "rb" );
}
if( !infile ) {
perror( infilename );
}
else {
FILE *outfile;
if( !outfilename ) {
outfile = stdout;
}
else {
outfile = fopen( outfilename, "wb" );
}
if( !outfile ) {
perror( outfilename );
}
else {
if( opt == 'e' ) {
encode( infile, outfile, linesize );
}
else {
decode( infile, outfile );
}
if (ferror( infile ) || ferror( outfile )) {
retcode = B64_FILE_IO_ERROR;
}
else {
retcode = 0;
}
if( outfile != stdout ) {
if( fclose( outfile ) != 0 ) {
perror( b64_message( B64_ERROR_OUT_CLOSE ) );
retcode = B64_FILE_IO_ERROR;
}
}
}
if( infile != stdin ) {
fclose( infile );
}
}

return( retcode );
}

/*
** showuse
**
** display usage information, help, version info
*/
void showuse( int morehelp )
{
{
printf( "\n" );
printf( " b64 (Base64 Encode/Decode) Bob Trower 08/03/01 \n" );
printf( " (C) Copr Bob Trower 1986-01. Version 0.00B \n" );
printf( " Usage: b64 -option [ -l num ] [<FileIn> [<FileOut>]] \n" );
printf( " Purpose: This program is a simple utility that implements\n" );
printf( " Base64 Content-Transfer-Encoding (RFC1113). \n" );
}
if( !morehelp ) {
printf( " Use -h option for additional help. \n" );
}
else {
printf( " Options: -e encode to Base64 -h This help text. \n" );
printf( " -d decode from Base64 -? This help text. \n" );
printf( " Note: -l use to change line size (from 72 characters)\n" );
printf( " Returns: 0 = success. Non-zero is an error code. \n" );
printf( " ErrCode: 1 = Bad Syntax, 2 = File Open, 3 = File I/O \n" );
printf( " Example: b64 -e binfile b64file <- Encode to b64 \n" );
printf( " b64 -d b64file binfile <- Decode from b64 \n" );
printf( " b64 -e -l40 infile outfile <- Line Length of 40 \n" );
printf( " Note: Will act as a filter, but this should only be \n" );
printf( " used on text files due to translations made by \n" );
printf( " operating systems. \n" );
printf( " Release: 0.00.00, Tue Aug 7 2:00:00 2001, ANSI-SOURCE C\n" );
}
}

#define B64_DEF_LINE_SIZE 72
#define B64_MIN_LINE_SIZE 4

#define THIS_OPT(ac, av) (ac > 1 ? av[1][0] == '-' ? av[1][1] : 0 : 0)

/*
** main
**
** parse and validate arguments and call b64 engine or help
*/


michaelsll 2008-01-23
  • 打赏
  • 举报
回复
太快了!太感谢了!!!
Mnky 2008-01-23
  • 打赏
  • 举报
回复
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// base64 tables
static char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
char * base64_encode(const unsigned char *value, int vlen);
unsigned char * base64_decode(const char *value, int *rlen);

static signed char index_64[128] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
-1, 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,-1, -1,-1,-1,-1,
-1,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,-1, -1,-1,-1,-1
} ;

char *base64_encode(const unsigned char *value, int vlen) {
unsigned char oval = 0 ;
char *result = (char *)malloc((vlen * 4) / 3 + 5) ;
char *out = result;
while (vlen >= 3) {
*out++ = basis_64[value[0] >> 2];
*out++ = basis_64[((value[0] << 4) & 0x30) | (value[1] >> 4)];
*out++ = basis_64[((value[1] << 2) & 0x3C) | (value[2] >> 6)];
*out++ = basis_64[value[2] & 0x3F];
value += 3;
vlen -= 3;
}
if (vlen > 0) {
*out++ = basis_64[value[0] >> 2];
oval = (value[0] << 4) & 0x30 ;
if (vlen > 1) oval |= value[1] >> 4;
*out++ = basis_64[oval];
*out++ = (vlen < 2) ? '=' : basis_64[(value[1] << 2) & 0x3C];
*out++ = '=';
}
*out = '\0';
return result;
}
unsigned char *base64_decode(const char *value, int *rlen)
{
int c1, c2, c3, c4;
int vlen = strlen(value);
unsigned char *result =(unsigned char *)malloc((vlen * 3) / 4 + 1);
unsigned char *out = result;

*rlen = 0;

while (1) {
if (value[0]==0) {
*out = '\0' ;
return result;
}
c1 = value[0];
if (CHAR64(c1) == -1) goto base64_decode_error;
c2 = value[1];
if (CHAR64(c2) == -1) goto base64_decode_error;
c3 = value[2];
if ((c3 != '=') && (CHAR64(c3) == -1)) goto base64_decode_error;
c4 = value[3];
if ((c4 != '=') && (CHAR64(c4) == -1)) goto base64_decode_error;
value += 4;
*out++ = (CHAR64(c1) << 2) | (CHAR64(c2) >> 4);
*rlen += 1;
if (c3 != '=') {
*out++ = ((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2);
*rlen += 1;
if (c4 != '=') {
*out++ = ((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4);
*rlen += 1;
}
}
}
base64_decode_error:
*result = 0;
*rlen = 0;
return result;
}
面向工程应用:市面上的一些密码学课程和密码学的书籍,很多都是从考证出发,讲解算法原理并不面向工程应用,而我们现在缺少的是工程应用相关的知识,本课程从工程应用出发,每种技术都主要讲解其在工程中的使用,并演示工程应用的代码。 从零实现部分算法: 课程中实现了base16编解码 ,XOR对称加解密算法,PKCS7 pading数据填充算法,通过对一些简单算法的实现,从而加深对密码学的理解。理论与实践结合: 课程如果只是讲代码,同学并不能理解接口背后的原理,在项目设计中就会留下隐患,出现错误也不容易排查出问题。如果只讲理论,比如对密码学的一些研究,对于大部分从事工程应用的同学并没有必要,而是理论与实践结合,一切为了工程实践。代码现场打出: 代码不放在ppt而是现场打出,更好的让学员理解代码编写的逻辑,老师现场敲出代码正是展示出了工程项目的思考,每个步骤为什么要这么做,考虑了哪些异常,易学不枯燥: 课程为了确保大部分人开发者都学得会,理解算法原理(才能真正理解算法特性),学会工程应用(接口调用,但不局限接口调用,理解接口背后的机制,并能解决工程中会出现的问题),阅读算法源码但不实现密码算法,,并能将密码学投入到实际工程中,如果是想学习具体的加密算法实现,请关注我后面的课程。

69,393

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧