M2Crypto加密/解密

时间:2014-03-13 11:42:05

标签: python encryption m2crypto

我在python中使用M2Crypto,代码如下:

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")

问题是我在in.txt文件中没有获得dec.txt个内容。我没有使用这个吗?

0 个答案:

没有答案