private void save() {
    Preferences pref = Preferences
        .userNodeForPackage(ConnectionDialog.class);

    pref.put("host", getHost());
    pref.put("user", getUser());

    boolean remember = getRememberPasswordCheckBox().isSelected();
    if (remember) {
      try {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        KeyGenerator keygen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(new byte[]{
          0x60, (byte) 0xFD, 0x28, 0x07, 0x35, 0x4A, 0x0D, 0x3B
        });
        byte[] iv = new byte[16];
        random.nextBytes(iv);
        IvParameterSpec spec = new IvParameterSpec(iv);
        keygen.init(128, random);
        cipher.init(Cipher.ENCRYPT_MODE, keygen.generateKey(), spec);
        byte[] clearText = getPassword().getBytes();
        byte[] cipherText = cipher.doFinal(clearText);
        pref.putByteArray("password", cipherText);
        pref.putBoolean("remember", true);
      } catch (Exception e) {
        logger.error("Could not encrypt password", e);
        pref.remove("password");
        pref.putBoolean("remember", false);
      }
    } else {
      pref.remove("password");
      pref.putBoolean("remember", false);
    }
  }
private void save ( ) { Preferences pref = Preferences . userNodeForPackage ( ConnectionDialog . class ) ; pref . put ( "host" , getHost ( ) ) ; pref . put ( "user" , getUser ( ) ) ; boolean remember = getRememberPasswordCheckBox ( ) . isSelected ( ) ; if ( remember ) { try { Cipher cipher = Cipher . getInstance ( "AES/CBC/PKCS5Padding" ) ; KeyGenerator keygen = KeyGenerator . getInstance ( "AES" ) ; SecureRandom random = SecureRandom . getInstance ( "SHA1PRNG" ) ; random . setSeed ( new byte [ ] { 0x60 , ( byte ) 0xFD , 0x28 , 0x07 , 0x35 , 0x4A , 0x0D , 0x3B } ) ; byte [ ] iv = new byte [ 16 ] ; random . nextBytes ( iv ) ; IvParameterSpec spec = new IvParameterSpec ( iv ) ; keygen . init ( 128 , random ) ; cipher . init ( Cipher . ENCRYPT_MODE , keygen . generateKey ( ) , spec ) ; byte [ ] clearText = getPassword ( ) . getBytes ( ) ; byte [ ] cipherText = cipher . doFinal ( clearText ) ; pref . putByteArray ( "password" , cipherText ) ; pref . putBoolean ( "remember" , true ) ; } catch ( Exception e ) { logger . error ( "Could not encrypt password" , e ) ; pref . remove ( "password" ) ; pref . putBoolean ( "remember" , false ) ; } } else { pref . remove ( "password" ) ; pref . putBoolean ( "remember" , false ) ; } }
Saves field values to preferences.