Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

getKey throws an exception on multi-thread env even if it is synchronized

843811May 7 2009 — edited May 20 2009
Hello,

I got the following exception when I used KeyStore#getKey method on multi-thread environment.

java.security.UnrecoverableKeyException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_z.a(DashoA13*..)
at com.sun.crypto.provider.JceKeyStore.engineGetKey(DashoA13*..)
at java.security.KeyStore.getKey(KeyStore.java:763)
at KeyStoreTest.run(KeyStoreTest.java:43)
at java.lang.Thread.run(Thread.java:619)

This is similar to the topic "Is KeyStore thread safe?"(Jan 29, 2008 8:40 PM), I suppose.
In the topic, KeyStore#getKey should be synchronized because it isn't thread safe.
Therefore I synchronized it, but this problem still occurred.


I used the following program for a duration test.
About in 1 hour, the exception always occurs.

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.Key;
import java.security.KeyStore;

public class KeyStoreTest implements Runnable {

public final static String KSFILE = ".\\.jcekeystore"; // Please change here. Your "JCEKS" file name.
public static String sLock = "lock";

public static void main(String[] args) {
Thread[] threads;
int count = 20;
while (true) {
threads = new Thread[count];
for (int i = 0; i < count; i++) {
threads[i] = new Thread(new KeyStoreTest());
threads.start();
}
for (int i = 0; i < count; i++) {
if (threads[i].isAlive()) {
try {
threads[i].join();
} catch (InterruptedException e) {}
}
}
}
}

public void run() {
InputStream is = null;
try {
is = new FileInputStream(KSFILE);
Key key;
synchronized (sLock) {
//System.out.print("start - ");
KeyStore store = KeyStore.getInstance("jceks");
store.load(is, "12345678".toCharArray()); // Please change here. PIN code.
String alias = "alias"; // Please change here. Alias.
String keypassed = "keypasswd"; // Please change here. Key Password.
key = store.getKey(alias, keypassed.toCharArray());
//System.out.println("end");
}
System.out.println(key.getAlgorithm());
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(0);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {}
is = null;
}
}
}
}

I think there's no problem in my test program.
Do you have any information about the problem?

My test environment is below:
Java : jre1.6.0 update 13
OS : Windows XP


I tested this code with jre1.5.0 update 18, too.
But I didn't get the exception.

Thanks,
Masanori Hayashi

Comments

darrylburke
Probably not very efficient, but seems to work ... fairly clean imho :-)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.plaf.basic.BasicTextPaneUI;
import javax.swing.text.BadLocationException;

public class LineHighlightTextPane {
   
   public LineHighlightTextPane () {
   }
   
   void makeUI () {
      
      JTextPane textPane = new JTextPane ();
      textPane.setUI (new LineHighlightTextPaneUI (textPane));
      
      JScrollPane scrollPane = new JScrollPane (textPane);
      
      JFrame frame = new JFrame ("Line Highlight Text Pane");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
      frame.setSize (300, 300);
      
      frame.add (scrollPane, BorderLayout.CENTER);
      frame.setVisible (true);
   }
   
   public static void main (String[] args) {
      SwingUtilities.invokeLater (new Runnable () {
         public void run () {
            new LineHighlightTextPane ().makeUI ();
         }
      });
   }
}

class LineHighlightTextPaneUI extends BasicTextPaneUI {
   
   JTextPane tc;
   
   LineHighlightTextPaneUI (JTextPane t) {
      
      tc = t;
      tc.addCaretListener (new CaretListener () {
         public void caretUpdate (CaretEvent e) {
            
            tc.repaint ();
         }
      });
   }
   
   @Override
   public void paintBackground (Graphics g) {
      
      super.paintBackground (g);
      
      try {
         Rectangle rect = modelToView(tc, tc.getCaretPosition ());
         int y = rect.y;
         int h = rect.height;
         g.setColor (Color.YELLOW);
         g.fillRect (0, y, tc.getWidth (), h);
      } catch (BadLocationException ex) {
         ex.printStackTrace();
      }
   }
}
Is that what you wanted or have I misread the requirement?

db
camickr
Or some code that should work on any JTextComponent:

http://www.discoverteenergy.com/files/LinePainter.java
843806
Thanks a lot for the help guys! I was able to do exactly what I needed.
1 - 3
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 17 2009
Added on May 7 2009
4 comments
2,926 views