有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从java转换而来的定时攻击C

我的第一篇帖子。:)

在过去的几周里,我用java为该方法编写了一个定时攻击实现:

static char[] a = new char[] { 's', 'e', 'c', 'r', 'e', 't', '1' };

static boolean passwordCompare(char[] a, char[] b){
    int i;
    if (a.length != b.length)
        return false;
    for (i = 0; i < a.length && a[i] == b[i]; i++);
         return i == a.length;}

一切如期进行。但后来我想用C编写同样的攻击,因为这是我目前正在努力学习的编程语言,没有什么是正确的

我将感激每一点帮助。:)

密码比较。o是lib,其中方法password_compare(const char*password) 而秘密密码是假的

干杯

#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "password_compare.o"

/* foreach macro for using a string as a collection of char */
#define foreach( ptrvar, strvar ) char* ptrvar; for( ptrvar=strvar ; 
        (*ptrvar) != '\0' ; *ptrvar++)


int main(void)
{
  // variables
   int counter;
   char[] test;
   long begin, end, begin2, end2;
   long duration;
   long duration2;
   long store = 0;
   char [] storechar;
   char[] tester;
   char [] storechar2;

 // Try to find out the length from the password. 
 // Longest time = password.length.
   for(int i=0; i<=10; ++i)
   {
      test = new char[i];
        for(int j=0; j<i; ++j)
         {
          test[j]=a;
         }

      const char [] fixtest = test;
      time_t start = time(0);
        for (int k=0; k<1000000000;k++)
         {

         password_compare(fixtest);
         }

      time_t end = time(0);
      duration = end-start;
        if(duration > store)
          {
            store = duration;
            storechar = test;
            counter = i; 
          }


        printf("Duration (" + i + ") = " + duration);
  }

  printf("The right length is :" + storechar.length);

  int len=sizeof(storechar)/sizeof(int);
  storechar2 = new char[len];
  tester = new char[counter];

  // Iterate through each position from the password array.
  for (int h=0; h<len; h++)
     {
        long store2 = 0;

        // Generate an char[] as String 
        // with all possible characters.
        char* s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc
                    defghijklmnopqrstuvwxyz1234567890";

        // Check for each character from s1 ... 

         foreach(p1, s1)
          {
             tester[h] = *p1;
             const char [] fixtester = tester;

             // Check for each p1 in s1 at Position h the time. 
             //The largest value will be saved at Pos h.  

              if (h < (len-1))
              {

                  time_t start2  = time();
              for ( int k = 0; k<1000000000;k++)
              {
                 password_compare(fixtester);
              }
              for ( int p = 0; p<1000000000;p++)
              {
                 password_compare(fixtester);
              }

              time_t end2 = time();
              duration2 = end2-begin2;
                if ( duration2 > store2)
                 {
                    store2 = duration2;
                storechar2[h] = *p1;
                 }
             printf("Duration (" + *p1 + ") = " + duration2);
              }

            // else if h == password.length, then try 
            // every character at the last position.

            else if (password_compare(fixtester) == true)
                {
                     storechar2[h] = *p1;
                     printf("Character at Pos " + (h + 1) + ": ")
                     printf("storechar2[%d]: %d\n",h,storechar2[h]);
                     printf("The Password is: ");

                         for(int b = 0; i < my_array.length; i++) 
                            {printf("%d ",storechar2[b]);}


                     exit(0);

               }
          }

       printf("Character at Pos " + (h + 1) + ": ");
       printf("storechar2[%d]: %d\n",h,storechar2[h]);
       tester[h] = storechar2[h];
    }

      // The Password was guessed wrong!
       printf("Failed");
       exit(0);
       return(0);

}


共 (1) 个答案

  1. # 1 楼答案

    存在多个语法错误,例如:

    #define foreach( ptrvar, strvar ) char* ptrvar; for( ptrvar=strvar ; 
        (*ptrvar) != '\0' ; *ptrvar++)
    

    只有第一行是宏的一部分。要使用多行,请在末尾添加反斜杠

    char [] storechar;
    storechar2 = new char[len];
    

    这个数组语法来自Java。检查如何在C中定义数组

    #include "password_compare.o"
    

    这会使文件充满垃圾。您希望包含头文件,而不是对象文件

    如果你不能解决问题,那么从一个新文件开始,一次添加一个文件。在添加任何其他内容之前,请检查它是否已编译。如果有错误,请记下确切的消息和导致错误的行