I don't quite understand why you change from encode to decode ?

This breaks the encoding of a newly generated public key in sc_pkcs15init_generate_key().