Previously we looked at creating keys, importing public and private keys and the overall setup of gnupg with python. This time around, we're going to take a look at file encryption. Overall the file encryption process is fairly general/easy. But it lacks in the area of scaleablilty, ie to start, you'll only be encrypting one file at a time, which can be done outside of Python with ease. The idea of going over everything in Python, is that you can setup a script to encrypt multiple files in a folder (look for that in Part 3).
Assumptions; you have python, and python-gnupg installed, and a public key from someone you want to encrypt and send files to imported to your keystore home (see Part 1 for more information here.
Let's get started with Python file encryption. Start off by getting into your python shell, and enter the following:
>>> import os
>>> import gnupg
>>> gpg_home = "/path/to/keyfile/.gnupg"
>>> gpg = gnupg.GPG(gnupghome=gpg_home)
>>> pub_key = 'fingerprint of public key'
>>> source = "/path/to/file.txt"
OK, let's take a look at what we have so far. We've setup our environment with the keyfile that gnupg is going to use, entered the path and name of the file we're going to encrypt, and told the system what public key to use. Let's keep going:
>>> unenc_file = open(source, "rb")
>>> enc_file = source+".pgp"
Pause. What we have here is that we've opened the file we want encrypted (because that's how pgp works), and we've setup the "pgp" extension for the final encrypted file. Now, you can alternate "pgp" and "gpg". Make sure you and the person you're sending this to know which extension to expect, however both work the same way.
>>> gpg.encrypt_file(unenc_file,pub_key,always_trust=True,output=enc_file)
Now, this line here takes all the variables we've just setup and completes the encryption.
>>> unenc_file.close()
>>> exit()
These last two lines closes the unencrypted file and exits the python shell. You should now have one unencrypted file and on encrypted one.
Now, the only other consideration is if you need to sign the file with your private key. This can be done by simply adding the variable:
>>> sign_key = 'fingerprint of private key'
And changing the GPG encryption line to:
>>> gpg.encrypt_file(unenc_file,pub_key,sign=sign_key,always_trust=True,output=enc_file)
And that's it. You can now encrypt one file at a time with python. Next time on this topic, we'll setup a script to automate the process and encrypt all the files in a specified folder.
Assumptions; you have python, and python-gnupg installed, and a public key from someone you want to encrypt and send files to imported to your keystore home (see Part 1 for more information here.
Let's get started with Python file encryption. Start off by getting into your python shell, and enter the following:
>>> import os
>>> import gnupg
>>> gpg_home = "/path/to/keyfile/.gnupg"
>>> gpg = gnupg.GPG(gnupghome=gpg_home)
>>> pub_key = 'fingerprint of public key'
>>> source = "/path/to/file.txt"
OK, let's take a look at what we have so far. We've setup our environment with the keyfile that gnupg is going to use, entered the path and name of the file we're going to encrypt, and told the system what public key to use. Let's keep going:
>>> unenc_file = open(source, "rb")
>>> enc_file = source+".pgp"
Pause. What we have here is that we've opened the file we want encrypted (because that's how pgp works), and we've setup the "pgp" extension for the final encrypted file. Now, you can alternate "pgp" and "gpg". Make sure you and the person you're sending this to know which extension to expect, however both work the same way.
>>> gpg.encrypt_file(unenc_file,pub_key,always_trust=True,output=enc_file)
Now, this line here takes all the variables we've just setup and completes the encryption.
>>> unenc_file.close()
>>> exit()
These last two lines closes the unencrypted file and exits the python shell. You should now have one unencrypted file and on encrypted one.
Now, the only other consideration is if you need to sign the file with your private key. This can be done by simply adding the variable:
>>> sign_key = 'fingerprint of private key'
And changing the GPG encryption line to:
>>> gpg.encrypt_file(unenc_file,pub_key,sign=sign_key,always_trust=True,output=enc_file)
And that's it. You can now encrypt one file at a time with python. Next time on this topic, we'll setup a script to automate the process and encrypt all the files in a specified folder.
Comments
Post a Comment