title: Convert XML Dsig format to DER ASN.1 public key
body: <p>I am working on an iPhone app that retrieves an RSA public key from an ASP.NET web service in the form:</p>
  
  <pre><code>&lt;RSAKeyValue&gt;
    &lt;Modulus&gt;qdd0paiiBJ+xYaN4TKDdbEzrJJw9xlbRAltb5OPdegjLoW60yOjL/sni52WVsGC9QxpNitZR33dnUscmI0cTJoxkXypPjbD94UpH+p4el2tuKBypHlE7bERApuUp55y8BiRkbQNFH8smZFWDwtIc/PsJryeGf8fAryel8c5V3PU=&lt;/Modulus&gt;
    &lt;Exponent&gt;AQAB&lt;/Exponent&gt;
  &lt;/RSAKeyValue&gt;
  </code></pre>
  
  <p>I need to then convert this response into an <code>NSData *</code> of the appropriate format (from some intense Googling, most likely 'ASN.1 DER' binary format. I've got code in place to convert both parts from their Base64 representations to the original binary values, but I can't for the life of me figure out a reasonable way to create the one-piece binary key.</p>
  
  <p>The code waiting for the key is the <code>-addPeerPublicKey:(NSString *) keyBits:(NSData *)</code> method of the <code>SecKeyWrapper</code> class from Apple's <code>CryptoExercise</code> example project (Code <a href="http://developer.apple.com/library/ios/#samplecode/CryptoExercise/Listings/Classes_SecKeyWrapper_m.html" rel="nofollow">here</a>).</p>
  
  <p>I would be more than happy to implement this another way--all I need is to encrypt a single string (no decryption required). As far as I can tell, though, the built-in Security framework has what I need, if I could just close this format gap. If there is a way to convert the key and send it Base64-encoded from the webservice, that works for me as well--but I couldn't find any way to ASN.1-encode it there, either.</p>