Guys,
I have been trying to connect to a Sybase 15 server which requires encrypted passwords.
I wrote a small method to encrypt the password,
public static String encrypt(String text) throws Exception {
String rValue = text;
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] input = text.getBytes();
Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding");
SecureRandom random = new SecureRandom();
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(512, random);
KeyPair pair = generator.generateKeyPair();
Key pubKey = pair.getPublic();
Key privKey = pair.getPrivate();
cipher.init(Cipher.ENCRYPT_MODE, pubKey, random);
byte[] cipherText = cipher.doFinal(input);
rValue = new String(cipherText);
System.out.println("cipher: " + rValue);
return rValue;
}
I tried to pass the password as mentioned in the sybase site.
[Sybase Online|http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc20155.1500/html/newfesd/newfesd95.htm]
/**
* Obtain JDBC connection for operation
* @return
* @throws Exception
*/
public static Connection getConnection() throws Exception {
try {
Class.forName("com.sybase.jdbc3.jdbc.SybDriver");
Properties props = new Properties();
props.put("ENCRYPT_PASSWORD", "false");
props.put("RETRY_WITH_NO_ENCRYPTION", "true");
props.put("JCE_PROVIDER_CLASS", "org.bouncycastle.jce.provider.BouncyCastleProvider");
/* Set up additional connnection properties as needed */
props.put("user", USER_NAME);
props.put("password", encrypt(PASSWORD));
DriverManager.setLogStream(System.out);
/* get the connection */
//URL = jdbc:sybase:Tds:host:port
Connection con = DriverManager.getConnection(JDBC_URL, props);
return con;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
If i connect I get the following error,
DriverManager.getConnection("jdbc:sybase:Tds:host:port")//modified original server name
trying driver[className=sun.jdbc.odbc.JdbcOdbcDriver,sun.jdbc.odbc.JdbcOdbcDriver@13a328f]
*Driver.connect (jdbc:sybase:Tds:host:port)
trying driver[className=com.sybase.jdbc3.jdbc.SybDriver,com.sybase.jdbc3.jdbc.SybDriver@1cd8669]
SQLWarning: reason(Adaptive Server requires encryption of the login password on the network.
) SQLState(01ZZZ) vendor code(1640)
SQLWarning: reason(Adaptive Server requires encryption of the login password on the network.
) SQLState(01ZZZ) vendor code(1640)
SQLWarning: reason(Login failed.
) SQLState(01ZZZ) vendor code(4002)
SQLWarning: reason(Login failed.
) SQLState(01ZZZ) vendor code(4002)
java.sql.SQLException: JZ00L: Login failed. Examine the SQLWarnings chained to this exception for the reason(s).
at com.sybase.jdbc3.jdbc.ErrorMessage.raiseError(Unknown Source)
at com.sybase.jdbc3.tds.Tds.for(Unknown Source)
at com.sybase.jdbc3.tds.Tds.a(Unknown Source)
at com.sybase.jdbc3.tds.Tds.login(Unknown Source)
at com.sybase.jdbc3.jdbc.SybConnection.a(Unknown Source)
at com.sybase.jdbc3.jdbc.SybConnection.a(Unknown Source)
at com.sybase.jdbc3.jdbc.SybConnection.<init>(Unknown Source)
at com.sybase.jdbc3.jdbc.SybConnection.<init>(Unknown Source)
at com.sybase.jdbc3.jdbc.SybDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at TestDBSybase.getConnection(TestDBSybase.java:100)
at TestDBSybase.main(TestDBSybase.java:57)
SQLException: SQLState(JZ00L)
SQLWarning: reason(Adaptive Server requires encryption of the login password on the network.
) SQLState(01ZZZ) vendor code(1640)
SQLWarning: reason(Login failed.
) SQLState(01ZZZ) vendor code(4002)
getConnection failed: java.sql.SQLException: JZ00L: Login failed. Examine the SQLWarnings chained to this exception for the reason(s).
java.sql.SQLException: JZ00L: Login failed. Examine the SQLWarnings chained to this exception for the reason(s).
at com.sybase.jdbc3.jdbc.ErrorMessage.raiseError(Unknown Source)
at com.sybase.jdbc3.tds.Tds.for(Unknown Source)
at com.sybase.jdbc3.tds.Tds.a(Unknown Source)
at com.sybase.jdbc3.tds.Tds.login(Unknown Source)
at com.sybase.jdbc3.jdbc.SybConnection.a(Unknown Source)
at com.sybase.jdbc3.jdbc.SybConnection.a(Unknown Source)
at com.sybase.jdbc3.jdbc.SybConnection.<init>(Unknown Source)
at com.sybase.jdbc3.jdbc.SybConnection.<init>(Unknown Source)
at com.sybase.jdbc3.jdbc.SybDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at TestDBSybase.getConnection(TestDBSybase.java:100)
at TestDBSybase.main(TestDBSybase.java:57)
No JDBC Connection!
Can some one please assist me on this? How do we connect to sybase 15 using jdbc. I am running on JDK 1.6