title: C# - Use a Third Parties Public Key To Encrypt Data Sent to Third Party
body: <p>I have been given a task to encrypt data that will be stored in our database and sent to one of our customers. I figured the best way to do this is to use asymetric encryption so that once we have encrypted it with our customer's public key nobody within but the customer (owner of the private key) will be able to decrypt it.</p>
  
  <p>I would like to store our customer's public key, algorithm type (RSA or DSA), and expiration date in our database instead of managing their certificate. The question is how can I store and use their public key? I have created the following little program to test and I am running into problems.</p>
  
  <pre><code>class Program
  {
  
      static void Main(string[] args)
      {
          const string publicKeyString = "30 81 89 02 81 81 00 c2 6e 7e e8 78 66 3d 74 fd a7 57 21 24 2d c0 ee 53 59 54 14 db f5 cb 5e 8c 64 c8 73 d5 83 d7 12 57 3f e2 92 54 9a 87 94 18 71 04 c8 b5 92 44 27 78 e9 d3 de cb 5f f6 93 75 c0 46 6b 50 c7 45 a8 38 f9 a1 83 8e 26 51 5a 8c 22 95 8e 2b 4c 10 ea c6 85 ed 02 ed 66 81 ef a3 55 15 ad 64 33 d3 bd ca 75 db 35 44 49 54 ef 6a ca 2a d5 90 a7 9b be 03 40 62 16 fd be 39 fb b6 f0 6b f8 f1 00 c0 c5 02 03 01 00 01";
          const string stringToEncrypt = "11111111111111111111";
  
          var encoding = new UTF8Encoding();
          var encryptedData = Encrypt(encoding.GetBytes(stringToEncrypt), encoding.GetBytes(publicKeyString));
  
          Console.WriteLine("**** Encrypted String ****");
          Console.WriteLine(encoding.GetString(encryptedData));
  
          var decryptedData = Decrypt(encryptedData);
  
          Console.WriteLine("**** Decrypted String ****");
          Console.WriteLine(encoding.GetString(decryptedData));
  
          Console.ReadKey();
  
  
      }
  
      static byte[] Encrypt(byte[] dataToEncrypt, byte[] publicKey)
      {
  
          var exponent = new byte[] { 1, 0, 1 };
          var rsa = new RSACryptoServiceProvider();
          rsa.ImportParameters(new RSAParameters() { Modulus = publicKey, Exponent = exponent });
          var encryptedData = rsa.Encrypt(dataToEncrypt, false);
          return encryptedData;
      }
  
      static byte[] Decrypt(byte[] dataToDecrypt)
      {
          var cert = new X509Certificate2(@"C:\certs\BP_DEV_CERT_1024.p12", "password");
          var rsa = (RSACryptoServiceProvider) cert.PrivateKey;
          var decryptedData = rsa.Decrypt(dataToDecrypt, false);
          return decryptedData;
  
      }
  
  }
  </code></pre>
  
  <p>When I run this program I get "The data to be decrypted exceeds the maximum for this modulus of 128 bytes." This leads me to believe the way I am prepare
  the public key to be used is totally wrong.</p>
  
  <p>So I guess I need to know a couple of things:</p>
  
  <ol>
  <li><p>I can copy the public key from the certificate but how should I store it in the database?</p></li>
  <li><p>How should I properly covert the public key string to a proper byte array?</p></li>
  <li><p>Any other pointers that someone may have.</p></li>
  </ol>