2014-04-26 111 views
0

用整個python腳本更新。C程序從命令行工作,但不是從Python腳本?

好的,所以我在Coursera.org上學習機器學習課程,我想看看這些算法類型中的哪些類型可以用加密技術來做,並測試是否有可能破解加密。一個神經網絡。首先,我需要爲訓練集創建一個哈希表,但我遇到了C字符串數組,args和Python傳遞字符串作爲參數到C程序的問題。

這是我叫hashpipe

#include <stdio.h> 
    #define __USE_GNU 
    #include <crypt.h> 
    #include <string.h> 

    int main(int argc, char** argv){ 
      char * salt = argv[2]; 
      struct crypt_data data; 
      data.initialized = 0; 
      char * result = crypt_r(argv[1], id, &data); 
      printf("%s\n", result); 
      return 0 // This was the bugger!!! I forgot it!!! 
    } 

以及調用這個小程序的Python腳本律C程序...請注意,我不知道我是否正確地使用exit

#!/usr/bin/env python2.7 

    import sys, random, subprocess 

    pathToWL = str(sys.argv[1]) #Path to WordList 
    pathForHT = str(sys.argv[2]) #Path to create Hash Table; no, its not smokable 
    mId = str(sys.argv[3]) #id for use with crypt_r() see 'man 3 crypt_r' 

    SaltCharSet = str("a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9") 
    SaltCharSet = SaltCharSet.split(" ") 

    try: 
     fdWL = open(pathToWL, 'r') 
    except: 
     print "Could not open wordlist file." 
     exit() 

    try: 
     fdHT = open(pathForHT, 'a') 
    except: 
     print "Could not open file for hash table" 
     fdWL.close() 
     exit() 

    #We have our wordlist now step through the thing and start generating hashes. 

    toStop = False 
    cursor = 0 #Use the cursor later once this program evolves 

    while(not toStop): 
     try: 
      ln = str(fdWL.readline()) 
     except: 
      toStop = True 
      continue 
     ln = ln.strip("\n") 
     if len(ln) < 6: 
      continue 
     # create random salts 
     # send ln, id, and salts to hashpipe 
     salt = [] 
     lenOfSalt = random.randint(6,16) 
     while(len(salt) < lenOfSalt + 1): 
      aORn = random.randint(0,1) 
      if aORn == 0:# Its a letter 
       uORl = random.randint(0,1) 
       if uORl == 0: 
        salt.append(SaltCharSet[(random.randint(0,25))].upper()) 
       elif uORl == 1: 
        salt.append(SaltCharSet[(random.randint(0,25))].lower()) 
       else: 
        print "Random Int 'uORl' out of bounds" 
        fdHT.close() 
        fdWL.close() 
        toStop = True 
        exit() # I don't know what happened 
        break #in case exit() fails or is used incorrectly 

      elif aORn == 1:# Its a number 
       salt.append(SaltCharSet[(random.randint(26, 35))]) 
      else: 
       print "Random Int 'aORn' out of bounds" 
       fdHT.close() 
       fdWL.close() 
       toStop = True 
       exit() # I don't know what happened 
       break #in case exit() fails or is used incorrectly 
     #Generated Salt 
     salt = "".join(salt) 
     wholeArg2 = str("$"+mId+"$"+salt) 
     try: 
      mHash = str(subprocess.check_output(["hashpipe", ln, wholeArg2])) 
     except: 
      print " error getting hash" 
      #Clean-up 
      fdHT.close() 
      fdWL.close() 
      toStop = True 
      exit() 
      break 
     #Generated hash, now write it to the fdHT file 
     print str(ln+" "+wholeArg2+"\t"+mHash) 
     fdHT.write(str(ln+"\t"+mHash+"\n")) 
     cursor = fdWL.tell() 

    fdHT.close() 
    fdWL.close() 

    return 0 #Yes, I forgot it here too, probably why my script never ended! LOL!!! so simple!!! 

我一直在重新改變這部分,沒有什麼作品,最新的交易?它可以在命令行上運行,但不能從python運行。例如,strip('\0')str(r"$")是我最近做出的一些修改,但仍然無效。也許我會寫一個bash腳本來代替...

請注意,編譯後的C程序在cmd行上應該做它應該做的事情。

+0

請給出示例輸入,使您的Python示例可重現。 – merlin2011

回答

1

DOH! 我需要在我的C程序中返回0。它現在的工作只是爲了讓你知道。創建我的第一個哈希表的種類:)

相關問題