Android Question SimplePGP eats bytes at the end of a file

Hello, i experiencing some problems with encrypting/decrypting files with simplepgp.
Maybe i am doing something wrong or misunderstood the example.
But files with size 2Mb and bigger the simplepgp eats some bytes during encryption/decryption at the end of the file and makes the file corrupt.

pgp code:
    Dim password As String = RandomString(64)
    Dim pubkey As OutputStream = File.OpenOutput(File.DirData("temp"),"publickey.pem",False)
    Dim seckey As OutputStream = File.OpenOutput(File.DirData("temp"),"secretkey.pem",False)
    pgpkeys.Initialize("")
    pgpkeys.generateKeyPair2("pgp",password,1024,pubkey,seckey)
    
    Dim crypt As PGPMessageEncryptor
    crypt.Initialize("crypt")
    Dim data As InputStream = File.OpenInput(File.DirData("temp"),"file.mp4")
    Dim publickey As InputStream = File.OpenInput(File.DirData("temp"),"publickey.pem")
    Dim out_data As OutputStream = File.OpenOutput(File.DirData("temp"),"file.pgp",False)
    crypt.encrypt(publickey,"", data, out_data)
    
    Dim input_data As InputStream = File.OpenInput(File.DirData("temp"),"file.pgp")
    Dim secretkey As InputStream = File.OpenInput(File.DirData("temp"),"secretkey.pem")
    Dim decrypted_data As OutputStream = File.OpenOutput(File.DirData("temp"),"testfile.mp4",False)
    crypt.decrypt(password,secretkey,input_data,decrypted_data)

Can anybody confirm? Or is it just a me problem?
 

emexes

Expert
Licensed User
Longtime User
My first thought is that if it looks like the text is otherwise ok, just a bit shorter, that it's a character encoding issue.

Whereabouts can I download the B4A version of PGPMessageEncrypter from to give it a burl?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Whereabouts can I download the B4A version of PGPMessageEncrypter from to give it a burl?
Simple search for SimplePGP would have pointed you to the Librarythread ;-)
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
Simple search for SimplePGP would have pointed you to the Librarythread ;-)

True, but then I run into the question of: am I using the exact same version as the OP? And are we talking B4A or B4J? So simpler if they just point me to the one they're using, rather than me guess.

And I've just now discovered it seems the OP example code needs more than just the library to make it run.
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
Simple search for SimplePGP would have pointed you to the Librarythread ;-)
Based on the Size of the Additional jars you need to download them here.
1741674678063.png


I am dropping out of the race. Collecting random stuff together to make the example compile is above my free-support paygrade.

If the OP is keen for an answer and provides a complete ready-to-compile example, I'll have another look.
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Longtime User
and downloaded a fresh Copy
For some reason it isn't downloading here. I think that is a sign from the gods above telling me to let it go, because dinner is on the table here.

If you've got the OP example running, was I anywhere in the ballpark with my guess about it being a character-encoding issue?

Although I've just realised that the example is for a binary video file, not for a text file, so... probably not.

But still, it'd be useful to confirm that the dropped bytes are from the end of the file, and that the rest of the file still matches what went in at the other end.

And then I'd try adding say 10k of recognizable junk at the end, and see what gets dropped, and if it's on a power-of-2 boundary.

Or maybe salt is getting added at the front but not properly desalted at the other end. Although... that doesn't sound like a bug that would exist very long in a widely-used library.
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
But files with size 2Mb and bigger the simplepgp eats some bytes

Do (non-empty) files under 2 million bytes come out the other end being exactly the same size, and byte-for-byte identical eg

FC /B file.mp4 testfile.mp4

reports no differences found?
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
files of 4kb where simplepgp ate 1kb

Is that 4 kB test file innocuous enough that you can post it before and after it went through the PGP encryption-decryption wringer?

I'm interested to see if it's possible to work out whereabouts in the file the bytes were lost.

And also whether it might have been cut at an encryptionary-block-size-looking boundary.
 
Upvote 0
Top