Wednesday, May 2, 2012

Eclipse JPA : errorDecryptingPassword

[EL Severe]: 2012-05-02 14:43:04.13--ServerSession(29752800)--java.lang.NullPointerException
at java.util.ResourceBundle.getBundle(ResourceBundle.java:950)
at org.eclipse.persistence.exceptions.i18n.ExceptionMessageGenerator.buildMessage(ExceptionMessageGenerator.java:47)
at org.eclipse.persistence.exceptions.ValidationException.errorDecryptingPassword(ValidationException.java:864)
at org.eclipse.persistence.internal.security.JCEEncryptor.decryptPassword(JCEEncryptor.java:124)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.updateLogins(EntityManagerSetupImpl.java:1183)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.updateServerSession(EntityManagerSetupImpl.java:1449)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:372)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:164)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:221)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:209)
at com.acme.peppo2.cmdb.client.CMDBClient.<init>(CMDBClient.java:17)
at com.acme.peppo2.cmdb.client.CMDBClient.main(CMDBClient.java:29)

Exception in thread "main" javax.persistence.PersistenceException: java.lang.NullPointerException
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:417)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:164)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:221)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:209)
at com.acme.peppo2.cmdb.client.CMDBClient.<init>(CMDBClient.java:17)
at com.acme.peppo2.cmdb.client.CMDBClient.main(CMDBClient.java:29)
Caused by: java.lang.NullPointerException
at java.util.ResourceBundle.getBundle(ResourceBundle.java:950)
at org.eclipse.persistence.exceptions.i18n.ExceptionMessageGenerator.buildMessage(ExceptionMessageGenerator.java:47)
at org.eclipse.persistence.exceptions.ValidationException.errorDecryptingPassword(ValidationException.java:864)
at org.eclipse.persistence.internal.security.JCEEncryptor.decryptPassword(JCEEncryptor.java:124)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.updateLogins(EntityManagerSetupImpl.java:1183)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.updateServerSession(EntityManagerSetupImpl.java:1449)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:372)
... 5 more


EntityManagerSetupImpl.updateLogins():

String user = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug("javax.persistence.jdbc.user", m, session);
String password = EntityManagerFactoryProvider.getConfigPropertyAsStringLogDebug("javax.persistence.jdbc.password", m, session);
if(user != null)
login.setUserName(user);
if(password != null)
login.setPassword(securableObjectHolder.getSecurableObject().decryptPassword(password));


so the JCEEncryptor failes miserably trying to unencrypt a password...
So I need to AES-encrypt mypassword :


import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.eclipse.persistence.internal.helper.Helper;

public class AESEncryptor {
 public static void main(String[] args) {
  System.out.println(AESEncryptor.encryptPassword("mypassword"));
 }

 private static class Synergizer
 {
  private static SecretKey getAESMultitasker()
  throws Exception
  {
   return new SecretKeySpec(Helper.buildBytesFromHexString("3E7CFEF156E712906E1F603B59463C67"), "AES");
  }

  private Synergizer()
  {
  }
 }


 public static String encryptPassword(String password)
 {
  try
  {
   Cipher encryptCipherAES = Cipher.getInstance("AES/ECB/PKCS5Padding");
   encryptCipherAES.init(1, Synergizer.getAESMultitasker());
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   CipherOutputStream cos = new CipherOutputStream(baos, encryptCipherAES);
   ObjectOutputStream oos = new ObjectOutputStream(cos);
   oos.writeObject(password);
   oos.flush();
   oos.close();
   return Helper.buildHexStringFromBytes(baos.toByteArray());
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
  return null;
 } 

}



for i in 1..1000000000:
print "I hate Eclipse"

4 comments:

parfito said...

Thank you very much for this post; i'm trying to go through this since yesterday.
i used your AESEncryptor class to encrypt my password and i replace my password in the persistence.xml by the encrypted one but it does not work. i got this:

[EL Info]: 2012-05-03 10:35:01.153--ServerSession(20355494)--EclipseLink, version: Eclipse Persistence Services - 2.3.2.v20111125-r10461
[EL Severe]: 2012-05-03 10:35:01.781--ServerSession(20355494)--[EL Severe]: 2012-05-03 10:35:01.781--ServerSession(20355494)--java.lang.NullPointerException
Exception in thread "main" javax.persistence.PersistenceException: java.lang.NullPointerException
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:517)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:294)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:272)
at org.persistence.Main.main(Main.java:16)
Caused by: java.lang.NullPointerException
at java.util.ResourceBundle.getBundle(Unknown Source)
at org.eclipse.persistence.exceptions.i18n.ExceptionMessageGenerator.getHeader(ExceptionMessageGenerator.java:95)
at org.eclipse.persistence.exceptions.EclipseLinkException.toString(EclipseLinkException.java:253)
at org.eclipse.persistence.logging.DefaultSessionLog.log(DefaultSessionLog.java:151)
at org.eclipse.persistence.internal.sessions.AbstractSession.log(AbstractSession.java:3100)
at org.eclipse.persistence.internal.sessions.AbstractSession.logThrowable(AbstractSession.java:4293)
at org.eclipse.persistence.internal.sessions.AbstractSession.handleSevere(AbstractSession.java:4422)
at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.connect(DatasourceAccessor.java:421)
at org.eclipse.persistence.sessions.server.ConnectionPool.buildConnection(ConnectionPool.java:216)
at org.eclipse.persistence.sessions.server.ConnectionPool.startUp(ConnectionPool.java:504)
at org.eclipse.persistence.sessions.server.ServerSession.connect(ServerSession.java:484)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl.java:633)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:208)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:488)
... 5 more

vernetto said...

it worked here...
make sure you also provide platform.oracle in the persistence.xml.

but honestly I am giving up on Eclipse JPA, it's simply too problematic.... I will use traditional javax.sql classes and hard code SQL. Eclipse = CRAP.

vernetto said...

incidentally the NullPointerException in the getBundle needs some other fix... otherwise you can't see the error message.
Anyway I got so disgusted by Eclipse that I promised myself to steer away from it for some time.

vernetto said...

try generating wlfullclient.jar and putting it in the classpath - after removing everything else...

http://www.javamonamour.org/2011/10/wlfullclientjar.html