Add encryption to account data - thx ThatGamerBlue

This commit is contained in:
Kyleeld
2019-04-24 02:02:13 +01:00
committed by GitHub
parent b5f4265012
commit 65f4b33f62
4 changed files with 864 additions and 443 deletions

View File

@@ -1,15 +1,37 @@
/* /*
* Decompiled with CFR 0.139. * Copyright (c) 2019, Spedwards <https://github.com/Spedwards>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package net.runelite.client.plugins.profiles; package net.runelite.client.plugins.profiles;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Color; import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JLabel; import javax.swing.JLabel;
@@ -17,58 +39,89 @@ import javax.swing.JPanel;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.border.CompoundBorder; import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.api.GameState; import net.runelite.api.GameState;
import net.runelite.client.ui.ColorScheme; import net.runelite.client.ui.ColorScheme;
import net.runelite.client.util.ImageUtil; import net.runelite.client.util.ImageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class ProfilePanel @Slf4j
extends JPanel { class ProfilePanel extends JPanel
private static final Logger log = LoggerFactory.getLogger(ProfilePanel.class); {
private static final ImageIcon DELETE_ICON; private static final ImageIcon DELETE_ICON;
private static final ImageIcon DELETE_HOVER_ICON; private static final ImageIcon DELETE_HOVER_ICON;
static
{
final BufferedImage deleteImg = ImageUtil.getResourceStreamFromClass(ProfilesPlugin.class, "delete_icon.png");
DELETE_ICON = new ImageIcon(deleteImg);
DELETE_HOVER_ICON = new ImageIcon(ImageUtil.alphaOffset(deleteImg, -100));
}
private final String loginText; private final String loginText;
private final ProfilesPanel parent;
private String password = null; private String password = null;
ProfilePanel(final Client client, final String data, final ProfilesConfig config) { ProfilePanel(final Client client, String data, ProfilesConfig config, ProfilesPanel parent)
{
this.parent = parent;
String[] parts = data.split(":"); String[] parts = data.split(":");
this.loginText = parts[1]; this.loginText = parts[1];
if (parts.length == 3) { if (parts.length == 3)
{
this.password = parts[2]; this.password = parts[2];
} }
final ProfilePanel panel = this; final ProfilePanel panel = this;
this.setLayout(new BorderLayout());
this.setBackground(ColorScheme.DARKER_GRAY_COLOR); setLayout(new BorderLayout());
setBackground(ColorScheme.DARKER_GRAY_COLOR);
JPanel labelWrapper = new JPanel(new BorderLayout()); JPanel labelWrapper = new JPanel(new BorderLayout());
labelWrapper.setBackground(ColorScheme.DARKER_GRAY_COLOR); labelWrapper.setBackground(ColorScheme.DARKER_GRAY_COLOR);
labelWrapper.setBorder(new CompoundBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, ColorScheme.DARK_GRAY_COLOR), BorderFactory.createLineBorder(ColorScheme.DARKER_GRAY_COLOR))); labelWrapper.setBorder(new CompoundBorder(
BorderFactory.createMatteBorder(0, 0, 1, 0, ColorScheme.DARK_GRAY_COLOR),
BorderFactory.createLineBorder(ColorScheme.DARKER_GRAY_COLOR)
));
JPanel panelActions = new JPanel(new BorderLayout(3, 0)); JPanel panelActions = new JPanel(new BorderLayout(3, 0));
panelActions.setBorder(new EmptyBorder(0, 0, 0, 8)); panelActions.setBorder(new EmptyBorder(0, 0, 0, 8));
panelActions.setBackground(ColorScheme.DARKER_GRAY_COLOR); panelActions.setBackground(ColorScheme.DARKER_GRAY_COLOR);
final JLabel delete = new JLabel();
JLabel delete = new JLabel();
delete.setIcon(DELETE_ICON); delete.setIcon(DELETE_ICON);
delete.setToolTipText("Delete account profile"); delete.setToolTipText("Delete account profile");
delete.addMouseListener(new MouseAdapter(){ delete.addMouseListener(new MouseAdapter()
{
@Override @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e)
{
panel.getParent().remove(panel); panel.getParent().remove(panel);
ProfilesPanel.removeProfile(data); try
{
parent.removeProfile(data);
}
catch (InvalidKeySpecException | NoSuchAlgorithmException ex)
{
ex.printStackTrace();
}
} }
@Override @Override
public void mouseEntered(MouseEvent e) { public void mouseEntered(MouseEvent e)
{
delete.setIcon(DELETE_HOVER_ICON); delete.setIcon(DELETE_HOVER_ICON);
} }
@Override @Override
public void mouseExited(MouseEvent e) { public void mouseExited(MouseEvent e)
{
delete.setIcon(DELETE_ICON); delete.setIcon(DELETE_ICON);
} }
}); });
panelActions.add((Component)delete, "East");
panelActions.add(delete, BorderLayout.EAST);
JLabel label = new JLabel(); JLabel label = new JLabel();
label.setText(parts[0]); label.setText(parts[0]);
label.setBorder(null); label.setBorder(null);
@@ -76,48 +129,50 @@ extends JPanel {
label.setPreferredSize(new Dimension(0, 24)); label.setPreferredSize(new Dimension(0, 24));
label.setForeground(Color.WHITE); label.setForeground(Color.WHITE);
label.setBorder(new EmptyBorder(0, 8, 0, 0)); label.setBorder(new EmptyBorder(0, 8, 0, 0));
labelWrapper.add((Component)label, "Center");
labelWrapper.add((Component)panelActions, "East");
label.addMouseListener(new MouseAdapter(){
labelWrapper.add(label, BorderLayout.CENTER);
labelWrapper.add(panelActions, BorderLayout.EAST);
label.addMouseListener(new MouseAdapter()
{
@Override @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e)
if (SwingUtilities.isLeftMouseButton(e) && client.getGameState() == GameState.LOGIN_SCREEN) { {
client.setUsername(ProfilePanel.this.loginText); if (SwingUtilities.isLeftMouseButton(e) && client.getGameState() == GameState.LOGIN_SCREEN)
if (config.rememberPassword() && ProfilePanel.this.password != null) { {
client.setPassword(ProfilePanel.this.password); client.setUsername(loginText);
if (config.rememberPassword() && password != null)
{
client.setPassword(password);
} }
} }
} }
}); });
JPanel bottomContainer = new JPanel(new BorderLayout()); JPanel bottomContainer = new JPanel(new BorderLayout());
bottomContainer.setBorder(new EmptyBorder(8, 0, 8, 0)); bottomContainer.setBorder(new EmptyBorder(8, 0, 8, 0));
bottomContainer.setBackground(ColorScheme.DARKER_GRAY_COLOR); bottomContainer.setBackground(ColorScheme.DARKER_GRAY_COLOR);
bottomContainer.addMouseListener(new MouseAdapter(){ bottomContainer.addMouseListener(new MouseAdapter()
{
@Override @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e)
if (SwingUtilities.isLeftMouseButton(e) && client.getGameState() == GameState.LOGIN_SCREEN) { {
client.setUsername(ProfilePanel.this.loginText); if (SwingUtilities.isLeftMouseButton(e) && client.getGameState() == GameState.LOGIN_SCREEN)
{
client.setUsername(loginText);
} }
} }
}); });
JLabel login = new JLabel(); JLabel login = new JLabel();
login.setText(config.isStreamerMode() ? "Hidden email" : this.loginText); login.setText(config.isStreamerMode() ? "Hidden email" : loginText);
login.setBorder(null); login.setBorder(null);
login.setPreferredSize(new Dimension(0, 24)); login.setPreferredSize(new Dimension(0, 24));
login.setForeground(Color.WHITE); login.setForeground(Color.WHITE);
login.setBorder(new EmptyBorder(0, 8, 0, 0)); login.setBorder(new EmptyBorder(0, 8, 0, 0));
bottomContainer.add((Component)login, "Center");
this.add((Component)labelWrapper, "North");
this.add((Component)bottomContainer, "Center");
}
static { bottomContainer.add(login, BorderLayout.CENTER);
BufferedImage deleteImg = ImageUtil.getResourceStreamFromClass(ProfilesPlugin.class, "net/runelite/client/plugins/profiles/delete_icon.png");
DELETE_ICON = new ImageIcon(deleteImg);
DELETE_HOVER_ICON = new ImageIcon(ImageUtil.alphaOffset(deleteImg, -100));
}
add(labelWrapper, BorderLayout.NORTH);
add(bottomContainer, BorderLayout.CENTER);
}
} }

View File

@@ -1,5 +1,26 @@
/* /*
* Decompiled with CFR 0.139. * Copyright (c) 2019, Spedwards <https://github.com/Spedwards>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package net.runelite.client.plugins.profiles; package net.runelite.client.plugins.profiles;
@@ -7,30 +28,74 @@ import net.runelite.client.config.Config;
import net.runelite.client.config.ConfigGroup; import net.runelite.client.config.ConfigGroup;
import net.runelite.client.config.ConfigItem; import net.runelite.client.config.ConfigItem;
@ConfigGroup(value="profiles") @ConfigGroup("profiles")
public interface ProfilesConfig public interface ProfilesConfig extends Config
extends Config { {
@ConfigItem(keyName="profilesData", name="", description="", hidden=true)
default public String profilesData() {
@ConfigItem(
keyName = "profilesData",
name = "",
description = "",
hidden = true
)
default String profilesData()
{
return ""; return "";
} }
@ConfigItem(keyName="profilesData", name="", description="") @ConfigItem(
public void profilesData(String var1); keyName = "profilesData",
name = "",
description = ""
)
void profilesData(String str);
@ConfigItem(keyName="rememberPassword", name="Remember Password", description="Remembers passwords for accounts") @ConfigItem(
default public boolean rememberPassword() { keyName = "salt",
name = "",
description = "",
hidden = true
)
default String salt()
{
return "";
}
@ConfigItem(
keyName = "salt",
name = "",
description = ""
)
void salt(String key);
@ConfigItem(
keyName = "rememberPassword",
name = "Remember Password",
description = "Remembers passwords for accounts"
)
default boolean rememberPassword()
{
return true; return true;
} }
@ConfigItem(keyName="streamerMode", name="Hide email addresses", description="Hides your account emails") @ConfigItem(
default public boolean isStreamerMode() { keyName = "streamerMode",
name = "Hide email addresses",
description = "Hides your account emails"
)
default boolean isStreamerMode()
{
return false; return false;
} }
@ConfigItem(keyName="switchPanel", name="Auto-open Panel", description="Automatically switch to the account switcher panel on the login screen") @ConfigItem(
default public boolean switchPanel() { keyName = "switchPanel",
return true; name = "Auto-open Panel",
description = "Automatically switch to the account switcher panel on the login screen"
)
default boolean switchPanel()
{
return false;
} }
} }

View File

@@ -1,248 +1,555 @@
/* /*
* Decompiled with CFR 0.139. * Copyright (c) 2019, Spedwards <https://github.com/Spedwards>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package net.runelite.client.plugins.profiles; package net.runelite.client.plugins.profiles;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.Insets; import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent; import java.awt.event.FocusEvent;
import java.awt.event.FocusListener; import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.Consumer; import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.inject.Inject; import javax.inject.Inject;
import javax.swing.JButton; import javax.swing.*;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.client.plugins.profiles.ProfilePanel;
import net.runelite.client.plugins.profiles.ProfilesConfig;
import net.runelite.client.ui.ColorScheme; import net.runelite.client.ui.ColorScheme;
import net.runelite.client.ui.PluginPanel; import net.runelite.client.ui.PluginPanel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class ProfilesPanel @Slf4j
extends PluginPanel { class ProfilesPanel extends PluginPanel
private static final Logger log = LoggerFactory.getLogger(ProfilesPanel.class); {
private static final int iterations = 100000;
private static final String UNLOCK_PASSWORD = "Encryption Password";
private static final String LOAD_ACCOUNTS = "Load Accounts";
private static final String ACCOUNT_USERNAME = "Account Username"; private static final String ACCOUNT_USERNAME = "Account Username";
private static final String ACCOUNT_LABEL = "Account Label"; private static final String ACCOUNT_LABEL = "Account Label";
private static final String PASSWORD_LABEL = "Account Password"; private static final String PASSWORD_LABEL = "Account Password";
private static final Dimension PREFERRED_SIZE = new Dimension(205, 30); private static final Dimension PREFERRED_SIZE = new Dimension(PluginPanel.PANEL_WIDTH - 20, 30);
private static final Dimension MINIMUM_SIZE = new Dimension(0, 30); private static final Dimension MINIMUM_SIZE = new Dimension(0, 30);
private final Client client; private final Client client;
private static ProfilesConfig profilesConfig; private static ProfilesConfig profilesConfig;
private final JTextField txtAccountLabel = new JTextField("Account Label");
private final JPasswordField txtAccountLogin = new JPasswordField("Account Username"); private final JPasswordField txtDecryptPassword = new JPasswordField(UNLOCK_PASSWORD);
private final JPasswordField txtPasswordLogin = new JPasswordField("Account Password"); private final JButton btnLoadAccounts = new JButton(LOAD_ACCOUNTS);
private final JTextField txtAccountLabel = new JTextField(ACCOUNT_LABEL);
private final JPasswordField txtAccountLogin = new JPasswordField(ACCOUNT_USERNAME);
private final JPasswordField txtPasswordLogin = new JPasswordField(PASSWORD_LABEL);
private final JPanel profilesPanel = new JPanel(); private final JPanel profilesPanel = new JPanel();
private GridBagConstraints c; private GridBagConstraints c;
@Inject @Inject
public ProfilesPanel(Client client, final ProfilesConfig config) { public ProfilesPanel(Client client, ProfilesConfig config)
{
super();
this.client = client; this.client = client;
profilesConfig = config; profilesConfig = config;
this.setBorder(new EmptyBorder(18, 10, 0, 10));
this.setBackground(ColorScheme.DARK_GRAY_COLOR);
this.setLayout(new GridBagLayout());
this.c = new GridBagConstraints();
this.c.fill = 2;
this.c.gridx = 0;
this.c.gridy = 0;
this.c.weightx = 1.0;
this.c.weighty = 0.0;
this.c.insets = new Insets(0, 0, 4, 0);
this.txtAccountLabel.setPreferredSize(PREFERRED_SIZE);
this.txtAccountLabel.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
this.txtAccountLabel.setBackground(ColorScheme.DARKER_GRAY_COLOR);
this.txtAccountLabel.setMinimumSize(MINIMUM_SIZE);
this.txtAccountLabel.addFocusListener(new FocusListener(){
setBorder(new EmptyBorder(18, 10, 0, 10));
setBackground(ColorScheme.DARK_GRAY_COLOR);
setLayout(new GridBagLayout());
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 0;
c.insets = new Insets(0, 0, 4, 0);
txtDecryptPassword.setEchoChar((char) 0);
txtDecryptPassword.setPreferredSize(PREFERRED_SIZE);
txtDecryptPassword.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
txtDecryptPassword.setBackground(ColorScheme.DARKER_GRAY_COLOR);
txtDecryptPassword.setMinimumSize(MINIMUM_SIZE);
txtDecryptPassword.setToolTipText(UNLOCK_PASSWORD);
txtDecryptPassword.addFocusListener(new FocusListener()
{
@Override @Override
public void focusGained(FocusEvent e) { public void focusGained(FocusEvent e)
if (ProfilesPanel.this.txtAccountLabel.getText().equals(ProfilesPanel.ACCOUNT_LABEL)) { {
ProfilesPanel.this.txtAccountLabel.setText(""); txtDecryptPassword.setForeground(ColorScheme.LIGHT_GRAY_COLOR);
ProfilesPanel.this.txtAccountLabel.setForeground(ColorScheme.LIGHT_GRAY_COLOR); if (String.valueOf(txtDecryptPassword.getPassword()).equals(UNLOCK_PASSWORD))
{
txtDecryptPassword.setText("");
txtDecryptPassword.setEchoChar('*');
} }
} }
@Override @Override
public void focusLost(FocusEvent e) { public void focusLost(FocusEvent e)
if (ProfilesPanel.this.txtAccountLabel.getText().isEmpty()) { {
ProfilesPanel.this.txtAccountLabel.setForeground(ColorScheme.MEDIUM_GRAY_COLOR); txtDecryptPassword.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
ProfilesPanel.this.txtAccountLabel.setText(ProfilesPanel.ACCOUNT_LABEL); if (txtDecryptPassword.getPassword().length == 0)
{
txtDecryptPassword.setText(UNLOCK_PASSWORD);
txtDecryptPassword.setEchoChar((char) 0);
} }
} }
}); });
this.add((Component)this.txtAccountLabel, this.c);
++this.c.gridy; add(txtDecryptPassword, c);
this.txtAccountLogin.setEchoChar('\u0000'); c.gridy++;
this.txtAccountLogin.setPreferredSize(PREFERRED_SIZE);
this.txtAccountLogin.setForeground(ColorScheme.MEDIUM_GRAY_COLOR); btnLoadAccounts.setPreferredSize(PREFERRED_SIZE);
this.txtAccountLogin.setBackground(ColorScheme.DARKER_GRAY_COLOR); btnLoadAccounts.setBackground(ColorScheme.DARKER_GRAY_COLOR);
this.txtAccountLogin.setMinimumSize(MINIMUM_SIZE); btnLoadAccounts.setMinimumSize(MINIMUM_SIZE);
this.txtAccountLogin.addFocusListener(new FocusListener(){ btnLoadAccounts.setToolTipText(LOAD_ACCOUNTS);
btnLoadAccounts.addMouseListener(new MouseListener()
{
@Override
public void mouseClicked(MouseEvent e)
{
}
@Override @Override
public void focusGained(FocusEvent e) { public void mousePressed(MouseEvent e)
if (ProfilesPanel.ACCOUNT_USERNAME.equals(String.valueOf(ProfilesPanel.this.txtAccountLogin.getPassword()))) { {
ProfilesPanel.this.txtAccountLogin.setText("");
if (config.isStreamerMode()) {
ProfilesPanel.this.txtAccountLogin.setEchoChar('*');
} }
ProfilesPanel.this.txtAccountLogin.setForeground(ColorScheme.LIGHT_GRAY_COLOR);
@Override
public void mouseReleased(MouseEvent e)
{
try
{
redrawProfiles();
}
catch (InvalidKeySpecException | NoSuchAlgorithmException ex)
{
showErrorMessage("Unable to load data", "Incorrect password!");
} }
} }
@Override @Override
public void focusLost(FocusEvent e) { public void mouseEntered(MouseEvent e)
if (ProfilesPanel.this.txtAccountLogin.getPassword().length == 0) { {
ProfilesPanel.this.txtAccountLogin.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
ProfilesPanel.this.txtAccountLogin.setText(ProfilesPanel.ACCOUNT_USERNAME); }
ProfilesPanel.this.txtAccountLogin.setEchoChar('\u0000');
@Override
public void mouseExited(MouseEvent e)
{
}
});
add(btnLoadAccounts, c);
c.gridy++;
txtAccountLabel.setPreferredSize(PREFERRED_SIZE);
txtAccountLabel.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
txtAccountLabel.setBackground(ColorScheme.DARKER_GRAY_COLOR);
txtAccountLabel.setMinimumSize(MINIMUM_SIZE);
txtAccountLabel.addFocusListener(new FocusListener()
{
@Override
public void focusGained(FocusEvent e)
{
if (txtAccountLabel.getText().equals(ACCOUNT_LABEL))
{
txtAccountLabel.setText("");
txtAccountLabel.setForeground(ColorScheme.LIGHT_GRAY_COLOR);
}
}
@Override
public void focusLost(FocusEvent e)
{
if (txtAccountLabel.getText().isEmpty())
{
txtAccountLabel.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
txtAccountLabel.setText(ACCOUNT_LABEL);
} }
} }
}); });
this.add((Component)this.txtAccountLogin, this.c);
++this.c.gridy;
this.txtPasswordLogin.setEchoChar('\u0000');
this.txtPasswordLogin.setPreferredSize(PREFERRED_SIZE);
this.txtPasswordLogin.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
this.txtPasswordLogin.setBackground(ColorScheme.DARKER_GRAY_COLOR);
this.txtPasswordLogin.setToolTipText("Account password");
this.txtPasswordLogin.setMinimumSize(MINIMUM_SIZE);
this.txtPasswordLogin.addFocusListener(new FocusListener(){
add(txtAccountLabel, c);
c.gridy++;
// Do not hide username characters until they focus or if in streamer mode
txtAccountLogin.setEchoChar((char) 0);
txtAccountLogin.setPreferredSize(PREFERRED_SIZE);
txtAccountLogin.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
txtAccountLogin.setBackground(ColorScheme.DARKER_GRAY_COLOR);
txtAccountLogin.setMinimumSize(MINIMUM_SIZE);
txtAccountLogin.addFocusListener(new FocusListener()
{
@Override @Override
public void focusGained(FocusEvent e) { public void focusGained(FocusEvent e)
if (ProfilesPanel.PASSWORD_LABEL.equals(String.valueOf(ProfilesPanel.this.txtPasswordLogin.getPassword()))) { {
ProfilesPanel.this.txtPasswordLogin.setText(""); if (ACCOUNT_USERNAME.equals(String.valueOf(txtAccountLogin.getPassword())))
ProfilesPanel.this.txtPasswordLogin.setEchoChar('*'); {
ProfilesPanel.this.txtPasswordLogin.setForeground(ColorScheme.LIGHT_GRAY_COLOR); txtAccountLogin.setText("");
if (config.isStreamerMode())
{
txtAccountLogin.setEchoChar('*');
}
txtAccountLogin.setForeground(ColorScheme.LIGHT_GRAY_COLOR);
} }
} }
@Override @Override
public void focusLost(FocusEvent e) { public void focusLost(FocusEvent e)
if (ProfilesPanel.this.txtPasswordLogin.getPassword().length == 0) { {
ProfilesPanel.this.txtPasswordLogin.setForeground(ColorScheme.MEDIUM_GRAY_COLOR); if (txtAccountLogin.getPassword().length == 0)
ProfilesPanel.this.txtPasswordLogin.setText(ProfilesPanel.PASSWORD_LABEL); {
ProfilesPanel.this.txtPasswordLogin.setEchoChar('\u0000'); txtAccountLogin.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
txtAccountLogin.setText(ACCOUNT_USERNAME);
txtAccountLogin.setEchoChar((char) 0);
} }
} }
}); });
if (config.rememberPassword()) {
this.add((Component)this.txtPasswordLogin, this.c); add(txtAccountLogin, c);
++this.c.gridy; c.gridy++;
txtPasswordLogin.setEchoChar((char) 0);
txtPasswordLogin.setPreferredSize(PREFERRED_SIZE);
txtPasswordLogin.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
txtPasswordLogin.setBackground(ColorScheme.DARKER_GRAY_COLOR);
txtPasswordLogin.setToolTipText(PASSWORD_LABEL);
txtPasswordLogin.setMinimumSize(MINIMUM_SIZE);
txtPasswordLogin.addFocusListener(new FocusListener()
{
@Override
public void focusGained(FocusEvent e)
{
if (PASSWORD_LABEL.equals(String.valueOf(txtPasswordLogin.getPassword())))
{
txtPasswordLogin.setText("");
txtPasswordLogin.setEchoChar('*');
txtPasswordLogin.setForeground(ColorScheme.LIGHT_GRAY_COLOR);
} }
this.c.insets = new Insets(0, 0, 15, 0); }
final JButton btnAddAccount = new JButton("Add Account");
@Override
public void focusLost(FocusEvent e)
{
if (txtPasswordLogin.getPassword().length == 0)
{
txtPasswordLogin.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
txtPasswordLogin.setText(PASSWORD_LABEL);
txtPasswordLogin.setEchoChar((char) 0);
}
}
});
if (config.rememberPassword())
{
add(txtPasswordLogin, c);
c.gridy++;
}
c.insets = new Insets(0, 0, 15, 0);
JButton btnAddAccount = new JButton("Add Account");
btnAddAccount.setPreferredSize(PREFERRED_SIZE); btnAddAccount.setPreferredSize(PREFERRED_SIZE);
btnAddAccount.setBackground(ColorScheme.DARKER_GRAY_COLOR); btnAddAccount.setBackground(ColorScheme.DARKER_GRAY_COLOR);
btnAddAccount.setMinimumSize(MINIMUM_SIZE); btnAddAccount.setMinimumSize(MINIMUM_SIZE);
btnAddAccount.addActionListener(e -> { btnAddAccount.addActionListener(e ->
String labelText = String.valueOf(this.txtAccountLabel.getText()); {
String loginText = String.valueOf(this.txtAccountLogin.getPassword()); String labelText = String.valueOf(txtAccountLabel.getText());
String passwordText = String.valueOf(this.txtPasswordLogin.getPassword()); String loginText = String.valueOf(txtAccountLogin.getPassword());
if (labelText.equals(ACCOUNT_LABEL) || loginText.equals(ACCOUNT_USERNAME)) { String passwordText = String.valueOf(txtPasswordLogin.getPassword());
if (labelText.equals(ACCOUNT_LABEL) || loginText.equals(ACCOUNT_USERNAME))
{
return; return;
} }
String data = config.rememberPassword() && this.txtPasswordLogin.getPassword() != null ? labelText + ":" + loginText + ":" + passwordText : labelText + ":" + loginText; String data;
log.info(data); if (config.rememberPassword() && txtPasswordLogin.getPassword() != null)
this.addAccount(data); {
ProfilesPanel.addProfile(data); data = labelText + ":" + loginText + ":" + passwordText;
this.txtAccountLabel.setText(ACCOUNT_LABEL); }
this.txtAccountLabel.setForeground(ColorScheme.MEDIUM_GRAY_COLOR); else
this.txtAccountLogin.setText(ACCOUNT_USERNAME); {
this.txtAccountLogin.setEchoChar('\u0000'); data = labelText + ":" + loginText;
this.txtAccountLogin.setForeground(ColorScheme.MEDIUM_GRAY_COLOR); }
this.txtPasswordLogin.setText(PASSWORD_LABEL);
this.txtPasswordLogin.setEchoChar('\u0000');
this.txtPasswordLogin.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
});
this.txtAccountLogin.addKeyListener(new KeyAdapter(){
try
{
if(!addProfile(data))
{
return;
}
}
catch (InvalidKeySpecException | NoSuchAlgorithmException ex)
{
ex.printStackTrace();
}
this.addAccount(data);
txtAccountLabel.setText(ACCOUNT_LABEL);
txtAccountLabel.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
txtAccountLogin.setText(ACCOUNT_USERNAME);
txtAccountLogin.setEchoChar((char) 0);
txtAccountLogin.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
txtPasswordLogin.setText(PASSWORD_LABEL);
txtPasswordLogin.setEchoChar((char) 0);
txtPasswordLogin.setForeground(ColorScheme.MEDIUM_GRAY_COLOR);
});
txtAccountLogin.addKeyListener(new KeyAdapter()
{
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e)
if (e.getKeyCode() == 10) { {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
btnAddAccount.doClick(); btnAddAccount.doClick();
btnAddAccount.requestFocus(); btnAddAccount.requestFocus();
} }
} }
}); });
this.txtAccountLogin.addMouseListener(new MouseListener(){ txtAccountLogin.addMouseListener(new MouseListener()
{
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e)
{
} }
@Override @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e)
{
} }
@Override @Override
public void mouseReleased(MouseEvent e) { public void mouseReleased(MouseEvent e)
{
} }
@Override @Override
public void mouseEntered(MouseEvent e) { public void mouseEntered(MouseEvent e)
{
} }
@Override @Override
public void mouseExited(MouseEvent e) { public void mouseExited(MouseEvent e)
{
} }
}); });
this.add((Component)btnAddAccount, this.c);
++this.c.gridy; add(btnAddAccount, c);
this.profilesPanel.setLayout(new GridBagLayout()); c.gridy++;
this.add((Component)this.profilesPanel, this.c);
this.c.gridy = 0; profilesPanel.setLayout(new GridBagLayout());
this.c.insets = new Insets(0, 0, 5, 0); add(profilesPanel, c);
this.addAccounts(config.profilesData()); c.gridy = 0;
c.insets = new Insets(0, 0, 5, 0);
//addAccounts(config.profilesData());
} }
void redrawProfiles() { void redrawProfiles() throws InvalidKeySpecException, NoSuchAlgorithmException
this.profilesPanel.removeAll(); {
this.c.gridy = 0; profilesPanel.removeAll();
this.addAccounts(profilesConfig.profilesData()); c.gridy = 0;
addAccounts(getProfileData());
revalidate();
repaint();
} }
private void addAccount(String data) { private void addAccount(String data)
ProfilePanel profile = new ProfilePanel(this.client, data, profilesConfig); {
++this.c.gridy; ProfilePanel profile = new ProfilePanel(client, data, profilesConfig, this);
this.profilesPanel.add((Component)profile, this.c); c.gridy++;
this.revalidate(); profilesPanel.add(profile, c);
this.repaint();
revalidate();
repaint();
} }
void addAccounts(String data) { void addAccounts(String data)
if (!(data = data.trim()).contains(":")) { {
//log.info("Data: " + data);
data = data.trim();
if (!data.contains(":"))
{
return; return;
} }
Arrays.stream(data.split("\\n")).forEach(this::addAccount); Arrays.stream(data.split("\\n")).forEach(this::addAccount);
} }
static void addProfile(String data) { boolean addProfile(String data) throws InvalidKeySpecException, NoSuchAlgorithmException
profilesConfig.profilesData(profilesConfig.profilesData() + data + "\n"); {
return setProfileData(
getProfileData() + data + "\n");
} }
static void removeProfile(String data) { void removeProfile(String data) throws InvalidKeySpecException, NoSuchAlgorithmException
profilesConfig.profilesData(profilesConfig.profilesData().replaceAll(data + "\\n", "")); {
setProfileData(
getProfileData().replaceAll(data + "\\n", ""));
redrawProfiles();
}
void setSalt(byte[] bytes)
{
profilesConfig.salt(base64Encode(bytes));
}
byte[] getSalt()
{
if(profilesConfig.salt().length() == 0)
{
return new byte[0];
}
return base64Decode(profilesConfig.salt());
}
SecretKey getAesKey() throws NoSuchAlgorithmException, InvalidKeySpecException
{
if(getSalt().length == 0)
{
byte[] b = new byte[16];
SecureRandom.getInstanceStrong().nextBytes(b);
setSalt(b);
}
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(txtDecryptPassword.getPassword(), getSalt(), iterations, 128);
SecretKey key = factory.generateSecret(spec);
return key;
}
String getProfileData() throws InvalidKeySpecException, NoSuchAlgorithmException
{
String tmp = profilesConfig.profilesData();
if(tmp.startsWith("¬"))
{
if(txtDecryptPassword.getPassword().length == 0 || String.valueOf(txtDecryptPassword.getPassword()).equals(UNLOCK_PASSWORD))
{
showErrorMessage("Unable to load data", "Please enter a password!");
return tmp;
}
tmp = tmp.substring(1);
return decryptText(base64Decode(tmp), getAesKey());
}
return tmp;
}
boolean setProfileData(String data) throws InvalidKeySpecException, NoSuchAlgorithmException
{
if(txtDecryptPassword.getPassword().length == 0 || String.valueOf(txtDecryptPassword.getPassword()).equals(UNLOCK_PASSWORD))
{
showErrorMessage("Unable to save data", "Please enter a password!");
return false;
}
byte[] enc = encryptText(data, getAesKey());
if(enc.length == 0)
return false;
String s = "¬"+base64Encode(enc);
profilesConfig.profilesData(s);
return true;
}
public byte[] base64Decode(String data)
{
return Base64.getDecoder().decode(data);
}
public String base64Encode(byte[] data)
{
return Base64.getEncoder().encodeToString(data);
}
/**
* Encrypts login info
*
* @param text text to encrypt
* @return encrypted string
*/
public static byte[] encryptText(String text, SecretKey aesKey)
{
try
{
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec newKey = new SecretKeySpec(aesKey.getEncoded(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, newKey);
return cipher.doFinal(text.getBytes());
}
catch (NoSuchAlgorithmException | IllegalBlockSizeException | InvalidKeyException | BadPaddingException | NoSuchPaddingException e)
{
e.printStackTrace();
}
return new byte[0];
}
public static String decryptText(byte[] enc, SecretKey aesKey)
{
try
{
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec newKey = new SecretKeySpec(aesKey.getEncoded(), "AES");
cipher.init(Cipher.DECRYPT_MODE, newKey);
return new String(cipher.doFinal(enc));
}
catch (NoSuchAlgorithmException | IllegalBlockSizeException | InvalidKeyException | BadPaddingException | NoSuchPaddingException e)
{
e.printStackTrace();
}
return "";
}
public static void showErrorMessage(String title, String text)
{
SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(null,
text,
title,
JOptionPane.ERROR_MESSAGE));
} }
} }

