Skip to content

Commit

Permalink
Logging in after encryption/decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikola-Mircic committed Dec 30, 2023
1 parent e9182bb commit 8957da4
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/efakturaplus/gui/KeyPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,16 @@ private void submitData() {
}
else {
try {
System.out.println("Decrypted: "+decryptData());
String data = decryptData();
System.out.println("Decrypted: "+data);

User.API_KEY = data;
} catch (Exception e) {
e.printStackTrace();
}
}

parent.showMainPanel();
}

private void encryptData() throws NoSuchAlgorithmException, InvalidKeySpecException {
Expand Down Expand Up @@ -198,11 +203,9 @@ private String decryptData() {
try (FileInputStream fileIn = new FileInputStream("user.enc")) {
byte[] fileIv = new byte[16];
byte[] salt = new byte[8];
byte[] data = new byte[16];

fileIn.read(fileIv);
fileIn.read(salt);
fileIn.read(data);

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(passInput.getPassword(), salt, 65536, 256);
Expand All @@ -213,7 +216,10 @@ private String decryptData() {

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(fileIv));
String plaintext = new String(cipher.doFinal(data));

CipherInputStream cis = new CipherInputStream(fileIn, cipher);

String plaintext = new String(cis.readAllBytes());

content = plaintext;

Expand Down

0 comments on commit 8957da4

Please sign in to comment.