2015-01-16 6 views
8

साथ बीजलेख मैं pycrypto के साथ एक सार्वजनिक और निजी कुंजी उत्पन्न है, और मैं निर्यात कुंजी का उपयोग कर एक फाइल करने के लिए उन्हें बचाने:pycrypto - गलत लंबाई

from Crypto.PublicKey import RSA 
bits=2048 
new_key = RSA.generate(bits, e=65537) 

prv = open('keymac.pem','w') 
prv.write(new_key.exportKey('PEM')) 
prv.close() 
pub = open('pubmac.pem', 'w') 
pub.write(new_key.publickey().exportKey('PEM')) 
pub.close() 

मैं सार्वजनिक कुंजी का उपयोग (निम्नलिखित एक फ़ाइल को एन्क्रिप्ट करने http://insiderattack.blogspot.com/2014/07/encrypted-file-transfer-utility-in.html#comment-form)

जब मैं फ़ाइल को डिक्रिप्ट करने के लिए फ़ाइल पढ़ता हूं, तो मुझे "गलत लंबाई के साथ सिफरटेक्स्ट" मिलता है।

try: 
    encryptedonetimekey = filetodecrypt.read(512) 
    privatekey = open("keymac.pem", 'r').read() 
    rsaofprivatekey = RSA.importKey(privatekey) 
    pkcs1ofprivatekey = PKCS1_OAEP.new(rsaofprivatekey) 
    aesonetimekey = pkcs1ofprivatekey.decrypt(encryptedonetimekey) 

    except Exception as decrypprivkeyerr: 
     print "Decryption of the one time key using the private key  failed!!" 
     print "Key error == %s" %decrypprivkeyerr 
    raise Exception("Decryption using Private key failed error = %s" %decrypprivkeyerr) 

मैं कुछ याद आ रही हूँ:

मैं दीपल Jayasekara उदाहरण पर डिक्रिप्शन कोड चारों ओर ब्लॉक को छोड़कर कोशिश एक जोड़ा? क्या मुझे निजी कुंजी को अलग से बचाया जाना चाहिए? क्या मैं निजी कुंजी सही ढंग से नहीं पढ़ रहा हूं?

+0

आप इसे पढ़ रहे हैं के रूप में बाइट्स खोलने के लिए याद इसे 'बी' के साथ, अन्यथा स्ट्रिंग स्ट्रिंग का उपयोग करने से पहले ट्रिम करने का प्रयास करें। –

उत्तर

0

यह आपके प्रश्न का सीधे उत्तर नहीं देता है लेकिन यह आपको समस्या के कुछ संकेत दे सकता है। मैं फ़ाइल को एन्क्रिप्ट करने के बजाय फ़ाइल में सामग्री एन्क्रिप्ट करने के लिए दो फ़ंक्शंस का उपयोग कर रहा हूं। एन्क्रिप्ट करने के लिए एक (मेरे मामले में उपयोगकर्ता नाम और पासवर्ड) फ़ाइल में एक और फिर उस डेटा को आवश्यकतानुसार उपयोग करने के लिए डिक्रिप्ट करने के लिए।

नोट गद्दी के लिए की जरूरत

फ़ाइल में बनाएं एन्क्रिप्टेड सामग्री:

from Crypto.Cipher import AES 
import base64 
import os 
import argparse 

parser = argparse.ArgumentParser(description='Arguments used to generate new credentials file, Use: -u for username, -p for password') 
parser.add_argument('-u', help='Specify username', required=True) 
parser.add_argument('-p', help='Specify password', required=True) 
parser.add_argument('-b', help='Specify debug', required=False, action='store_true') 
args = vars(parser.parse_args()) 


def encrypt(username, password): 
    #Encrypt Credentials To '.creds' file, including 'secret' for username and password 
    dir_path = os.path.dirname(os.path.realpath(__file__)) 
    # the block size for the cipher object; must be 16 per FIPS-197 
    BLOCK_SIZE = 16 

    # the character used for padding--with a block cipher such as AES, the value 
    # you encrypt must be a multiple of BLOCK_SIZE in length. This character is 
    # used to ensure that your value is always a multiple of BLOCK_SIZE 
    PADDING = '{' 

    # one-liner to sufficiently pad the text to be encrypted 
    pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING 

    # generate a random secret key 
    user_secret = os.urandom(BLOCK_SIZE) 
    pass_secret = os.urandom(BLOCK_SIZE) 

    # one-liners to encrypt/encode and decrypt/decode a string 
    # encrypt with AES, encode with base64 

    EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))) 
    # create a cipher object using the random secret 
    user_cipher = AES.new(user_secret) 
    pass_cipher = AES.new(pass_secret) 

    # encode a string 
    user_encoded = EncodeAES(user_cipher, username) 
    pass_encoded = EncodeAES(pass_cipher, password) 
    try: 
     with open('.creds', 'w') as filename: 
      filename.write(user_encoded + '\n') 
      filename.write(user_secret + '\n') 
      filename.write(pass_encoded + '\n') 
      filename.write(pass_secret + '\n') 
      filename.close() 
      print '\nFile Written To: ', dir_path + '/.creds' 
    except Exception, e: 
     print e 

    if args['b']: 
     print((user_encoded, user_secret), (pass_encoded, pass_secret)) 

username = args['u'] 
password = args['p'] 

encrypt(username, password) 

डिक्रिप्ट डाटा

def decrypt(dir_path, filename): 
    #Read '.creds' file and return unencrypted credentials (user_decoded, pass_decoded) 

    lines = [line.rstrip('\n') for line in open(dir_path + filename)] 

    user_encoded = lines[0] 
    user_secret = lines[1] 
    pass_encoded = lines[2] 
    pass_secret = lines[3] 

    # the character used for padding--with a block cipher such as AES, the value 
    # you encrypt must be a multiple of BLOCK_SIZE in length. This character is 
    # used to ensure that your value is always a multiple of BLOCK_SIZE 
    PADDING = '{' 

    DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING) 

    # create a cipher object using the random secret 
    user_cipher = AES.new(user_secret) 
    pass_cipher = AES.new(pass_secret) 

    # decode the encoded string 
    user_decoded = DecodeAES(user_cipher, user_encoded) 
    pass_decoded = DecodeAES(pass_cipher, pass_encoded) 

    return (user_decoded, pass_decoded) 
संबंधित मुद्दे