AES密码进程创建

时间:2016-11-13 15:20:59

标签: python encryption process aes pycrypto

我有一个程序密码和解密一个文件,一个名为a1的文件,转换成一个名为a1.enc的文件,它运行正常,我的问题是-p选项,它应该定义数字进程用来制作密码,如果我使用-p 20,它会比我使用-p 10使用更多,并且默认情况下它必须使用1个进程,我得到代码,直到-p选项正常工作,但我失踪了这个要点,谢谢

import sys
import os, random, time
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from optparse import OptionParser

def pad(bytestring):
    nbytes = len(bytestring)
    padding_bytes = AES.block_size - nbytes
    padding_str = padding_bytes * chr(padding_bytes)
    return bytestring + padding_str

def cipher(filename):
    key = "abcdefghijklmnopqrstuwvxyz012345"
    cipher = AES.new(key)
    with open(filename) as ifile, open(filename + ".enc", "w") as ofile:
        bytestring = ifile.read(AES.block_size)
        while len(bytestring) == AES.block_size:
            ofile.write(cipher.encrypt(pad(bytestring)))
            bytestring = ifile.read(AES.block_size)
    ofile.write(cipher.encrypt(pad(bytestring)))

def decipher(filename):
    key = "abcdefghijklmnopqrstuwvxyz012345"
    cipher = AES.new(key)
    with open(filename + ".enc") as ifile, open(filename + ".dec", "w") as ofile:
        previous = None
        current = None
        NOT_DONE = True
        while NOT_DONE:
            bytestring = cipher.decrypt(ifile.read(AES.block_size))
            if current == None:
                current = bytestring
            else:
                previous = current
            current = bytestring
            nbytes = len(current)
            if nbytes == 0:
                padding_bytes = ord(previous[-1])
                previous = previous[:-padding_bytes]
                NOT_DONE = False
            ofile.write(previous)

def main():
    parser = OptionParser(usage="usage: %prog -e|-d [-p n] [-v] {files}",
                          version="%prog 1.0")
    parser.add_option("-e", "--encrypt",
            action="store_true",
            dest="encrypting",
            default=False,
            help="ecrypt file")
    parser.add_option("-d", "--decrypt",
            action="store_true",
                        dest="decrypting",
                        default="False",
                        help="Decrypt file")
    parser.add_option("-p", "--processes",
                        action="store",
                        dest="nprocessesthreads",
                type='int', 
                        help="Number processes/threads to create")
    parser.add_option("-v", "--existes",
                        action="store_true",
                        dest="existfile",
                        default="False",
                        help="No file exists the question command continues")
        (options, args) = parser.parse_args()

    inputFilename = args

    if options.encrypting:
        for j in range(len(args)):
            cipher(args[j])

    elif options.decrypting:
        for i in range(len(args)):
            decipher(args[i])

    #elif options.nprocessesthreads > 1:
'''

#Option -p      
#process creation

        i=0
        while i <= options.nprocessesthreads: 


            pid = os.fork()
            if pid == 0:
                "operates"
                "ends"
            else:
                i+=1

''' 
    #elif options.existsfile:  
'''

#Opçao -p       
#If file does not exist option to exit or continue  

        if os.path.exists(args):
            print('This will overwrite the file %s. (C)ontinue or (Q)uit?' % (args))
            response = input('> ')
        if not response.lower().startswith('c'):
            sys.exit()

'''
if __name__ == '__main__':
    main()

它应该输出这样的东西:

so000@kali:~/so$ python pcrypt.py -e exemple

so000@kali:~/so$ ls 

a1    a2    a4    exemple      exemple.enc   exemple.key   pcrypt.py 

so000@kali:~/so$ python pcrypt.py -e -v a1 a3 a4 

O file a3 doesnt exist. Continue (s/n) ? 

n 

so000@kali:~/so$ ls 

a1    a1.enc      a1.key      a2    a4    exemple      exemplo.enc
   exemple.key 

pcrypt.py 

0 个答案:

没有答案