2016-10-28 4 views
0

AES (128 비트) CBC 모드에서 파일을 암호화 (암호 해독) 할 수있는 작은 조각의 코드를 발견했습니다. 해독에도 완벽하게 작동하므로 OpenSSL은 (물론) 내 파일의 암호를 해독 할 수는 있지만 불가 능해 보입니다. 나는파이썬과 OpenSSL : 암호 해독 할 수 없습니다.

import os, random, struct 
from Crypto.Cipher import AES 

def encrypt_file(key, in_filename, out_filename=None, chunksize=64*1024): 
    """ Encrypts a file using AES (CBC mode) with the 
     given key. 

     key: 
      16, 24 or 32 bytes long 

     in_filename: 
      Name of the input file 

     out_filename: 
      If None, '<in_filename>.enc' will be used. 

     chunksize: 
      Sets the size of the chunk which the function 
      uses to read and encrypt the file. 
      Chunksize must be divisible by 16. 
     """ 
    if not out_filename: 
      out_filename = in_filename + '.enc' 

    iv = ''.join(chr(random.randint(0, 0xFF)) for i in range(16)) 
    encryptor = AES.new(key, AES.MODE_CBC, iv) 
    filesize = os.path.getsize(in_filename) 

    with open(in_filename, 'rb') as infile: 
     with open(out_filename, 'wb') as outfile: 
      outfile.write(struct.pack('<Q', filesize)) 
      outfile.write(iv) 

      while True: 
       chunk = infile.read(chunksize) 
       if len(chunk) == 0: 
        break 
       elif len(chunk) % 16 != 0: 
        chunk += ' ' * (16 - len(chunk) % 16) 

       outfile.write(encryptor.encrypt(chunk)) 

오류 "읽기 오류 입력 파일"같은 지금 시간이 얻을 : "오류 읽기 입력 파일". 어떻게 가능할까요? 내가 사용하는 명령은 다음과 같습니다.

openssl aes-128-cbc -d -in test_enc.txt -out test_dec.txt 

왜 작동하지 않습니까?

답변

0

OpenSSL은 자체 파일 형식을 사용합니다. 특히, Salted__ 헤더뿐만 아니라 IV를 유도하는 데 사용되는 소금 (즉, IV가 암호화 된 데이터와 직접 저장되지 않음)이 있습니다.

https://scottlinux.com/2013/10/13/how-to-encrypt-a-file-with-openssl/https://crypto.stackexchange.com/questions/3298/is-there-a-standard-for-openssl-interoperable-aes-encryption에서 몇 가지 힌트를 찾을 수 있지만 실제 형식은 설명하지 않습니다.

OpenSSL 코드를 조사해 볼 수는 있지만, OpenSSL이 자체 파일 형식을 사용한다는 결론이 듭니다. OpenSSL이 파일을 해독하도록하려면 OpenSSL을 사용해야합니다.

+0

어 감사합니다. 이제 기본 형식이 달랐습니다. 정말 고맙습니다 –

관련 문제