python 3.x - How do I tell if OpenPGP encryption is symmetric or asymmetric? -
is there way tell if things encrypted via gnu privacy guard symmetric or asymmetric (without decrypting them or knowing start with)? how?
anyway (for want know i'm doing), used python 3.x program gui-based ide of sorts can open symmetrically encrypted files (and save them, too). can open asymmetrically encrypted files (enter passphrase use secret key instead of passphrase decrypt symmetrically encrypted file). however, doesn't know they're asymmetric , overwrite them symmetrically encrypted files if saved. nice able save them asymmetrically, too. editor uses gpg
command-line program on linux (no gpg
libraries or that).
i have checkbox on password prompt asymmetric encryption, i'd rather not make has manual thing user.
for own personal files, add kind of marker saved files distinguish, want able open them correctly if weren't created in ide.
i know there's question similar title, question asked in body fundamentally different.
openpgp hybrid cryptosystem, means messages (or files) encrypted symmetrically using so-called session key. session key again encrypted using asymmetric encryption (using public key) or symmetric encryption again (using string key function).
this has technical reasons (asymmetric cryptography very slow large amounts of data), practical ones: encrypting small session key multiple times (once each recipient), can have multiple recipients different keys , mix asymmetric (public key) , symmetric (password based) encryption in single openpgp message.
each of encrypted copies of session key form openpgp packet, either packet tag 1 (public-key encrypted session key packet) or packet tag 3 (symmetric-key encrypted session key packet). packets in openpgp message can decomposed using pgpdump
. example using gnupg create openpgp message encrypting both own key , symmetrically passphrase foo
:
$ echo foo | gpg --recipient a4ff2279 --symmetric --passphrase foo --encrypt | pgpdump old: public-key encrypted session key packet(tag 1)(524 bytes) new version(3) key id - 0xcc73b287a4388025 pub alg - rsa encrypt or sign(pub 1) rsa m^e mod n(4096 bits) - ... -> m = sym alg(1 byte) + checksum(2 bytes) + pkcs-1 block type 02 old: symmetric-key encrypted session key packet(tag 3)(46 bytes) new version(4) sym alg - aes 128-bit key(sym 7) iterated , salted string-to-key(s2k 3): hash alg - sha512(hash 10) salt - 0c a6 e6 1d d2 f4 9a 50 count - 102400(coded count 105) encrypted session key -> sym alg(1 bytes) + session key new: symmetrically encrypted , mdc packet(tag 18)(63 bytes) ver 1 encrypted data [sym alg specified in sym-key encrypted session key] (plain text + mdc sha1(20 bytes))
each of first 2 packets forms key open encrypted string in symmetrically encrypted , mdc packet.
this explains how analyze how message encrypted: through packets, looking either tag 1 or 3 packets, indicating asymmetric or symmetric encryption (and aware both might exist). seem lucky, , python gnupg module brings listpackets
class, neither have interface pgpdump
nor write own openpgp parser.
Comments
Post a Comment