void _tmain(int argc, TCHAR **argv) { BOOL fEncrypt = FALSE; LPTSTR lpszCertificateName = NULL; LPTSTR lpszCertificateStoreName = NULL; LPTSTR lpszInputFileName = NULL; LPTSTR lpszOutputFileName = NULL; DWORD dwCertStoreOpenFlags = CERT_SYSTEM_STORE_CURRENT_USER; if (argc != 7) { PrintUsage(); return; } /* Check whether the action to be performed is encrypt or decrypt */ if (_tcsicmp(argv[1], _T("/e")) == 0) { fEncrypt = TRUE; } else if (_tcsicmp(argv[1], _T("/d")) == 0) { fEncrypt = FALSE; } else { PrintUsage(); return; } lpszCertificateName = argv[2]; lpszCertificateStoreName = argv[3]; /* Check whether the certificate store to be opened is user or machine */ if (_tcsicmp(argv[4], _T("/u")) == 0) { dwCertStoreOpenFlags = CERT_SYSTEM_STORE_CURRENT_USER; } else if (_tcsicmp(argv[4], _T("/m")) == 0) { dwCertStoreOpenFlags = CERT_SYSTEM_STORE_LOCAL_MACHINE; } else { PrintUsage(); return; } lpszInputFileName = argv[5]; lpszOutputFileName = argv[6]; EncryptDecryptFile(lpszCertificateName, lpszCertificateStoreName, dwCertStoreOpenFlags, lpszInputFileName, lpszOutputFileName, fEncrypt); }
bool ConfigFile::Load(const std::string &filename) { if (filename.empty()) { return false; } in_close(); fin=fopen(filename.c_str(),"rt"); #ifndef WIN32 if (fin == NULL) { //add some logic for encryption file //if file not exit //add .enc to filename; //try to open the encrypted file again. //if still not exit,return false; //else decrypt the file to a new file named fileName.tmp,add tempfileName=ileName.tmp //open the temp file to fin. //if still not exit,return false; std::string strEncryptedFile; char acDecryptedFile[256]; bool status=false; char acBaseName[256]; char acTmpBuf[256]; char *szDstCur = acTmpBuf; const char *szSrcCur = filename.c_str() + filename.length() - 1; memset(acTmpBuf, 0, sizeof(acTmpBuf)); while(szSrcCur >= filename.c_str()) { if('\\' == *szSrcCur || '/' == *szSrcCur) break; if(szSrcCur == filename.c_str()) { *szDstCur = *szSrcCur; break; } *szDstCur++ = *szSrcCur--; } if(strlen(acTmpBuf) > 0) { szSrcCur = acTmpBuf + strlen(acTmpBuf) - 1; szDstCur = acBaseName; while(szSrcCur >= acTmpBuf) *szDstCur++ = *szSrcCur--; *szDstCur = '\0'; } #ifdef WIN32 UTILITY::Snprintf(acDecryptedFile, sizeof(acDecryptedFile), "%s.Dec", acBaseName); #else UTILITY::Snprintf(acDecryptedFile, sizeof(acDecryptedFile), "/tmp/%s.Dec", acBaseName); #endif strEncryptedFile = filename; strEncryptedFile.append(".enc"); fin = fopen(strEncryptedFile.c_str(), "rt"); if (NULL == fin) { printf("File[%s] Not Found\n", strEncryptedFile.c_str()); return false; } status = EncryptDecryptFile((char*)strEncryptedFile.c_str(), acDecryptedFile, "CFETS", INDC_ONLY_DECRYPT); if (status != true) { printf("Error in Decrypting File[%s]\n",strEncryptedFile.c_str()); fclose(fin); return false; } fclose(fin); fin = NULL; UTILITY::Snprintf(m_acDecryptedFile, sizeof(m_acDecryptedFile), "%s", acDecryptedFile); fin = fopen(acDecryptedFile, "rt"); if (NULL == fin) { printf("Decrypted File[%s] Not Found\n", acDecryptedFile); return false; } } #else if(NULL == fin) { printf("Config file[%s] not found\n", filename); return false; } #endif return true; }