Skip to content

Commit

Permalink
Improve exception handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
obraliar committed Aug 29, 2016
1 parent fca9962 commit f459b5c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.security.GeneralSecurityException;
import java.sql.SQLException;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -43,8 +44,13 @@
import net.sf.jabref.shared.prefs.SharedDatabasePreferences;
import net.sf.jabref.shared.security.Password;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class OpenSharedDatabaseDialog extends JDialog {

private static final Log LOGGER = LogFactory.getLog(Password.class);

private final JabRefFrame frame;

private final GridBagLayout gridBagLayout = new GridBagLayout();
Expand Down Expand Up @@ -203,7 +209,11 @@ private void applyPreferences() {
}

if (sharedDatabasePassword.isPresent() && sharedDatabaseUser.isPresent()) {
passwordField.setText(new Password(sharedDatabasePassword.get(), sharedDatabaseUser.get()).decrypt());
try {
passwordField.setText(new Password(sharedDatabasePassword.get(), sharedDatabaseUser.get()).decrypt());
} catch (GeneralSecurityException e) {
LOGGER.error("Could not read password due to decryption problems.");
}
}

rememberPassword.setSelected(sharedDatabaseRememberPassword);
Expand Down Expand Up @@ -320,7 +330,11 @@ public void setPreferences() {
prefs.setUser(userField.getText());

if (rememberPassword.isSelected()) {
prefs.setPassword(new Password(new String(passwordField.getPassword()), userField.getText()).encrypt());
try {
prefs.setPassword(new Password(new String(passwordField.getPassword()), userField.getText()).encrypt());
} catch (GeneralSecurityException e) {
LOGGER.error("Could not store password due to encryption problems.");
}
} else {
prefs.clearPassword(); // for the case that the password is already set
}
Expand Down
31 changes: 8 additions & 23 deletions src/main/java/net/sf/jabref/shared/security/Password.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* {@link Password} contains methods which are useful to encrypt and decrypt passwords using symetric algorithms.
*/
public class Password {

private static final Log LOGGER = LogFactory.getLog(Password.class);

private static final String ALGORITHM = "AES";
private static final String STATIC_KEY = "ThisIsA128bitKey";
private final byte[] key;
Expand All @@ -36,31 +31,21 @@ public Password(String phrase, String key) {
*
* @return Encrypted phrase/password
*/
public String encrypt() {
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
return Base64.getEncoder().encodeToString(cipher.doFinal(phrase.getBytes()));
} catch (GeneralSecurityException e) {
LOGGER.error("Encryption error", e);
return "";
}
public String encrypt() throws GeneralSecurityException {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
return Base64.getEncoder().encodeToString(cipher.doFinal(phrase.getBytes()));
}

/**
* Decrypts the set phrase/password which was encrypted via {@link Password#encrypt()}.
*
* @return Decrypted phrase/password
*/
public String decrypt() {
try {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
return new String(cipher.doFinal(Base64.getDecoder().decode(phrase)));
} catch (GeneralSecurityException e) {
LOGGER.error("Decryption error", e);
return "";
}
public String decrypt() throws GeneralSecurityException {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
return new String(cipher.doFinal(Base64.getDecoder().decode(phrase)));
}

/**
Expand Down

0 comments on commit f459b5c

Please sign in to comment.