1. $encode is allowing multiple padding switches to be specified, and uses them in a hierarchy regardless of the order used.

2. $decode is ignoring the 'pnz' switches or the lack of a padding switch, and instead attempts to find all 4 padding types at the end of the decrypted string.

--
Depending on the padding switch used when encrypting the string, decryption can result in false positives which removes too many bytes. The default PKCS#5 padding or the 'p' padding cannot be confused with each other, but it's easy for 'z' and 'n' to have false positives, as shown by this example:

Code:
//bset -t &v 1 $chr(192) | echo -a original = $bvar(&v,1-) | noop $encode(&v,bmcz,key) $decode(&v,bmcz,key) | echo 4 -a decrypted = $bvar(&v,1-)


result:
original = 195 128
decrypted = 195

As I understand padding behavior, decode should only search for the default PKCS#5 padding if none of the 'pnz' switches are used, and should only search for 1 of the 4 methods specified by the p|n|z|$null padding switch.

This should make valid syntax have the pnz switches be mutually exclusive for both encryption and decryption. Instead, $encode allows all 3 padding switches to be used, and uses them in this order:

A. $encode pads with 'z' method if 'z' switch is present, regardless if it's the 1st or last of several padding switches.

B. Otherwise, $encode pads with the 'n' method if 'z' switch is present, regardless if 'n' switch is used and if 'n' precedes or follows 'p'.

C. Otherwise, $encode pads with the 'p' method only if it's the only padding switch used.

D. Otherwise, $encode pads with the standard PKCS#5 padding only if no padding switch is used.

Solution:

$encode and $decode should permit only 1 or zero of the 'pnz' switches to be used.
$decode should only remove the 1 of 4 padding methods defined by which of the 'pnz' padding switches is used or their lack of use. This means that, if a different padding method is specified during decryption than used during encryption, the decrypted message should retain the padding added by the default PKCS#5 method, or the spaces from the 'p' method, etc.