title: Encryption and Decryption with X.509 public certificate
body: <p>I want to encrypt my post payload with an X.509 certificate and the inherited public key. So far I have this java code to perform the encryption</p>
  
  <pre><code>private String encrypt(String str) throws Exception {
      ClassPathResource classPathResource = new ClassPathResource("testcert1.crt");
      CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
      X509Certificate certificate = (X509Certificate)certificateFactory.generateCertificate(classPathResource.getInputStream());
      PublicKey pk = certificate.getPublicKey();
      Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
      cipher.init(Cipher.ENCRYPT_MODE, pk);
      return Base64.encodeBase64String(cipher.doFinal(str.getBytes()));
  }
  </code></pre>
  
  <p>which returns the base64 encoded string. From the endpoint I am always getting the result, that the certificate is not valid.</p>
  
  <p>So I want to validate my encrypted string on the console using the <code>openssl</code> command, but failing to do so. </p>
  
  <p>I can read out the certificate with: <code>openssl x509 -in testcert1.crt -text -noout</code></p>
  
  <pre><code>Certificate:
      Data:
          Version: 3 (0x2)
          Serial Number: 0 (0x0)
          Signature Algorithm: md5WithRSAEncryption
          Issuer: C=xxx, ST=xxx, L=xxx, O=xxx, OU=xxx, CN=xxx
          Validity
              Not Before: Jul 24 11:40:39 2013 GMT
              Not After : Jul 24 11:40:39 2015 GMT
          Subject: C=xxx, ST=xxx, L=xxx, O=xxx, OU=xxx, CN=xxx
          Subject Public Key Info:
              Public Key Algorithm: rsaEncryption
              RSA Public Key: (4096 bit)
                  Modulus (4096 bit):
                  ....
               Exponent: 65537 (0x10001)
  </code></pre>
  
  <p>But I cannot figure out the command lines to encrypt/decrypt a text file using that certificate</p>