-2

I'm trying to cipher files using symmetric key. Since I'm not able to o this, I'm doing some tests using the file (.txt) content and ciphering that same content using symmetric key and everything works fine:

filename1 = raw_input("Insert file name: ")
with open(filename1,'rb') as f:
    s = f.read()

data1 = s


# insert password to create key

password1 = raw_input('Enter password (symmetric key): ')

# generate 16bytes (AES-128) key from inserted password
h1 = SHA256.new()
h1.update(password1)
key1 = h1.digest()[0:16]

# generate initialization 16bytes vector
iv1 = Random.new().read(16)

# criptogram creation (cipher data)
cipher1 = AES.new(key1, AES.MODE_CFB, iv1)
criptogram1 = iv1 + cipher1.encrypt(data1) 

But, what I need is to use the symmetric key ciphering to cipher files, not just the content like I'm doing at the moment. I need to be able to choose the file and then use the symmetric key in it.

Edit: For "cipher files, not just the content"? I mean that I can encrypt something inside a .txt file, something written in it, but I want to be able to encrypt 'directly' the file, I don't want to open it and read what is inside and then encrypt that... The example that I post, I'm entering a file name (e.g. xpto.txt) wich has something written inside (e.g. Hello world!), so in the example I'm just encrypting that content.

I want to get a file encrypted with out have to read what is inside of it. Because if I try to encrypt a picture, I'm not going to read inside it like in a .txt file, I want to get the entire file and encrypt it.

Psycho_Mind
  • 27
  • 2
  • 6
  • 3
    What do you mean for "cipher files, not just the content"? Of course encrypting a file means encrypting its contents.. :confused: – redShadow Oct 07 '14 at 10:47
  • 1
    It's not obvious what you want, what you have and how you expect to get there with what you've done. Please **give example input and output** needed and *explain* what you've been trying to do to achieve this. – Veedrac Oct 07 '14 at 10:57
  • Your question doesn't make much sense. You won't read the picture file like a text file, you'd open it in binary mode and read/encrypt blocks of data in it. You can process text files in exactly the same way. Check the documentation for the `open(...)` function, you'll want to pass in the "rb" mode to set it into binary mode. – Brett Lempereur Oct 07 '14 at 12:42
  • @Brett Lempereur, I know I can't read a picture like a text file, that's the point that I'm trying to make. That's why I need to encrypt the 'entire' file and not just the content. Binary mode (or something like that) is problably what I need. I want to be able to encrypt any kind of file, so I need to have something in common. I will thake a look in the open(...) function documentation. Thanks a lot for your reply. – Psycho_Mind Oct 07 '14 at 13:11
  • @Psycho_Mind Which parts of the content of a file do you think you would miss by reading it in chunks? Have you run any experiments? Try implementing your own version of "cp". Check the MD5 hashes of the files afterwards, has it changed? If it has then you're doing it wrong. – Brett Lempereur Oct 07 '14 at 21:48
  • @Brett Lempereur, I've done different experiments, but with no pratical results. The answer that I've posted meanwhile, allows me to do what I want (encryption over any file), but in fact I still have some issues: I can't obtain at the end the same MD5 hash or recover the original file that I've encrypted. – Psycho_Mind Oct 08 '14 at 09:16

2 Answers2

0

If I got it right - You are able to encrypt something in current file, but You don't know how to, let's say, run script from "myfile1.py" which will encrypt lines from "myfile2.txt"?

Well just read lines in the first file from the second file with command like:

with open('myfile2.txt') as myfile:
    mytext = myfile.readlines()

and then do encryption on mytext.

pbialy
  • 1,025
  • 14
  • 26
0

After some research I managed to find a solution:

#read binary file to get bytes 
    while True:
        buf = fo.read(1024) #read 1024bytes from the file on each iteration of the loop 
        if len(buf) == 0:
             break

    fo.close()


    # insert password to create key

    password1 = raw_input('Enter password (symmetric key): ')

    # generate 16bytes (AES-128) key from inserted password
    h1 = SHA256.new()
    h1.update(password1)
    key1 = h1.digest()[0:16]

    # generate initialization 16bytes vector
    iv1 = Random.new().read(16)

    # criptogram creation (cipher data)
    cipher1 = AES.new(key1, AES.MODE_CFB, iv1)
    criptogram1 = iv1 + cipher1.encrypt(buf)
Psycho_Mind
  • 27
  • 2
  • 6