【问题标题】:C program works from the command line but not from Python script?C 程序从命令行工作,但不能从 Python 脚本工作?
【发布时间】:2014-06-12 01:25:35
【问题描述】:

更新了整个 python 脚本。

好的,所以我正在 Coursera.org 上学习机器学习课程,我想看看这些类型的算法中的一些可以用加密技术做什么,并测试看看是否有可能用神经网络破解加密。网络。首先,我需要为训练集创建一个哈希表,但是我遇到了 C 字符串数组、args 和 Python 将字符串作为 args 传递给 C 程序的问题。

这是我的名为 hashpipe 的 lil C 程序

    #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 脚本……请注意,我不确定我是否正确使用了 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-line 上执行它应该执行的操作。

【问题讨论】:

  • 请提供示例输入以使您的 Python 示例可重现。

标签: c string python-2.7 hash machine-learning


【解决方案1】:

DOH! 我需要在我的 C 程序中返回 0。它现在工作只是为了让你知道。创建我的第一个哈希表:)

【讨论】:

    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 2013-08-31
    • 2015-04-12
    • 1970-01-01
    • 2021-05-02
    • 2018-05-04
    • 2015-11-15
    • 2011-03-02
    相关资源
    最近更新 更多