#include <stdio.h> #include
<string.h>
/************************************************************** 認証用ユーザIDとパスワードから,「認証用ユーザID+'\0'+認証用ユーザID+'\0'+パスワード」の 文字列をBASE64でMIME変換した文字列を生成する AuthPlainGenarator genarating
auth plain string from userID and password usage:
AuthPlainGenerator userID password
AuthPlainGenerator /c example: AuthPlainGenerator
watashi watahinopassword AuthPlainGenerator taro
uso800 **************************************************************/ //mime変換のためのテーブル
0123456789012345678901234567890123456789012345678901234567890123 const
char
MimeEncodeTable[70]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//文字列intext(normal)を文字列otext(mime
encoded)に変換 //BASE64を利用したMIME変換 void PureMimeEncode(char *intext,
char *otext, int len) { unsigned char
*p; unsigned long int x,x1;
unsigned char tmp[8]={0,0,0,0,0,0,0,0}; char
one[4]="A"; int add,len1,i,j;
add=0; len1=len; if (len%3!=0)
{ add=
3-(len%3);
len1=len/3*3; } p=(unsigned
char *)intext; *otext=0; for
(i=0; i<len1; i+=3) {
x=(p[0]<<16)+(p[1]<<8)+p[2];
x1=(x>>18)&0x3f; one[0]=MimeEncodeTable[x1];
strcat(otext,one);
x1=(x>>12)&0x3f; one[0]=MimeEncodeTable[x1];
strcat(otext,one);
x1=(x>>06)&0x3f; one[0]=MimeEncodeTable[x1];
strcat(otext,one);
x1=( x )&0x3f; one[0]=MimeEncodeTable[x1];
strcat(otext,one);
p+=3; } if (add)
{ for (j=0 ; i<len;
i++,j++) tmp[j]=*p++;
x=(tmp[0]<<16)+(tmp[1]<<8)+tmp[2];
x1=(x>>18)&0x3f; one[0]=MimeEncodeTable[x1];
strcat(otext,one);
x1=(x>>12)&0x3f; one[0]=MimeEncodeTable[x1];
strcat(otext,one);
x1=(x>>06)&0x3f; one[0]=MimeEncodeTable[x1];
strcat(otext,one);
x1=( x )&0x3f; one[0]=MimeEncodeTable[x1];
strcat(otext,one); } //add=0
-> del=0, add=1 -> del=1, add=2 -> del=2
p=otext+strlen(otext)-1; for (i=0; i<add; i++)
*p--='='; }
//ユーザIDuserとパスワードpassからauth文字列(mime(user\0user\0pass))を作る void
makeAuthPlain(char *user, char *pass, char
*auth) { int num,i; char
plain[256];
strcpy(plain,user);
strcat(plain,"\t");
strcat(plain,user);
strcat(plain,"\t");
strcat(plain,pass);
//puts(plain);
num=strlen(plain); for (i=0;i<num;i++) if
(plain[i]=='\t') plain[i]=0;
PureMimeEncode(plain,auth,num); }
int main(int argc, char
*argv[]) { char
userid[256],passwd[256]; char
MimeEncoded[256]; if (argc==1)
{
printf("AuthPlainGenarator\n");
printf("genarating auth plain string from userID and
password\n"); printf("Please
enter userID and
password\n"); printf("userID
:");
gets(userid);
printf("password :");
gets(passwd); } else if (argc==2)
{
printf("AuthPlainGenarator\n");
printf("genarating auth plain string from userID and
password\n");
printf("usage:\n");
printf(" AuthPlainGenerator userID
password\n");
printf(" AuthPlainGenerator
/c\n");
printf("example:\n");
printf(" AuthPlainGenerator watashi
watahinopassword\n");
printf(" AuthPlainGenerator taro
uso800\n");
getchar(); return
1; } else
{
strcpy(userid,argv[1]);
strcpy(passwd,argv[2]); }
printf("userID: %s\n",userid); printf("passwd:
%s\n",passwd); makeAuthPlain(userid, passwd,
MimeEncoded); printf("auth plain string:
%s\n",MimeEncoded); getchar();
return 0; }
|