小暴力应用程序需要帮助(C编程)
我正在尝试用C语言开发一个小型暴力应用程序。作为操作系统,我使用Ubuntu 14.04。我的CPU是Intel Core i7-920@2.67GHz(股票) 我的问题是,在运行时,如何在openmp中使用MD5_Init、MD5_Update和MD5_Final而无需特别小心 例如,my main()-函数如下所示:小暴力应用程序需要帮助(C编程),c,openmp,C,Openmp,我正在尝试用C语言开发一个小型暴力应用程序。作为操作系统,我使用Ubuntu 14.04。我的CPU是Intel Core i7-920@2.67GHz(股票) 我的问题是,在运行时,如何在openmp中使用MD5_Init、MD5_Update和MD5_Final而无需特别小心 例如,my main()-函数如下所示: #include <stdio.h> #include <stdlib.h> #include <string.h> #include &l
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#include <openssl/md5.h>
#include <time.h>
const char LiteralsLower[] = "abcdefghijklmnopqrstuvwxyz";
const int NLiteralsLower = sizeof( LiteralsLower ) - 1;
char HexByteHash[16];
time_t StartTime, EndTime;
/* Prototypes */
static void MD5Hash( const char * );
static void BruteForce( const int * restrict, const int * restrict, const char *, const int *, int );
int main()
{
const char HashAsString[] = "172522ec1028ab781d9dfd17eaca4427"; // 'david'
const int *BFOptionSize = &NLiteralsLower;
const char *BFOption = LiteralsLower;
int PasswordLength = 6;
int StrLength = strlen( HashAsString );
for( int i = 0; i < StrLength / 2 ; i++ )
{
if( !sscanf( HashAsString + 2 * i, "%02x", ( unsigned int * ) &HexByteHash[i] ) )
{
fprintf( stderr, "FEHLER!!! -> '%s' ist kein Hash-Wert!\n", HashAsString );
exit( EXIT_FAILURE );
}
}
fprintf( stdout, "Hash: %s mit einer Länge von %li Zeichen.\n", HashAsString, strlen( HashAsString ) / 2 );
/* Timer starten */
time( &StartTime );
#pragma omp parallel shared( PasswordLength, BFOptionSize )
for( int PWLength = 0; PWLength < PasswordLength + 1; ++PWLength )
{
/* Array-Größe für FstEntry und LstEntry festlegen */
int FstEntry[PWLength], LstEntry[PWLength];
/* Array-Felder mit 0 initialisieren */
for( int j = 0; j < PWLength; ++j )
FstEntry[j] = LstEntry[j] = 0;
#pragma omp for schedule( dynamic )
for( int i = 0; i < *BFOptionSize; ++i )
{
FstEntry[0] = i;
LstEntry[0] = i + 1;
BruteForce( FstEntry, LstEntry, BFOption, BFOptionSize, PWLength );
}
}
/* Timer stoppen */
time( &EndTime );
puts( "!!! Wort nicht gefunden !!!" );
printf( "Elapsed time: %ld minutes %ld seconds\n\n", ( EndTime - StartTime ) / 60, ( EndTime - StartTime ) % 60 );
return EXIT_FAILURE;
}
问题是,当我使用全部八个内核时,有时如果启动应用程序,它找不到要搜索的单词。
例如:172522ec1028ab781d9dfd17eaca4427我不擅长openmp,但通过打印一些调试信息,我发现生成的密码末尾没有“\0”,它们包含如下字符:abrv▒♣▒
,要解决这个问题,只需添加密码[PSSWDLength]='\0'代码>在第105行
编译使用:gcc my_md5.c-o my_md5-fopenmp-lcrypto
使用Cygwin的Windows7操作系统
将restrict替换为\uu restrict
使用密码azor而不是david
#包括
#包括
#包括
#包括
#包括
#包括
const char LiteralsLower[]=“abcdefghijklmnopqrstuvxyz”;
常量int NLiteralsLower=sizeof(literalsower)-1;
char-HexByteHash[16];
时间-开始时间,结束时间;
/*原型*/
静态void MD5Hash(const char*);
静态void BruteForce(常数int*\uuu restrict,常数int*\uu restrict,常数char*,常数int*,int);
int omp_get_num_线程(void);
int main()
{
//常量字符哈希字符串[]=“172522ec1028ab781d9dfd17eaca4427”/“david”
常量字符哈希字符串[]=“f68d28e078cc6aa6c163f787da7572eb”/“azor”
常量int*bOptionSize=&nLiteralSleer;
const char*BFOption=LiteralsLower;
int PasswordLength=6;
int StrLength=strlen(哈希字符串);
int i;
对于(i=0;i'%s'是kein Hash Wert!\n”,hashastring);
退出(退出失败);
}
}
fprintf(标准值,“哈希值:%s在%li Zeichen的表中。\n”,哈希字符串,标准值(哈希字符串)/2);
/*计时器开始*/
时间(和开始时间);
整数长度;
#pragma omp并行共享(密码长度,bOptionSize)
{
对于(PWLength=0;PWLength
你好,亲爱的Houssam,非常感谢。你救了我一天。你是我的英雄。:-)现在它正常工作了。密码[PSSWDLength]='\0'@你应该接受答案,这是当地表达感谢的方式。第一个问题很好。写得很好,你提供了关于这个问题的想法和尝试。下次一定要为我们这些看不懂的人翻译德语评论(比如我:()
static void BruteForce( const int * restrict FirstEntry, const int * restrict LastEntry, const char *Letters, const int *NLetters, int PSSWDLength )
{
char Password[PSSWDLength];
int Entry[PSSWDLength + 1];
int i, j;
/* Null-Byte hinzufügen */
memset( Entry, '\0', PSSWDLength );
memset( Password, '\0', PSSWDLength );
/* FirstEntry in Entry kopieren */
for( i = 0; i < PSSWDLength; ++i )
Entry[i] = FirstEntry[i];
i = 0;
while( i < PSSWDLength )
{
/* Generiere Passwort für Hash-Vergleich */
for( i = 0; i < PSSWDLength; ++i )
Password[i] = Letters[Entry[i]];
/* generiertes Wort hashen */
MD5Hash( Password );
/* Entry inkrementieren */
for( i = 0; i < PSSWDLength && ++Entry[PSSWDLength-i-1] == *NLetters; i++ )
Entry[PSSWDLength-i-1] = 0;
/* Return wenn Entry != LastEntry raus aus der Schleife */
for( j = 0; j < PSSWDLength; ++j )
if( Entry[j] != LastEntry[j] )
break;
/* wenn Entry == LastEntry Funktion verlassen */
if( j == PSSWDLength )
return;
}
}
static void MD5Hash( const char *PasswordStringPointer )
{
unsigned char Digest[16];
/* MD5-Hash erzeugen */
MD5_CTX md5;
MD5_Init( &md5 );
MD5_Update( &md5, PasswordStringPointer, strlen( PasswordStringPointer ) );
MD5_Final( Digest, &md5 );
if( memcmp( HexByteHash, Digest, 16 ) == 0 )
{
printf( "!!! Wort gefunden: '%s' !!!\n", PasswordStringPointer );
/* Timer stoppen */
time( &EndTime );
printf( "Elapsed time: %ld minutes %ld seconds\n\n", ( EndTime - StartTime ) / 60, ( EndTime - StartTime ) % 60 );
/* Passwortsuche war erfolgreich */
exit( EXIT_SUCCESS);
}
}
Hash: 172522ec1028ab781d9dfd17eaca4427 mit einer Länge von 16 Zeichen.
!!! Wort gefunden: 'david' !!!
Elapsed time: 0 minutes 1 seconds
Hash: 172522ec1028ab781d9dfd17eaca4427 mit einer Länge von 16 Zeichen.
!!! Wort nicht gefunden !!!
Elapsed time: 0 minutes 14 seconds
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#include <openssl/md5.h>
#include <time.h>
const char LiteralsLower[] = "abcdefghijklmnopqrstuvwxyz";
const int NLiteralsLower = sizeof( LiteralsLower ) - 1;
char HexByteHash[16];
time_t StartTime, EndTime;
/* Prototypes */
static void MD5Hash( const char * );
static void BruteForce( const int * __restrict , const int * __restrict , const char *, const int *, int );
int omp_get_num_threads(void);
int main()
{
//const char HashAsString[] = "172522ec1028ab781d9dfd17eaca4427"; // 'david'
const char HashAsString[] = "f68d28e078cc6aa6c163f787da7572eb"; // 'azor'
const int *BFOptionSize = &NLiteralsLower;
const char *BFOption = LiteralsLower;
int PasswordLength = 6;
int StrLength = strlen( HashAsString );
int i;
for(i = 0; i < StrLength / 2 ; i++ )
{
if( !sscanf( HashAsString + 2 * i, "%02x", ( unsigned int * ) &HexByteHash[i] ) )
{
fprintf( stderr, "FEHLER!!! -> '%s' ist kein Hash-Wert!\n", HashAsString );
exit( EXIT_FAILURE );
}
}
fprintf( stdout, "Hash: %s mit einer L?nge von %li Zeichen.\n", HashAsString, strlen( HashAsString ) / 2 );
/* Timer starten */
time( &StartTime );
int PWLength;
#pragma omp parallel shared( PasswordLength, BFOptionSize )
{
for(PWLength = 0; PWLength < PasswordLength + 1; ++PWLength )
{
int nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
/* Array-Gr??e für FstEntry und LstEntry festlegen */
int FstEntry[PWLength], LstEntry[PWLength];
/* Array-Felder mit 0 initialisieren */
int j ;
for( j= 0; j < PWLength; ++j )
FstEntry[j] = LstEntry[j] = 0;
int i ;
#pragma omp for schedule( dynamic )
for( i= 0; i < *BFOptionSize; ++i )
{
FstEntry[0] = i;
LstEntry[0] = i + 1;
BruteForce( FstEntry, LstEntry, BFOption, BFOptionSize, PWLength );
}
}
}
/* Timer stoppen */
time( &EndTime );
puts( "!!! Wort nicht gefunden !!!" );
printf( "Elapsed time: %ld minutes %ld seconds\n\n", ( EndTime - StartTime ) / 60, ( EndTime - StartTime ) % 60 );
return EXIT_FAILURE;
}
static void BruteForce( const int * __restrict FirstEntry, const int * __restrict LastEntry, const char *Letters, const int *NLetters, int PSSWDLength )
{
char Password[PSSWDLength];
int Entry[PSSWDLength + 1];
int i, j;
/* Null-Byte hinzufügen */
memset( Entry, '\0', PSSWDLength );
memset( Password, '\0', PSSWDLength );
/* FirstEntry in Entry kopieren */
for( i = 0; i < PSSWDLength; ++i )
Entry[i] = FirstEntry[i];
i = 0;
while( i < PSSWDLength )
{
/* Generiere Passwort für Hash-Vergleich */
for( i = 0; i < PSSWDLength; ++i )
Password[i] = Letters[Entry[i]];
//houssam
Password[PSSWDLength] = '\0';
printf("%s length = %d %d \n ", Password , PSSWDLength , strlen(Password));
/* generiertes Wort hashen */
MD5Hash( Password );
/* Entry inkrementieren */
for( i = 0; i < PSSWDLength && ++Entry[PSSWDLength-i-1] == *NLetters; i++ )
Entry[PSSWDLength-i-1] = 0;
/* Return wenn Entry != LastEntry raus aus der Schleife */
for( j = 0; j < PSSWDLength; ++j )
if( Entry[j] != LastEntry[j] )
break;
/* wenn Entry == LastEntry Funktion verlassen */
if( j == PSSWDLength )
return;
}
}
static void MD5Hash( const char *PasswordStringPointer )
{
unsigned char Digest[16];
/* MD5-Hash erzeugen */
MD5_CTX md5;
MD5_Init( &md5 );
MD5_Update( &md5, PasswordStringPointer, strlen( PasswordStringPointer ) );
MD5_Final( Digest, &md5 );
if( memcmp( HexByteHash, Digest, 16 ) == 0 )
{
printf( "!!! Wort gefunden: '%s' !!!\n", PasswordStringPointer );
/* Timer stoppen */
time( &EndTime );
printf( "Elapsed time: %ld minutes %ld seconds\n\n", ( EndTime - StartTime ) / 60, ( EndTime - StartTime ) % 60 );
/* Passwortsuche war erfolgreich */
exit( EXIT_SUCCESS);
}
}