It seems that
pdfencrypt requires an 8bit (ISO-8859) encoded password and does not know how to deal with (i.e. convert) UTF-8 passwords.
You must use a compatible character encoding when executing
pdfencrypt, for encryption and decryption. Plain ASCII ("C") or ISO-8859-n (like "de_DE.latin1" as suggested by Hauke Laging) should work. In my opinion, that just instructs
pdfencrypt to take the characters of your password as is (i.e. no conversion necessary), that's all.
Just add this line at the top of your script:
export LC_ALL=de_DE.latin1 # Or fr_FR, or latin15, or... Any valid locale for your system
Or changing this single line may be enough:
LC_ALL=de_DE.latin1 pdfencrypt "$1" -p "$password" -o "$2"
Do the same for the decoding.
If you convert your password with
iconv instead of setting LC_ALL, I guess you would have the same issue since
pdfencrypt would still believe you are giving it UTF-8 characters when it encounters 8bit ones.
Anyway, to make things easier, you probably should stick to the 7bit ASCII character set: space, non-accented letters, digits, basic punctuation, etc. (
man 7 ascii)
As for the space, you should be aware that reading into a specific variable with (
read -r var) removes leading and trailing spaces (actually the characters in the shell variable
IFS). That may be annoying for a password. You should use
read -r which put the entire line read into the
That would give that code:
read -r -s -p "Password: "
pdfencrypt "$1" -p "$REPLY" -o "$2"