diff --git a/modules/saml2/lib/Message.php b/modules/saml2/lib/Message.php
index 866d95f3206ebfc5fe1108becd58798130a32e21..be6af33403b00384887421057eb274a9f460a326 100644
--- a/modules/saml2/lib/Message.php
+++ b/modules/saml2/lib/Message.php
@@ -448,14 +448,25 @@ class sspmod_saml2_Message {
 	private static function encodeAttributes(SimpleSAML_Configuration $srcMetadata,
 		SimpleSAML_Configuration $dstMetadata, array $attributes) {
 
-		$base64Attributes = $dstMetadata->getBoolean('base64attributes', FALSE);
+		$base64Attributes = $dstMetadata->getBoolean('base64attributes', NULL);
+		if ($base64Attributes === NULL) {
+			$base64Attributes = $srcMetadata->getBoolean('base64attributes', FALSE);
+		}
+
 		if ($base64Attributes) {
 			$defaultEncoding = 'base64';
 		} else {
 			$defaultEncoding = 'string';
 		}
 
-		$encodings = $dstMetadata->getArray('attributeencodings', array());
+		$srcEncodings = $srcMetadata->getArray('attributeencodings', array());
+		$dstEncodings = $dstMetadata->getArray('attributeencodings', array());
+
+		/*
+		 * Merge the two encoding arrays. Encodings specified in the target metadata
+		 * takes precedence over the source metadata.
+		 */
+		$encodings = array_merge($srcEncodings, $dstEncodings);
 
 		$ret = array();
 		foreach ($attributes as $name => $values) {