0

I am using M2Crypto in python with the following code:

import M2Crypto;
def encrypt_file(key, in_filename, out_filename,iv):
    cipher=M2Crypto.EVP.Cipher('aes_256_cfb',key,iv, op=1)
    with open(in_filename, 'rb') as infile:
        with open(out_filename, 'wb') as outfile:
          while True:
            buf = infile.read(1024)
            if not buf:
                break
            print buf+"."
            outfile.write(cipher.update(buf))

          outfile.write( cipher.final() )  
          outfile.close()
        infile.close()

def decrypt_file(key, in_filename, out_filename,iv):
    cipher = M2Crypto.EVP.Cipher("aes_256_cfb",key , iv, op = 0)
    with open(in_filename, 'rb') as infile: 
        with open(out_filename, 'wb') as outfile:
          while True:
            buf = infile.read(1024)
            if not buf:
                break
            try:
                outfile.write(cipher.update(buf))
            except:
                print "here"
            outfile.write(cipher.final())
          outfile.close()
        infile.close()

encrypt_file("1234", "D:\\in.txt", "D:\\out.txt", "00")
decrypt_file("1234", "D:\\out.txt", "D:\\dec.txt", "00")

The problem is that I don't get in.txt content in the dec.txt file. Am I not using this right?

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
AlexandruC
  • 3,527
  • 6
  • 51
  • 80
  • Please tag questions about [tag:cryptography] or [tag:encryption] with either one of those tags, otherwise they may go unnoticed... – Maarten Bodewes Apr 13 '14 at 00:56
  • 3
    You don't have to explicitly `close` files if you are using `with` as a context manager. Also, your problem description is basically "I wrote this 50-line program and it doesn't do what I expect -- thoughts?" What is actually happening when you run this? – Two-Bit Alchemist Apr 13 '14 at 00:57

0 Answers0