 +	int			data_len,
 +				res;
 +	char	   *buf;
-+	char	   *key,
++	text	   *key,
 +			   *utf8key;
 +	int			buflen;
 +
 +	data = PG_GETARG_BYTEA_P(0);
 +	data_len = VARSIZE(data) - VARHDRSZ;
 +
-+	/* convert the key to UTF-8 */
-+	key = text_to_cstring(PG_GETARG_TEXT_PP(1));
-+	utf8key = pg_server_to_any(key, strlen(key), PG_UTF8);
-+
++	key = PG_GETARG_TEXT_P(1);
++	utf8key = convert_to_utf8(key);
 +	res = pgp_armor_header((uint8 *) VARDATA(data), data_len,
-+							utf8key, strlen(utf8key),
++							VARDATA(utf8key), VARSIZE(utf8key) - VARHDRSZ,
 +							(uint8 **) &buf, &buflen);
 +	if (res < 0)
 +		ereport(ERROR,

 	n = b64_encode(src, len, pos);
 	pos += n;
 
@@ -370,10 +387,115 @@ out:
 	return res;
 }
 

+LANGUAGE C IMMUTABLE STRICT;
+
diff --git a/pgp-armor.c b/pgp-armor.c
index 87adf91..86a0810 100644
--- a/pgp-armor.c
+++ b/pgp-armor.c
@@ -179,7 +179,7 @@ b64_dec_len(unsigned srclen)
