암호 걸린 Access(.MDB) 파일의 암호 확인하기 Posted by 빵빵빵 2009/06/11 17:13 전산(컴퓨터)/PC-Windows 전산(컴퓨터)/PC-Windows 출처 : http://neodreamer.tistory.com/85종종 암호가 걸린 Access 파일을 봐야하는 경우가 있어 구해 놓은 프로그램이다.Access 2003 까지는 되는걸 확인했는데 최신 Access 2007 의 암호도 확일할 수 있는지 모르겠다.프로그램을 실행시키고 "Get Password" 버튼을 눌러 Access 파일을 선택하면 암호가 걸려 있는경우 프로그램 창에 암호가 출력된다. AccessPassView.7z 2009/06/11 17:13 2009/06/11 17:13 Tags Access, MDB, MDB 비밀번호 찾기, 비밀번호 Trackback: 0 Comment: 0 이 글에는 트랙백을 보낼 수 없습니다
리소스 실행시에 파일로 내보내기 Posted by 빵빵빵 2009/06/11 17:04 전산(컴퓨터)/PC-Windows 전산(컴퓨터)/PC-Windows #include <io.h> #include <stdlib.h> HMODULE hModule = GetModuleHandle(NULL); TCHAR szFilename[MAX_PATH]; TCHAR Drive[_MAX_DRIVE]; TCHAR Path[_MAX_DIR]; TCHAR Filename[_MAX_FNAME]; TCHAR Ext[_MAX_EXT]; GetModuleFileName(NULL, szFilename, sizeof(szFilename)); _tsplitpath_s(szFilename, Drive, _MAX_DRIVE, Path, _MAX_DIR, Filename, _MAX_FNAME, Ext, _MAX_EXT); TCHAR szFile[MAX_PATH]; // Extract "file.fnt" _stprintf_s(szFile, MAX_PATH, _T("%s%sfile.fnt"), Drive, Path); if( (_waccess( szFile, 0 )) == -1 ) // 파일이 존재하지 않을 경우 { HRSRC hrsrc = FindResource(hModule, (LPCTSTR)IDR_MY_FONT, _T("MyFILE")); DWORD dwSize = SizeofResource(hModule, hrsrc); HGLOBAL hGlobal = LoadResource(hModule, hrsrc); CHAR *pData = (CHAR *) LockResource(hGlobal); CFile file; CFileException e; if (file.Open(szFile, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary, &e)) { file.Write(pData, dwSize); file.Close(); } else { TCHAR szErr[1024]; e.GetErrorMessage(szErr, 1024); AfxMessageBox(szErr); } DeleteObject(hGlobal); } // Extract "file.dat" _stprintf_s(szFile, MAX_PATH, _T("%s%sfile.dat"), Drive, Path); if( (_waccess( szFile, 0 )) == -1 ) // 파일이 존재하지 않을 경우 { HRSRC hrsrc = FindResource(hModule, (LPCTSTR)IDR_MY_DATA, _T("MyFILE")); DWORD dwSize = SizeofResource(hModule, hrsrc); HGLOBAL hGlobal = LoadResource(hModule, hrsrc); CHAR *pData = (CHAR *) LockResource(hGlobal); CFile file; CFileException e; if (file.Open(szFile, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary, &e)) { file.Write(pData, dwSize); file.Close(); } else { TCHAR szErr[1024]; e.GetErrorMessage(szErr, 1024); AfxMessageBox(szErr); } DeleteObject(hGlobal); } 2009/06/11 17:04 2009/06/11 17:04 Tags 리소스 저장 Trackback: 0 Comment: 0 이 글에는 트랙백을 보낼 수 없습니다
Microsoft CryptoAPI 를 이용한 데이터 암호화 복호화 Posted by 빵빵빵 2009/06/11 17:01 전산(컴퓨터)/PC-Windows 전산(컴퓨터)/PC-Windows //------------------------------------------------------------------------ #include <vcl.h> #pragma hdrstop #include "uMain.h" //------------------------------------------------------------------------ #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //------------------------------------------------------------------------ #define ENCRYPT_ALGORITHM CALG_RC4 #define KEYLENGTH 0x00800000 static void EncryptFunc(sqlite3_context *context, int argc, sqlite3_value **argv) { if (argc == 0) sqlite3_result_error(context, "Error: No Data)", -1); else if (argc == 1) sqlite3_result_error(context, "Error: No Password", -1); else if (argc != 2) sqlite3_result_error(context, "Error: Invalid param count", -1); else { const unsigned char *pwd; pwd = (const unsigned char *)sqlite3_value_text(argv[1]); const unsigned char *src; src = (const unsigned char *)sqlite3_value_blob(argv[0]); if ( strlen( pwd ) > 0 ) { HCRYPTPROV hCryptProv = NULL; HCRYPTKEY hKey = NULL; HCRYPTHASH hHash = NULL; // Get the Handle to the default provider if ( !CryptAcquireContext( &hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0 ) ) { if ( GetLastError() == NTE_BAD_KEYSET ) { if (!CryptAcquireContext(&hCryptProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { // CryptAcquireContext() failed. sqlite3_result_error( context, "Error: CryptAcquireContext()", -1); return; } } else { // CryptAcquireContext() failed. sqlite3_result_error( context, "Error: CryptAcquireContext()", -1); return; } } // Create Hash object if ( !CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash ) ) { // Error during CryptCreateHash() sqlite3_result_error( context, "Error: CryptCreateHash()", -1); return; } // Hash the password if ( !CryptHashData( hHash, pwd, strlen(pwd), 0 ) ) { // Error during CryptHashData sqlite3_result_error( context, "Error: CryptHashData()", -1); return; } // Derive a session key from the hash object if ( !CryptDeriveKey( hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey ) ) { // Error during CryptDeriveKey sqlite3_result_error( context, "Error: CryptDeriveKey()", -1); return; } // Encrypt unsigned long length = strlen(src); unsigned char *dst = (unsigned char *)malloc(length + 1); memcpy(dst, src, length + 1 ); if ( !CryptEncrypt( hKey, NULL, TRUE, 0, dst, &length, length ) ) { // Error during CryptEncrypt sqlite3_result_error( context, "Error: CryptEncrypt()", -1); free( dst ); return; } sqlite3_result_blob( context, dst, strlen( dst ), SQLITE_TRANSIENT ); free( dst ); } } } //------------------------------------------------------------------------ static void DecryptFunc(sqlite3_context *context, int argc, sqlite3_value **argv) { if (argc == 0) sqlite3_result_error(context, "Error: No Data)", -1); else if (argc == 1) sqlite3_result_error(context, "Error: No Password", -1); else if (argc != 2) sqlite3_result_error(context, "Error: Invalid param count", -1); else { const unsigned char *src; src = (const unsigned char *)sqlite3_value_blob(argv[0]); const unsigned char *pwd; pwd = (const unsigned char *)sqlite3_value_text(argv[1]); if ( strlen( pwd ) > 0 ) { HCRYPTPROV hCryptProv = NULL; HCRYPTKEY hKey = NULL; HCRYPTHASH hHash = NULL; // Get the Handle to the default provider if ( !CryptAcquireContext( &hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0 ) ) { if ( GetLastError() == NTE_BAD_KEYSET ) { if (!CryptAcquireContext(&hCryptProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { // CryptAcquireContext() failed. sqlite3_result_error( context, "Error: CryptAcquireContext()", -1); return; } } else { // CryptAcquireContext() failed. sqlite3_result_error( context, "Error: CryptAcquireContext())", -1); return; } } // Create Hash object if ( !CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash ) ) { // Error during CryptCreateHash() sqlite3_result_error( context, "Error: CryptCreateHash()", -1); return; } // Hash the password if ( !CryptHashData( hHash, pwd, strlen( pwd ), 0 ) ) { // Error during CryptHashData sqlite3_result_error( context, "Error: CryptHashData()", -1); return; } // Derive a session key from the hash object if ( !CryptDeriveKey( hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey ) ) { // Error during CryptDeriveKey sqlite3_result_error( context, "Error: CryptDeriveKey()", -1); return; } // Decrypt unsigned long length = strlen(src); unsigned char *dst = (unsigned char *)malloc(length + 1); memcpy(dst, src, length + 1 ); if ( !CryptDecrypt( hKey, NULL, TRUE, 0, dst, &length ) ) { // Error during CryptEncrypt sqlite3_result_error( context, "Error: CryptDecrypt()", -1); return; } sqlite3_result_blob( context, dst, strlen( dst ), SQLITE_TRANSIENT ); free( dst ); } } } //------------------------------------------------------------------------ __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { m_pDB = NULL; } //------------------------------------------------------------------------ void __fastcall TForm1::FormDestroy(TObject *Sender) { if ( m_pDB ) sqlite3_close( m_pDB ); } //------------------------------------------------------------------------ void __fastcall TForm1::FormCreate(TObject *Sender) { int nRst = sqlite3_open( "test.db", &m_pDB ); if ( nRst != SQLITE_OK ) { ShowMessage("Cannot open database."); return; } int nResult; nResult = sqlite3_create_function(m_pDB, "Encrypt", -1, SQLITE_UTF8, NULL, &EncryptFunc, NULL, NULL); nResult = sqlite3_create_function(m_pDB, "Decrypt", -1, SQLITE_UTF8, NULL, &DecryptFunc, NULL, NULL); sqlite3_stmt* pStmt = NULL; const char* szChar; String strTemp; String strQuery; strQuery = "SELECT Encrypt('test', '123')"; if ( sqlite3_prepare( m_pDB, strQuery.c_str(), strQuery.Length(), &pStmt, &szChar ) == SQLITE_OK ) { int nRow = sqlite3_data_count( pStmt ); int nCol = sqlite3_column_count( pStmt ); const char* col1name = sqlite3_column_name( pStmt, 0 ); int nRowCnt = 0; if ( sqlite3_step( pStmt ) == SQLITE_ROW ) { ++nRowCnt; const unsigned char* col1 = sqlite3_column_text( pStmt, 0 ); String strMsg; strMsg.sprintf( "ROW %d: %s => %s", nRowCnt, col1name, col1 ); strTemp.sprintf( "%s", col1 ); ShowMessage( strMsg ); } } sqlite3_finalize( pStmt ); strQuery = "SELECT Decrypt('" + strTemp + "', '123')"; if ( sqlite3_prepare( m_pDB, strQuery.c_str(), strQuery.Length(), &pStmt, &szChar ) == SQLITE_OK ) { int nRow = sqlite3_data_count( pStmt ); int nCol = sqlite3_column_count( pStmt ); const char* col1name = sqlite3_column_name( pStmt, 0 ); int nRowCnt = 0; if ( sqlite3_step( pStmt ) == SQLITE_ROW ) { ++nRowCnt; const unsigned char* col1 = sqlite3_column_text( pStmt, 0 ); String strMsg; strMsg.sprintf( "ROW %d: %s => %s", nRowCnt, col1name, col1 ); ShowMessage( strMsg ); } } sqlite3_finalize( pStmt ); } //------------------------------------------------------------------------ 2009/06/11 17:01 2009/06/11 17:01 Tags 복호화, 암호화 Trackback: 0 Comment: 0 이 글에는 트랙백을 보낼 수 없습니다