//プロジェクト名crypto001 C++ソースファイルcrypto001.cpp #define _WIN32_WINNT 0x0500 #include #include #include #include extern "C" { __declspec(dllexport) char * text_to_crypto(char * string01,char * string02,char * string03); __declspec(dllexport) char * crypto_to_text(char * string01,char * string02,char * string03); void base64_encode(int size,char* src, char* dst); int base64_decode(char* src, char* dst); static char* b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; //パスワード、平文_to_暗号文 char * text_to_crypto(char * string01,char * string02,char * string03) { char *param,*param1; int n,len,done; done = FALSE; HCRYPTPROV hProv; HCRYPTHASH hHash; HCRYPTKEY hKey; len = strlen(string02); DWORD dwDataLen = len+1; param = (char *)calloc(1,len*2 + 1024); DWORD dwBufLen = len*2 + 1024; param = strcpy(param, string02); if(CryptAcquireContext(&hProv,NULL,MS_DEF_PROV,PROV_RSA_FULL,0)) done = TRUE; else if(CryptAcquireContext(&hProv,NULL,MS_DEF_PROV,PROV_RSA_FULL,CRYPT_NEWKEYSET)) done = TRUE; if (done) { done = FALSE; if(CryptCreateHash(hProv,CALG_MD5,0,0,&hHash)) { if(CryptHashData(hHash,(BYTE*)string01,(DWORD)strlen(string01),0)) { if(CryptDeriveKey(hProv,CALG_RC4,hHash,CRYPT_EXPORTABLE,&hKey)) { if(CryptEncrypt(hKey,0,TRUE,0,(unsigned char *)param,&dwDataLen,dwBufLen)) { done = TRUE; } } } } } if (done) { n=dwDataLen; param1 = (char *)calloc(1,n*2 + 1024); base64_encode(n,param,param1); len=strlen(string03); n=strlen(param1); if (len>=n+1) { memcpy(string03,param1,n); * (string03+n)='\0'; } else { * string03='\0'; } } else { * string03='\0'; } free(param); free(param1); CryptReleaseContext(hProv, 0); CryptDestroyHash(hHash); CryptDestroyHash(hKey); return string03; } //パスワード、暗号文_to_平文 char * crypto_to_text(char * string01,char * string02,char * string03) { char *param; int n,len,done; done = FALSE; HCRYPTPROV hProv; HCRYPTHASH hHash; HCRYPTKEY hKey; len = strlen(string02); param = (char *)calloc(1,len*2 + 1024); n=base64_decode(string02, param); DWORD dwDataLen = n; if(CryptAcquireContext(&hProv,NULL,MS_DEF_PROV,PROV_RSA_FULL,0)) done = TRUE; else if(CryptAcquireContext(&hProv,NULL,MS_DEF_PROV,PROV_RSA_FULL,CRYPT_NEWKEYSET)) done = TRUE; if (done) { done = FALSE; if(CryptCreateHash(hProv,CALG_MD5,0,0,&hHash)) { if(CryptHashData(hHash,(BYTE*)string01,(DWORD)strlen(string01),0)) { if(CryptDeriveKey(hProv,CALG_RC4,hHash,CRYPT_EXPORTABLE,&hKey)) { if(CryptDecrypt(hKey,0,TRUE,0,(unsigned char *)param,&dwDataLen)) { done = TRUE; } } } } } if (done) { len=strlen(string03); n=dwDataLen; if (len>=n+1) { memcpy(string03,param,n); * (string03+n)='\0'; } else { * string03='\0'; } } else { * string03='\0'; } free(param); CryptReleaseContext(hProv, 0); CryptDestroyHash(hHash); CryptDestroyHash(hKey); return string03; } void base64_encode(int size,char* src, char* dst) { int i, j; int cnt = 0; int pos = 0; int wk = 0; char* p = dst; for(i=0 ; i=0 ; j--) { *p = b64[(wk>>(6*j)) & 0x3f]; p++; if( ++cnt == 76 ) // MIME { *p = '\r'; p++; *p = '\n'; p++; cnt = 0; } } wk = 0; pos = 0; } else pos++; } if( pos >= 1 ) { for(i=0 ; i<(3-pos) ; i++) wk <<= 8; for(j=3 ; j>=0 ; j--) { if( (3-pos) <= j ) *p = b64[(wk>>(6*j)) & 0x3f]; else *p = '='; p++; if( ++cnt == 76 ) { *p = '\r'; p++; *p = '\n'; p++; cnt = 0; } } } *p = '\0'; } int base64_decode(char* src, char* dst) { int i, j; int n = 0; char c; int pos = 0; int wk = 0; char* p = dst; int end01=0; for(i=0 ; (unsigned)i= 'A' && c <= 'Z' ) wk |= (c - 'A') + 0; else if( c >= 'a' && c <= 'z' ) wk |= (c - 'a') + 26; else if( c >= '0' && c <= '9' ) wk |= (c - '0') + 52; else if( c == '+' ) wk |= 62; else if( c == '/' ) wk |= 63; else if( c == '=' ) { wk |= 0; end01++; } else { wk >>= 6; continue; } if( pos == 3 ) { if (end01==0) { for(j=2 ; j>=0 ; j--) { *p = (wk>>(j*8) & 0xff); p++; n++; } } if (end01==1) { for(j=2 ; j>=1 ; j--) { *p = (wk>>(j*8) & 0xff); p++; n++; } } if (end01==2) { for(j=2 ; j>=2 ; j--) { *p = (wk>>(j*8) & 0xff); p++; n++; } } wk = 0; pos = 0; } else pos++; } if (pos==2) { for(i=0 ; i<(4-pos) ; i++) wk <<= 6; for(j=2 ; j>=2 ; j--) { *p = (wk>>(j*8) & 0xff); p++; n++; } } if (pos==3) { for(i=0 ; i<(4-pos) ; i++) wk <<= 6; for(j=2 ; j>=1 ; j--) { *p = (wk>>(j*8) & 0xff); p++; n++; } } *p = '\0'; return n; } }