in reply to works as a perl script, but not as cgi
That's the output one gets if the first program isn't called.
$ gpg --enarmor < /dev/null | sed -n 5p =twTO [download]
Loops like gpg --gen-random 1 16 is failing. Did you check your error log?