Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to connect DB2 database using java,Below the driver and connection string details i am giving,

Class.forName("com.ibm.db2.jcc.DB2Driver");

DB2DataSource db2ds = new DB2DataSource();
db2ds.setServerName("servername");
db2ds.setPortNumber(portnumber);
db2ds.setDatabaseName(databasename);
db2ds.setUser(username);
db2ds.setPassword(password);
db2ds.setSecurityMechanism(DB2BaseDataSource.ENCRYPTED_USER_AND_DATA_SECURITY);
db2ds.setDriverType(4);    
sourceConnection=db2ds.getConnection();

for this connection i have added the below jars

1)db2jcc.jar 
2)db2jcc_license_cu.jar

But i am getting the below error,

com.ibm.db2.jcc.a.SqlException: java.security.InvalidAlgorithmParameterException is caught when initializing EncryptionManager 'Prime size must be multiple of 64, and can only range from 512 to 1024 (inclusive)'
at com.ibm.db2.jcc.a.x.<init>(x.java:89)
at com.ibm.db2.jcc.b.b.lc(b.java:2353)
at com.ibm.db2.jcc.b.b.A(b.java:1190)
at com.ibm.db2.jcc.b.b.b(b.java:759)
at com.ibm.db2.jcc.b.b.a(b.java:725)
at com.ibm.db2.jcc.b.b.a(b.java:430)
at com.ibm.db2.jcc.b.b.<init>(b.java:374)
at com.ibm.db2.jcc.DB2DataSource.getSimpleConnection(DB2DataSource.java:87)
at com.ibm.db2.jcc.DB2DataSource.getConnection(DB2DataSource.java:65)
at com.ibm.db2.jcc.DB2DataSource.getConnection(DB2DataSource.java:47)
at TestDB2.main(TestDB2.java:73)

I have tried different security mechanism but still i am getting the same error.the below options i have tried for security mechanism,

1) ENCRYPTED_USER_AND_DATA_SECURITY 
2) CLEAR_TEXT_PASSWORD_SECURITY 
3) ENCRYPTED_PASSWORD_SECURITY 
4) ENCRYPTED_USER_AND_PASSWORD_SECURITY
5)ENCRYPTED_USER_PASSWORD_AND_DATA_SECURITY

And I am using JDK 1.6.0 version for developing the code

Please guide on what needs to be done for successful connection.

Thanks in advance.

share|improve this question

1 Answer 1

You will have to use a SecurityProvider. BouncyCastleProvider can be a good option for you. All you have to do is, as a first step in getConnection() method use below line:

 Security.addProvider(new BouncyCastleProvider());
share|improve this answer
    
Hi, Thanks for the response. i am not able to add the provider in security file, so i have copied the file and paste it some other place and add the provider into security file, than i have added below piece of code in my old code , import org.bouncycastle.jce.provider.BouncyCastleProvider; ..... Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); –  mick_000 Jun 20 '13 at 12:44
    
And we added the security file location in the VM arguments in eclipse. -Djava.security.policy=\java.security now i got the same error, so i have encrypted my password using the below code and then i have set this password into datasource. This time also i got the same error. "com.ibm.db2.jcc.a.SqlException: java.security.InvalidAlgorithmParameterException is caught when initializing EncryptionManager 'Prime size must be multiple of 64, and can only range from 512 to 1024 (inclusive)'" –  mick_000 Jun 20 '13 at 12:45
    
final String AES_ALGORITHM = "AES/CTR/NoPadding"; final String PROVIDER = BouncyCastleProvider.PROVIDER_NAME; final byte[] AES_KEY_128 = { // Hard coded for now 78, -90, 42, 70, -5, 20, -114, 103, -99, -25, 76, 95, -85, 94, 57, 54}; final byte[] IV = { // Hard coded for now -85, -67, -5, 88, 28, 49, 49, 85, 114, 83, -40, 119, -65, 91, 76, 108}; final SecretKeySpec secretKeySpec = new SecretKeySpec(AES_KEY_128, "AES"); final IvParameterSpec ivSpec = new IvParameterSpec(IV); –  mick_000 Jun 20 '13 at 12:46
    
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); Cipher cipher = Cipher.getInstance(AES_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec); byte[] encrypted = cipher.doFinal("Password".getBytes()); db2ds.setPassword(encrypted.toString()); Can you please guide me on how to proceed further? –  mick_000 Jun 20 '13 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.