Last active
February 23, 2016 02:01
-
-
Save AgustinParmisano/c67f1f20378e720b6f8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ejemplo de: http://www.saltycrane.com/blog/2011/10/python-gnupg-gpg-example/ | |
#Workflow: | |
#1. Generar keys (o sea los respectivos home de cada usuario tanto cliente como servidor) | |
#2. Listar keys de usuario1, verlas. | |
#3. Exportar keys de usuario2, genera un archivo con las keys .asc, guardar el path o enviarlo. | |
#4. Importar las keys desde usuario1 (o sea el archivo .asc generado desde la exportacion) | |
#5. Listar keys de usuario1 otra vez, ver si la nueva key se lista | |
#6. Encriptar archivo desde usuario1, con el fingerprint del usuario2 (el fingerprint se lista al listar las keys), esto genera un archivo encriptado, recordar el path o enviarlo | |
#7. Desencriptar el archivo desde usuario2 pasando el path al mismo | |
# PROBLEMAS: al exportar la key, el raw_input toma el fingerprint a exportar pero la funcion de gpg.export_keys a veces no retorna nada | |
# INCONVENIENTES: Hay que estar enviando archivos de un lado a otro todo el tiempo, hay que enviar las keys exportadas, lo cual dificulta la comunicación | |
import gnupg | |
import os | |
from pprint import pprint | |
dirHome = "/home/spring" | |
importedPubKey = '' | |
def generate_key(password, mail, gpghome='/gpghome'): | |
homeGPGDir = dirHome + gpghome | |
os.system('rm -rf ' + homeGPGDir) | |
gpg = gnupg.GPG(gnupghome=homeGPGDir) | |
input_data = gpg.gen_key_input( | |
name_email=mail, | |
passphrase=password) | |
key = gpg.gen_key(input_data) | |
print key | |
def listar_keys(gpghome='/gpghome'): | |
homeGPGDir = dirHome + gpghome | |
gpg = gnupg.GPG(gnupghome=homeGPGDir) | |
public_keys = gpg.list_keys() | |
private_keys = gpg.list_keys(True) | |
print 'public keys:' | |
pprint(public_keys) | |
print 'private keys:' | |
pprint(private_keys) | |
def encrypt_string(recipiente, string, gpghome='/gpghome'): | |
homeGPGDir = dirHome + gpghome | |
gpg = gnupg.GPG(gnupghome=homeGPGDir) | |
unencrypted_string = string | |
encrypted_data = gpg.encrypt(unencrypted_string, recipiente) | |
encrypted_string = str(encrypted_data) | |
print 'ok: ', encrypted_data.ok | |
print 'status: ', encrypted_data.status | |
print 'stderr: ', encrypted_data.stderr | |
print 'unencrypted_string: ', unencrypted_string | |
print 'encrypted_string: ', encrypted_string | |
return encrypted_string | |
def decrypt_string(password, encryption, gpghome='/gpghome'): | |
homeGPGDir = dirHome + gpghome | |
gpg = gnupg.GPG(gnupghome=homeGPGDir) | |
encrypted_data = encryption | |
encrypted_string = str(encrypted_data) | |
decrypted_data = gpg.decrypt(encrypted_string, passphrase=password) | |
print "Ecnrypted String ", encrypted_string | |
print 'ok: ', decrypted_data.ok | |
print 'status: ', decrypted_data.status | |
print 'stderr: ', decrypted_data.stderr | |
print 'decrypted string: ', decrypted_data.data | |
def export_pubkey(gpghome='/gpghome'): | |
homeGPGDir = dirHome + gpghome | |
gpg = gnupg.GPG(gnupghome=homeGPGDir) | |
key = raw_input('Ingrese la key a exportar ') | |
print 'key es ' + key | |
ascii_armored_public_keys = gpg.export_keys(str(key)) | |
ascii_armored_private_keys = gpg.export_keys(str(key), True) | |
with open('mykeyfile.asc', 'w') as f: | |
f.write(ascii_armored_public_keys) | |
f.write(ascii_armored_private_keys) | |
pprint(ascii_armored_public_keys) | |
pprint(ascii_armored_private_keys) | |
def import_pubkey(keyFileDir, gpghome='/gpghome'): | |
importedPubKey = '' | |
homeGPGDir = dirHome + gpghome | |
gpg = gnupg.GPG(gnupghome=homeGPGDir) | |
key_data = open(keyFileDir).read() | |
import_result = gpg.import_keys(key_data) | |
pprint(import_result.results) | |
def encryp_file(recipiente, decryptedFile, gpghome='/gpghome'): | |
homeGPGDir = dirHome + gpghome | |
gpg = gnupg.GPG(gnupghome=homeGPGDir) | |
with open(decryptedFile, 'rb') as f: | |
status = gpg.encrypt_file( | |
f, recipients=[recipiente], | |
output='my-encrypted.txt.gpg', always_trust=True) | |
print 'ok: ', status.ok | |
print 'status: ', status.status | |
print 'stderr: ', status.stderr | |
def decrypt_file(encryptedFile, password, gpghome='/gpghome'): | |
homeGPGDir = dirHome + gpghome | |
gpg = gnupg.GPG(gnupghome=homeGPGDir) | |
with open(encryptedFile, 'rb') as f: | |
status = gpg.decrypt_file(f, passphrase=password, output='my-decrypted.txt') | |
print 'ok: ', status.ok | |
print 'status: ', status.status | |
print 'stderr: ', status.stderr | |
gpghome = raw_input('Ingrese su home de GPG (por defecto sera /home/spring/gpghome) ') | |
while True: | |
opcion = 0 | |
opcion = raw_input('Ingrese la opcion: 1: Generar Clave, 2: Encriptar string, 3: Desencriptar String, 4: Exportar Pub Key, 5: Importar Pub Key, 6: Encriptar Archivo, 7: Desencriptar Archivo, 8: Listar Llaves') | |
if opcion == "1": | |
password = raw_input('Ingrese passphrase ') | |
mail = raw_input('Ingrese mail ') | |
if (not password) | (not mail): | |
print 'Todo mal' | |
generate_key(password, mail, gpghome) | |
if opcion == "2": | |
recipiente = raw_input('ingrese mail o fingerprint del recipiente ') | |
string = raw_input('Ingrese el string a encriptar ') | |
if (not string) | (not recipiente): | |
print 'Todo mal' | |
encrypt_string(string, gpghome) | |
if opcion == "3": | |
password = raw_input('Ingrese passphrase ') | |
string = raw_input('Ingrese el string a desencriptar ') | |
if (not password) | (not string): | |
print 'Todo mal' | |
decrypt_string(password, string, gpghome) | |
if opcion == "4": | |
export_pubkey() | |
if opcion == "5": | |
keyFileDir = raw_input('Ingrese direccion del archivo de la llave publica ') | |
if (not keyFileDir): | |
print 'Todo mal' | |
import_pubkey(keyFileDir, gpghome) | |
if opcion == "6": | |
recipiente = raw_input('ingrese mail o fingerprint del recipiente ') | |
decryptedFile = raw_input('Ingrese el archivo a encriptar ') | |
if (not decryptedFile) | (not recipiente): | |
print 'Todo mal' | |
encryp_file(recipiente, decryptedFile, gpghome) | |
if opcion == "7": | |
password = raw_input('Ingrese passphrase ') | |
encryptedFile = raw_input('Ingrese el archivo a desencriptar ') | |
if (not password) | (not encryptedFile): | |
print 'Todo mal' | |
decrypt_file(encryptedFile, password, gpghome) | |
if opcion == "8": | |
listar_keys(gpghome) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ejemplo de: http://www.saltycrane.com/blog/2011/10/python-gnupg-gpg-example/
Workflow:
#1. Generar keys (o sea los respectivos home de cada usuario tanto cliente como servidor)
#2. Listar keys de usuario1, verlas.
#3. Exportar keys de usuario2, genera un archivo con las keys .asc, guardar el path o enviarlo.
#4. Importar las keys desde usuario1 (o sea el archivo .asc generado desde la exportacion)
#5. Listar keys de usuario1 otra vez, ver si la nueva key se lista
#6. Encriptar archivo desde usuario1, con el fingerprint del usuario2 (el fingerprint se lista al listar las keys), esto genera un archivo encriptado, recordar el path o enviarlo
#7. Desencriptar el archivo desde usuario2 pasando el path al mismo
PROBLEMAS: al exportar la key, el raw_input toma el fingerprint a exportar pero la funcion de gpg.export_keys a veces no retorna nada
INCONVENIENTES: Hay que estar enviando archivos de un lado a otro todo el tiempo, hay que enviar las keys exportadas, lo cual dificulta la comunicación