title: Paypal Encrypted Website Payments with ASP.NET
body: <p>I am trying to generate encrypted URLs to redirect the customer to PayPal for payment. I have uploaded my own certificate to PayPal, downloaded PayPal's public certificate, and use the following code to do the job:</p>
  
  <pre><code>  Dim _vals As String = "cert_id=ID" &amp; vbLf &amp; "cmd=_cart" &amp; vbLf &amp; _
                 "business=businessemail" &amp; vbLf &amp; _
                 "upload=1" &amp; vbLf &amp; _
                 "no_shipping=1" &amp; vbLf &amp; _
                 "custom=poidccffgd" &amp; vbLf &amp; _
                 "invoice=oid12345" &amp; vbLf &amp; _
                 "currency_code=EUR" &amp; vbLf &amp; _
                 "item_name_1=" &amp; _oname &amp; vbLf &amp; _
                 "amount_1=" &amp; _curps &amp; vbLf &amp; _
                 "item_number_1=10" &amp; vbLf &amp; _
                 "item_name_2=Free Bonus" &amp; vbLf &amp; _
                 "amount_2=0" &amp; vbLf &amp; _
                 "discount_rate_cart=30"
  
      Dim _url As String = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_s-xclick&amp;encrypted=" &amp; Server.UrlEncode(EncryptX509(_vals))
  
  Friend Shared Function EncryptX509(aData As String) As String
      Dim _signerCert As New X509Certificates.X509Certificate2(MyPrivateCertificateByteArray, "password", X509Certificates.X509KeyStorageFlags.DefaultKeySet)
      Dim _recipientCert As New X509Certificates.X509Certificate2(PayPalPublicCertificateByteArray)
      'get data
      '--------
      Dim messageBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(aData)
      'sign
      '----
      Dim content = New System.Security.Cryptography.Pkcs.ContentInfo(messageBytes)
  
      Dim signed = New System.Security.Cryptography.Pkcs.SignedCms(content)
      Dim signer = New System.Security.Cryptography.Pkcs.CmsSigner(_signerCert)
      signed.ComputeSignature(signer)
      Dim signedBytes = signed.Encode()
      'encrypt
      '-------
      Dim content2 = New System.Security.Cryptography.Pkcs.ContentInfo(signedBytes)
      Dim envMsg = New System.Security.Cryptography.Pkcs.EnvelopedCms(content2)
      Dim recipient = New Pkcs.CmsRecipient(Pkcs.SubjectIdentifierType.IssuerAndSerialNumber, _recipientCert)
      envMsg.Encrypt(recipient)
      Dim encryptedBytes As Byte() = envMsg.Encode()
      'format
      '------
      Const PKCS7_HEADER As String = "-----BEGIN PKCS7-----"
      Const PKCS7_FOOTER As String = "-----END PKCS7-----"
  
      Dim base64 = Convert.ToBase64String(encryptedBytes)
      Dim formatted = New StringBuilder()
      formatted.Append(PKCS7_HEADER)
      formatted.Append(base64)
      formatted.Append(PKCS7_FOOTER)
      Return formatted.ToString
  End Function
  </code></pre>
  
  <p>Yet, when I go to generated URL it says cannot decode certificate ID! I tried with both live and sandbox and get the same result. What is the problem with this code? How do I get a working EWP with ASP.NET? Please do not suggest alternative methods such as a hosted Express Checkout, or hosted buttons.</p>
  
  <p>Thank you.</p>