Search code examples
gnupg

How to use *.pub/*.sec files to encrypt/decrypt another file?


I created a pair of *.pub and *.sec files using the instructions and code given here:

https://www.gnupg.org/documentation/manuals/gnupg/Unattended-GPG-key-generation.html

(I am using this documentation because the ultimate application I have in mind is an automated encryption/decryption pipeline.)

Q1: How can I use gpg2 and the *.pub file to encrypt another file?

Q2: How can I use gpg2 and the companion *.sec to decrypt a file encrypted using the companion *.pub file?


Important: I am interested only in answers that are suitable for programmatic implementation of an unsupervised operation. Please do not post answers that can only be carried out interactively. I am particularly interested in solutions that can be implemented in Python.


Please include precise pointers to the relevant documentation.


Solution

  • Some information about what you said:

    I created a pair of *.pub and *.sec files using the instructions

    Perfect to share the public key(s) with people you are exchanging information, but technically, when you are working programmatically, you don't need to use these files directly.

    To be noted:

    • when you encrypt data, you will specify the recipient corresponding to the key to use to encrypt
    • when you decrypt data, you will first import the owner's public key, and then you will be able to decrypt data without specifying recipient, because it is embedded in the encrypted data

    Actually, I am somewhat confused on this question. I have read conflicting information [...]

    I agree it's quite confusing. In this situation, I think it is better to use version 1 for which there is more experience, and for which you find third party library to use.

    In this answer, I tried:

    • python-gnupg (for GnuPG v1) which is a well known Python library and match perfectly your needs
    • cryptorito (for GnuPG v2) for which I didn't find enough documentation

    With the first library, you can simply install it in your system:

    sudo pip install python-gnupg
    

    And then write a Python script to perform all the operations you want.

    I wrote a simple one to answer your question.

    #!/bin/python
    
    import gnupg
    
    GPG_DIR='/home/bsquare/.gnupg'
    FILE_TO_ENCRYPT='/tmp/myFileToEncrypt'
    ENCRYPTED_FILE='/tmp/encryptedFile'
    DECRYPTED_FILE='/tmp/decryptedFile'
    SENDER_USER='Bsquare'
    TARGET_USER='Kjo'
    
    gpg = gnupg.GPG(gnupghome=GPG_DIR)
    
    print("Listing keys ...")
    print(gpg.list_keys())
    
    # On SENDER_USER side ... encrypt the file for TARGET_USER, with his public key (would match the kjo.pub if the key was exported).
    print("Encrypting file " + FILE_TO_ENCRYPT + " for " + TARGET_USER + " ...")
    with open(FILE_TO_ENCRYPT, "rb") as sourceFile:
        encrypted_ascii_data = gpg.encrypt_file(sourceFile, TARGET_USER)
        # print(encrypted_ascii_data)
        with open(ENCRYPTED_FILE, "w+") as targetFile:
            print("encrypted_ascii_data", targetFile)
    
    
    # On TARGET_USER side ... decrypt the file with his private key (would match the kjo.sec if the key was exported).
    print("Decrypting file " + ENCRYPTED_FILE + " for " + TARGET_USER + " ...")
    with open(ENCRYPTED_FILE, "rb") as sourceFile:
        decrypted_ascii_data = gpg.decrypt_file(sourceFile)
        # print(decrypted_ascii_data)
        with open(DECRYPTED_FILE, "w+") as targetFile:
            print(decrypted_ascii_data, targetFile)
    

    To be noted my keyring contains pub/sec pair for my Bsquare user, and the pub key of Kjo user.