C 程序可以在命令行中运行,但不能在 Python 脚本中运行?

标签 c string python-2.7 hash machine-learning

更新了整个 python 脚本。

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

这是我的小 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 脚本...请注意,我不确定是否正确使用了 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 程序在命令行上执行其应执行的操作。

最佳答案

卫生部! 我需要在我的 C 程序中返回 0。它现在正在运作只是为了让您知道。创建我的第一个哈希表:)

关于C 程序可以在命令行中运行,但不能在 Python 脚本中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23308221/

相关文章:

c - 套接字编程

string - 如何在 NSIS 中连接 2 个字符串

python - 为什么我的值字典需要浅拷贝才能正确更新?

bash - 在 shell 脚本中处理 python 返回的退出代码

python - 动态规划 - 节省计算时间

c - 在 C 中交换一维数组的列/行

c++ - linux中uint32_t从4字节变为6字节

c - char 指针类型的 C 函数有问题,有人可以解释一下吗?

python-3.x - 如何从 int(hashlib.sha256(string.encode()).hexdigest) 获取字符串输出?

ios - 获取不超过两个字母的日历工作日符号