fix saving of key with a NUL character at the end #652

Just chop off the last character as long as it's a NUL.

fixes #652