View File

@@ -1,10 +1,30 @@
/* /*
* Decompiled with CFR 0.139. * Copyright (c) 2019, Spedwards <https://github.com/Spedwards>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package net.runelite.client.plugins.profiles; package net.runelite.client.plugins.profiles;
import com.google.inject.Provides; import com.google.inject.Provides;
import java.awt.image.BufferedImage;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
import java.security.Key; import java.security.Key;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@@ -14,9 +34,10 @@ import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException; import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import javax.inject.Inject;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.events.ConfigChanged; import net.runelite.api.events.ConfigChanged;
import net.runelite.api.events.GameStateChanged;
import net.runelite.client.config.ConfigManager; import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe; import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.Plugin;
@@ -25,112 +46,85 @@ import net.runelite.client.plugins.PluginType;
import net.runelite.client.ui.ClientToolbar; import net.runelite.client.ui.ClientToolbar;
import net.runelite.client.ui.NavigationButton; import net.runelite.client.ui.NavigationButton;
import net.runelite.client.util.ImageUtil; import net.runelite.client.util.ImageUtil;
import javax.inject.Inject;
import java.awt.image.BufferedImage;
@PluginDescriptor( @PluginDescriptor(
name = "Account Switcher", name = "Account Switcher",
description = "Allow for a allows you to easily switch between multiple OSRS Accounts", description = "Allow for a allows you to easily switch between multiple OSRS Accounts",
tags={"profile", "account", "login", "log in"}, tags = {"profile", "account", "login", "log in", "pklite"},
type = PluginType.UTILITY type = PluginType.UTILITY
) )
public class ProfilesPlugin extends Plugin
public class ProfilesPlugin {
extends Plugin {
@Inject @Inject
private ClientToolbar clientToolbar; private ClientToolbar clientToolbar;
@Inject @Inject
private Client client; private Client client;
@Inject @Inject
private ProfilesConfig config; private ProfilesConfig config;
private ProfilesPanel panel; private ProfilesPanel panel;
private NavigationButton navButton; private NavigationButton navButton;
String text = "Hello World";
private static String key = "Bar12345Bar12345";
private static Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
@Provides @Provides
ProfilesConfig getConfig(ConfigManager configManager) { ProfilesConfig getConfig(ConfigManager configManager)
{
return configManager.getConfig(ProfilesConfig.class); return configManager.getConfig(ProfilesConfig.class);
} }
@Override @Override
protected void startUp() throws Exception { protected void startUp() throws Exception
this.panel = this.injector.getInstance(ProfilesPanel.class); {
BufferedImage icon = ImageUtil.getResourceStreamFromClass(this.getClass(), "/net/runelite/client/plugins/profiles/profiles_icon.png"); panel = injector.getInstance(ProfilesPanel.class);
this.navButton = NavigationButton.builder().tooltip("Profiles").icon(icon).priority(8).panel(this.panel).build();
this.clientToolbar.addNavigation(this.navButton); final BufferedImage icon = ImageUtil.getResourceStreamFromClass(getClass(), "profiles_icon.png");
navButton = NavigationButton.builder()
.tooltip("Profiles")
.icon(icon)
.priority(8)
.panel(panel)
.build();
clientToolbar.addNavigation(navButton);
} }
@Override @Override
protected void shutDown() { protected void shutDown()
this.clientToolbar.removeNavigation(this.navButton); {
clientToolbar.removeNavigation(navButton);
} }
@Subscribe @Subscribe
private void onConfigChanged(ConfigChanged event) throws Exception { void onGameStateChanged(GameStateChanged event)
if (event.getGroup().equals("profiles") && event.getKey().equals("rememberPassword")) { {
this.panel = this.injector.getInstance(ProfilesPanel.class); if (event.getGameState().equals(GameState.LOGIN_SCREEN) && config.switchPanel())
{
if (!navButton.isSelected())
{
navButton.getOnSelect().run();
}
}
}
@Subscribe
private void onConfigChanged(ConfigChanged event) throws Exception
{
if (event.getGroup().equals("profiles"))
{
if (event.getKey().equals("rememberPassword"))
{
panel = injector.getInstance(ProfilesPanel.class);
this.shutDown(); this.shutDown();
this.startUp(); this.startUp();
} }
} }
public static String decryptText(String text) {
byte[] bb = new byte[text.length()];
for (int i = 0; i < text.length(); ++i) {
bb[i] = (byte)text.charAt(i);
}
Cipher cipher = null;
try {
cipher = Cipher.getInstance("AES");
}
catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
}
try {
cipher.init(2, aesKey);
}
catch (InvalidKeyException e) {
e.printStackTrace();
}
try {
Logger.getLogger("EncryptionLogger").info("Decrypted " + text + " to " + new String(cipher.doFinal(bb)));
return new String(cipher.doFinal(bb));
}
catch (BadPaddingException | IllegalBlockSizeException e) {
e.printStackTrace();
return "";
}
} }
public static String encryptText(String text) {
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(1, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : encrypted) {
sb.append((char)b);
} }
Logger.getLogger("EncryptionLogger").info("Encrypted " + text + " to " + sb.toString());
return sb.toString();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (NoSuchPaddingException e) {
e.printStackTrace();
}
catch (BadPaddingException e) {
e.printStackTrace();
}
catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
catch (InvalidKeyException e) {
e.printStackTrace();
}
return "";
}
}