title: ECIES encryption/decryption Java
body: <p>I am trying to use ECIES for encryption and decryption. This is what I have done:</p>
  <ol>
  <li><p>generated ECC key pair</p>
  </li>
  <li><p>generated CSR</p>
  </li>
  <li><p>Generated X509 certificate, signed by intermediate CA</p>
  </li>
  <li><p>Server side, I need to use this certificate to encrypt data. So I have extracted <code>PublicKey</code> (which is showing <code>BCECPublicKey</code> format) from certificate.</p>
  <p>I couldn't find a way to convert it into <code>ECPublicKey</code> format so after upgrading to bouncycastle 1.55, I am able to use directly <code>BCECPublicKey</code> for encryption.</p>
  </li>
  <li><p>Converted private key into <code>ECPrivateKey</code> and using it for decryption but now it's throwing <code>BadPaddingException: Invalid MAC during decryption</code></p>
  </li>
  </ol>
  <p>I am new to cryptography, Please help to resolve this. Below is code I am using:</p>
  <pre><code>byte[] localcert = Base64.decode(
              &quot;MIID5TCCAc2gAwIBAgICEAEwDQYJKoZIhvcNAQELBQAwMzELMAkGA1UEBhMCc2cx&quot; +
                      &quot;CzAJBgNVBAgMAnNnMRcwFQYDVQQKDA5pbnRlcm1lZGlhdGVDQTAeFw0xNjExMjgw&quot; +
                      &quot;NDAzMjdaFw0xNzEyMDgwNDAzMjdaMDsxCzAJBgNVBAYTAnNnMQswCQYDVQQIEwJz&quot; +
                      &quot;ZzELMAkGA1UEBxMCc2cxEjAQBgNVBAoTCWxvY2FsaG9zdDBZMBMGByqGSM49AgEG&quot; +
                      &quot;CCqGSM49AwEHA0IABDuhAyMw6OilNmfWo1v6b8XwU8xbQm0Sy/I9qpdC4+qDToSl&quot; +
                      &quot;EOe+vw7GiVgONTJz2gwMW+VgoGp49aM5GTPo39ujgcUwgcIwCQYDVR0TBAIwADAR&quot; +
                      &quot;BglghkgBhvhCAQEEBAMCBaAwMwYJYIZIAYb4QgENBCYWJE9wZW5TU0wgR2VuZXJh&quot; +
                      &quot;dGVkIENsaWVudCBDZXJ0aWZpY2F0ZTAdBgNVHQ4EFgQUnbUGm/qaO1JbhY+qVlXw&quot; +
                      &quot;BewUI/swHwYDVR0jBBgwFoAUPSzKlcBTp0pCQ290SlDLmIQS+/0wDgYDVR0PAQH/&quot; +
                      &quot;BAQDAgXgMB0GA1UdJQQWMBQGCCsGAQUFBwMCBggrBgEFBQcDBDANBgkqhkiG9w0B&quot; +
                      &quot;AQsFAAOCAgEAJj0J2A3AAcrRw02ZzQsEC4nTyd05krF4oRFo0JODlzNiKaOhQt76&quot; +
                      &quot;Va427cpVUZwmjb/f1We+AjLJgQiEfnuD7JPSvXHLQTbXNDMgpZ9HXHZoXYfH+2h7&quot; +
                      &quot;MGvw6Qkj4lC10q9UC14rDSD/ZsR1J0mQCQuOIBRFNOkSPiSUu4zouCD3xv5uZVXR&quot; +
                      &quot;mimhJ1zgqSYF4LHegJAVwrowMsuaeQXybrIQ+/LJ8HXf8McvPZwtQTuoN/q5zHXz&quot; +
                      &quot;l+7q4nglyVY+TXPAdwyha0Yq2p0z0jdWm5UpEehmIpXtJghNtcCCRfb48flfZ/B7&quot; +
                      &quot;JW9VrlcjScOtQfSOrElYgwJ8MlUTzz7oWgbbVp9uNQZeAQQPeOQYLAvSNchPnLiP&quot; +
                      &quot;ftPuICW2siDeFC42lwYsDYR/9sYs7/gzL79i7bHrdMJ07brXw30hb1r6Vu9a+sHF&quot; +
                      &quot;D087NxHv33u22+W/2PMLDE89MynTC3H3gWvyzGIky0/kYSpZO/xZuFrg0jIJu0lH&quot; +
                      &quot;9b7jw1hQM1nDkTO5Gn2wJuaHaiZ22tMr47e4Xlkctal4hAA4Ya1uBXuMuwy0BC8q&quot; +
                      &quot;nLLxCLBcJJPAyIG2LvIT2vdWIP0Gz84mHKDbOPekHmXIF3bHE4pPeyDIJ+w00UoM&quot; +
                      &quot;xJdedT5BJarqEpiQtrGn4FBh3fsnHFXyNnNMCIylCvbg0Ij/AsQJCpg=&quot;);
  
      CertificateFactory cf = CertificateFactory.getInstance(&quot;X.509&quot;);
      X509Certificate x509Certificate= (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(localcert));
      PublicKey publicKey=x509Certificate.getPublicKey();
  
      byte[] pkey=Base64.decode(&quot;MHcCAQEEINmVG7z3YutAqRYZ5iAaJSXcP+GJWjtmSx3ba6RfKkJQoAoGCCqGSM49&quot; +
              &quot;AwEHoUQDQgAEO6EDIzDo6KU2Z9ajW/pvxfBTzFtCbRLL8j2ql0Lj6oNOhKUQ576/&quot; +
              &quot;DsaJWA41MnPaDAxb5WCganj1ozkZM+jf2w==&quot;);
      Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
      ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(&quot;prime256v1&quot;);
      KeyFactory kf = KeyFactory.getInstance(&quot;ECDSA&quot;, new BouncyCastleProvider());
      ECPrivateKeySpec ecPrivateKeySpec = new ECPrivateKeySpec(new BigInteger(1, pkey), spec);
      ECPrivateKey privkey= (ECPrivateKey) kf.generatePrivate(ecPrivateKeySpec);
  
      String name = &quot;prime256v1&quot;;
  
      //  generate derivation and encoding vectors
      byte[]  d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
      byte[]  e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
      IESParameterSpec param = new IESParameterSpec(d, e, 256);
  
      Cipher iesCipher = Cipher.getInstance(&quot;ECIES&quot;, BouncyCastleProvider.PROVIDER_NAME);
      //Encrypt
      iesCipher.init(Cipher.ENCRYPT_MODE, publicKey, param);
      byte[] enc= iesCipher.doFinal(&quot;TestECIES&quot;.getBytes());
      System.out.println(new String(enc));
      //Decrypt
      iesCipher.init(Cipher.DECRYPT_MODE, privkey, param);
      byte[] decry=iesCipher.doFinal(enc);
      System.out.println(new String(decry));
  </code></pre>