Encode key when generating query string

The key is decoded during the parsing of the query string, but not when it is encoded.