synchronizing access to static variables

Discussion in 'Java' started by sosecret2006, Dec 26, 2006.

  1. sosecret2006

    sosecret2006 Guest

    I copied and pasted some DES crypto code from the internet. I am trying
    to transform the class to be more utility-based instead of having to
    instantiate a copy each time. I am not great at Java threads. Can you
    please examine the (small amount of) code below and tell me if the
    synchronization is optimal? Please feel free to make any other comments
    about how I can improve the class.

    public final class DESCrypto {
    private static final Log logger =
    LogFactory.getLog(DESCrypto.class);
    private static final String DES = "DES";
    private static final String UTF8 = "UTF8";

    private static final Cipher ecipher;
    private static final Cipher dcipher;

    private DESCrypto() {
    throw new UnsupportedOperationException("do not use");
    }

    static {
    try {
    // load the stored key
    InputStream keyis =
    DESCrypto.class.getResourceAsStream("DESCrypto.key");
    ByteArrayOutputStream byteos = new ByteArrayOutputStream();
    int b;
    while ((b = keyis.read()) != -1) {
    byteos.write(b);
    }
    SecretKey key = new SecretKeySpec(byteos.toByteArray(),
    DES);

    // to generate a new key, use:
    // SecretKey key = KeyGenerator.getInstance("DES").generateKey();
    // byte[] keyout = key.getEncoded();
    // FileOutputStream keyfos = new
    FileOutputStream("DesEncrypter.key");
    // keyfos.write(keyout);
    // keyfos.close();

    // create and initialize ciphers
    ecipher = Cipher.getInstance(DES);
    dcipher = Cipher.getInstance(DES);
    ecipher.init(Cipher.ENCRYPT_MODE, key);
    dcipher.init(Cipher.DECRYPT_MODE, key);
    } catch (Exception ex) {
    logger.error(ex);
    throw new RuntimeException(ex);
    }
    }

    public static String encrypt(String str) {
    try {
    synchronized (DESCrypto.class) {
    byte[] utf8 = str.getBytes(UTF8);
    byte[] enc = ecipher.doFinal(utf8);
    return new sun.misc.BASE64Encoder().encode(enc);
    }
    } catch (Exception ex) {
    logger.error(ex);
    throw new RuntimeException(ex);
    }
    }

    public static String decrypt(String str) {
    try {
    synchronized (DESCrypto.class) {
    byte[] dec = new
    sun.misc.BASE64Decoder().decodeBuffer(str);
    byte[] utf8 = dcipher.doFinal(dec);
    return new String(utf8, UTF8);
    }
    } catch (Exception ex) {
    logger.error(ex);
    throw new RuntimeException(ex);
    }
    }
    }
    sosecret2006, Dec 26, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rog
    Replies:
    1
    Views:
    383
    Eric Marvets
    May 22, 2004
  2. Christopher D. Wiederspan

    Synchronizing ASP.NET Content Across WebFarm

    Christopher D. Wiederspan, Jul 30, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    452
    Yan-Hong Huang[MSFT]
    Aug 1, 2003
  3. Paul Tomblin

    synchronizing static methods?

    Paul Tomblin, Jul 10, 2003, in forum: Java
    Replies:
    1
    Views:
    4,387
    John C. Bollinger
    Jul 10, 2003
  4. dlugi
    Replies:
    9
    Views:
    353
    Nigel Wade
    Oct 4, 2006
  5. Neviton
    Replies:
    9
    Views:
    446
    Alf P. Steinbach
    Sep 17, 2007
Loading...

Share This Page