From c303b3f80717a87c187799f571b6ee1262d93948 Mon Sep 17 00:00:00 2001 From: Björn Hagemeister Date: Wed, 1 Oct 2014 16:11:17 +0200 Subject: Exception handling. --- .../util/EncryptWithServerIdPublicKey.java | 74 +++++++++++++++------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/openslx/satellitedaemon/util/EncryptWithServerIdPublicKey.java b/src/main/java/org/openslx/satellitedaemon/util/EncryptWithServerIdPublicKey.java index 9a064f8..2e4c925 100644 --- a/src/main/java/org/openslx/satellitedaemon/util/EncryptWithServerIdPublicKey.java +++ b/src/main/java/org/openslx/satellitedaemon/util/EncryptWithServerIdPublicKey.java @@ -18,36 +18,64 @@ import java.security.UnrecoverableKeyException; import java.security.cert.Certificate; import java.security.cert.CertificateException; +import org.apache.log4j.Logger; + // TODO: More general naming; this isn't really limited to serverids... -// Might also be worth moving this encrypt/decrypt stuff from satserver and masterserver to the shared project (one class doing both) +// Might also be worth moving this encrypt/decrypt stuff from satserver and masterserver to the +// shared project (one class doing both) + +public class EncryptWithServerIdPublicKey +{ -public class EncryptWithServerIdPublicKey { + private static Logger log = Logger.getLogger( EncryptWithServerIdPublicKey.class ); KeyPair pair; - public EncryptWithServerIdPublicKey(String alias, String password, String file) - throws NoSuchAlgorithmException, CertificateException, - FileNotFoundException, IOException, KeyStoreException, - UnrecoverableKeyException { - KeyStore keystore = KeyStore.getInstance("JKS"); - keystore.load(new FileInputStream(new File(file)), - password.toCharArray()); - Certificate cert = null; - - Key key = keystore.getKey(alias, password.toCharArray()); - - if (key instanceof PrivateKey) { - cert = keystore.getCertificate(alias); - PublicKey publicKey = cert.getPublicKey(); - pair = new KeyPair(publicKey, (PrivateKey) key); + public EncryptWithServerIdPublicKey( String alias, String password, String file ) + { + try { + KeyStore keystore = KeyStore.getInstance( "JKS" ); + keystore.load( new FileInputStream( new File( file ) ), + password.toCharArray() ); + Certificate cert = null; + + Key key = keystore.getKey( alias, password.toCharArray() ); + + if ( key instanceof PrivateKey ) { + cert = keystore.getCertificate( alias ); + PublicKey publicKey = cert.getPublicKey(); + pair = new KeyPair( publicKey, (PrivateKey)key ); + } + } catch ( KeyStoreException e ) { + log.error( "KeystoreException.", e ); + } catch ( FileNotFoundException e ) { + log.error( "File " + file + " not found.", e ); + } catch ( UnrecoverableKeyException e ) { + log.error( "UnrecoverableKeyException", e ); + } catch ( CertificateException e ) { + log.error( "CertificateException", e ); + } catch ( NoSuchAlgorithmException e ) { + log.error( "NoSuchAlgorithmException", e ); + } catch ( IOException e ) { + log.error( "IOException", e ); } } - public byte[] encryptString(String message) throws NoSuchAlgorithmException, - InvalidKeyException, SignatureException { - Signature signature = Signature.getInstance("SHA256WITHRSA"); - signature.initSign(pair.getPrivate()); - signature.update(message.getBytes()); - return signature.sign(); + public byte[] encryptString( String message ) + { + Signature signature = null; + try { + signature = Signature.getInstance( "SHA256WITHRSA" ); + signature.initSign( pair.getPrivate() ); + signature.update( message.getBytes() ); + return signature.sign(); + } catch ( NoSuchAlgorithmException e ) { + log.error( "NoSuchAlgorithmException", e ); + } catch ( InvalidKeyException e ) { + log.error( "InvalidKeyException", e ); + } catch ( SignatureException e ) { + log.error( "SignatureException", e ); + } + return null; } } -- cgit v1.2.3-55-g7522