Maxime
Maxime

Reputation: 858

Python library to encrypt files with a GnuPG v1.2.1 Elgamal key

I'm developping an Azure function that can encrypt/decrypt PGP messages given a key and a message.

It works fine for most keys using pgpy library but I have a public key Version: GnuPG v1.2.1 (GNU/Linux) that I can't use with pgpy since it returns :

NotImplementedError: PubKeyAlgorithm.ElGamal

I searched for library that would support this and could not find one, so I decided to implement it on my own, as it did not look too difficult and the algorithm is well described on multiple places. (I can even use the elgamal python lib)

My problem is the key format is

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.2.1 (GNU/Linux)

mQGiBEAGBfgR...4gjeSOrg5udxZPAY=
=DeVs
-----END PGP PUBLIC KEY BLOCK-----

And I need parse it to get the base values for the elgamal algorithm:

p is an n bit prime.  The probability that p is actually prime is 1-(2^-t)
g is the square of a primitive root mod p
h = g^x mod p; x is randomly chosen, 1 <= x < p

So what I have tried this far is to b64decode the key, then encode the bytes to utf8, some informations are readable most stay gibberish ..

And also I'm struggling to return ASCII armored data once I'm done encrypting my message I will have something like this :

41155426744616094260320646117097177014417149694981800076578614840250750912799 21847662953868369697518400876143332991536427661010013455083791071327058182726 25533970261208196790800768492397193896597431865219430397358207004668354954697 98474109913310069178222115337497647685706488829152682996028960479235003620264 45737203823163192128318007724696648616950141941868449765480536839812674490077 75421156343349776013489675322340893073631187437555299960920254302344648207981 109276522387603521366364233791986000982319400223147578897126930221436966076170 18488401135401021688982958133975082823772861068940759564364820322251740709188 90827265910982362637936753464357413123014385175560975236907746789761396065512 26765031632882957792087095719232883256771264945602713064106822591208103463697 5564468107973560028527296626687480799230695290490266959809792867232976049666 71244794049103052356092181781178951887799420583195494031510496299648642048011 97413323791458315049498899102961692661892266857994849672602511682865645282605 71465106552027587076817289250198631699837182375059034449765014072810202331202 97056406518631644075542549218057999306283588715133100119527104765778879761699 110485038492534789098488263918059645033722124555228757157121582934391248102132 82139266370810299307518275070745017159045416343382734826251477396790862859930 109150852725244251194053374807744808523920933876515828341771567293006328719174 97953680579504996947384453783557976134604995097882075015898175236326262555884 7295684970455413822681788097881898822060140399584227196856446892730212890655

Should I just base64encode it ?

Upvotes: 0

Views: 412

Answers (0)

Related Questions