From d40fd139efc30e5287f459fabc481a75cb387db1 Mon Sep 17 00:00:00 2001 From: Asterios Raptis Date: Mon, 1 Apr 2024 19:11:03 +0200 Subject: [PATCH] obfuscation table model can now be extracted to a seperated file --- gradle.properties | 6 +- .../file/xml/ApplicationXmlFileReader.java | 2 + .../xml/ApplicationXmlFileStoreWorker.java | 8 +-- .../ObfuscationOperationRuleTablePanel.java | 52 ++++++++++++++++-- .../simple/ObfuscationRuleTablePanel.java | 55 +++++++++++++++++-- .../panel/privatekey/NewPrivateKeyPanel.java | 1 - 6 files changed, 105 insertions(+), 19 deletions(-) diff --git a/gradle.properties b/gradle.properties index 31ebfd72..7d973abf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -60,9 +60,9 @@ javahelpVersion=2.0.05 jobjCoreVersion=8.2 xstreamVersion=1.4.20 throwableVersion=2.3 -cryptApiVersion=8.7 -cryptDataVersion=8.5 -mysticCryptVersion=8.1 +cryptApiVersion=9 +cryptDataVersion=9 +mysticCryptVersion=9 bouncycastleVersion=1.70 checksumUpVersion=3 xmlExtensionsVersion=8.2 diff --git a/src/main/java/io/github/astrapi69/mystic/crypt/app/file/xml/ApplicationXmlFileReader.java b/src/main/java/io/github/astrapi69/mystic/crypt/app/file/xml/ApplicationXmlFileReader.java index 44a39c3e..ffac6842 100644 --- a/src/main/java/io/github/astrapi69/mystic/crypt/app/file/xml/ApplicationXmlFileReader.java +++ b/src/main/java/io/github/astrapi69/mystic/crypt/app/file/xml/ApplicationXmlFileReader.java @@ -211,6 +211,8 @@ public static ApplicationModelBean getApplicationModelBean(File applicationFile, byte[] encryptedBytes = ReadFileExtensions.readFileToBytearray(applicationFile); String xml = genericDecryptor.decrypt(encryptedBytes); applicationModelBean = XmlToObjectExtensions.toObject(xml); + applicationModelBean.getMasterPwFileModelBean() + .setPrivateKeyInfo(KeyModelExtensions.toKeyModel(privateKey)); return applicationModelBean; } } diff --git a/src/main/java/io/github/astrapi69/mystic/crypt/app/file/xml/ApplicationXmlFileStoreWorker.java b/src/main/java/io/github/astrapi69/mystic/crypt/app/file/xml/ApplicationXmlFileStoreWorker.java index 4fd7e9d3..8032a624 100644 --- a/src/main/java/io/github/astrapi69/mystic/crypt/app/file/xml/ApplicationXmlFileStoreWorker.java +++ b/src/main/java/io/github/astrapi69/mystic/crypt/app/file/xml/ApplicationXmlFileStoreWorker.java @@ -116,7 +116,6 @@ public static File saveToFileWithPrivateKey(ApplicationModelBean applicationMode encryptor = RuntimeExceptionDecorator .decorate(() -> new PublicKeyEncryptor(encryptModel, symmetricKeyModel)); genericEncryptor = new PublicKeyGenericEncryptor<>(encryptor); - applicationModelBean.getMasterPwFileModelBean().setPrivateKeyInfo(null); xml = ObjectToXmlExtensions.toXml(applicationModelBean); @@ -137,7 +136,7 @@ public static File saveToFileWithPasswordAndPrivateKey( SecretKey symmetricKey; PublicKeyGenericEncryptor genericEncryptor; PrivateKey privateKey; - CryptModel encryptModel; + CryptModel encryptionModel; String xml; char[] masterPw; PublicKey publicKey; @@ -154,7 +153,7 @@ public static File saveToFileWithPasswordAndPrivateKey( publicKey = RuntimeExceptionDecorator .decorate(() -> PrivateKeyExtensions.generatePublicKey(privateKey)); - encryptModel = CryptModel. builder().key(publicKey).build(); + encryptionModel = CryptModel. builder().key(publicKey).build(); symmetricKey = RuntimeExceptionDecorator.decorate( () -> SecretKeyFactoryExtensions.newSecretKey(AesAlgorithm.AES.getAlgorithm(), 128)); @@ -163,13 +162,12 @@ public static File saveToFileWithPasswordAndPrivateKey( .algorithm(AesAlgorithm.AES).operationMode(Cipher.ENCRYPT_MODE).build(); encryptor = RuntimeExceptionDecorator - .decorate(() -> new PublicKeyEncryptor(encryptModel, symmetricKeyModel)); + .decorate(() -> new PublicKeyEncryptor(encryptionModel, symmetricKeyModel)); genericEncryptor = new PublicKeyGenericEncryptor<>(encryptor); passwordStringEncryptor = new PasswordStringEncryptor(String.valueOf(masterPw)); - applicationModelBean.getMasterPwFileModelBean().setPrivateKeyInfo(null); xml = ObjectToXmlExtensions.toXml(applicationModelBean); diff --git a/src/main/java/io/github/astrapi69/mystic/crypt/panel/obfuscate/character/ObfuscationOperationRuleTablePanel.java b/src/main/java/io/github/astrapi69/mystic/crypt/panel/obfuscate/character/ObfuscationOperationRuleTablePanel.java index 8b7ce860..6d86c853 100644 --- a/src/main/java/io/github/astrapi69/mystic/crypt/panel/obfuscate/character/ObfuscationOperationRuleTablePanel.java +++ b/src/main/java/io/github/astrapi69/mystic/crypt/panel/obfuscate/character/ObfuscationOperationRuleTablePanel.java @@ -23,13 +23,26 @@ import java.awt.event.ActionEvent; import java.io.File; import java.io.IOException; +import java.security.PrivateKey; +import java.security.PublicKey; import java.util.List; import java.util.Map; import java.util.logging.Level; import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.TableColumn; +import io.github.astrapi69.crypt.data.key.KeyModelExtensions; +import io.github.astrapi69.crypt.data.key.PrivateKeyExtensions; +import io.github.astrapi69.crypt.data.model.KeyModel; +import io.github.astrapi69.file.read.ReadFileExtensions; +import io.github.astrapi69.file.write.StoreFileExtensions; +import io.github.astrapi69.mystic.crypt.ApplicationModelBean; +import io.github.astrapi69.mystic.crypt.key.PrivateKeyStringDecryptor; +import io.github.astrapi69.mystic.crypt.key.PublicKeyStringEncryptor; +import io.github.astrapi69.xstream.ObjectToXmlExtensions; +import io.github.astrapi69.xstream.XmlToObjectExtensions; import lombok.Getter; import lombok.NonNull; import lombok.extern.java.Log; @@ -62,6 +75,7 @@ public class ObfuscationOperationRuleTablePanel extends BasePanel>> tblKeyRules; @@ -96,31 +110,56 @@ protected void onEditObfuscationOperationRule( protected void onExport(final ActionEvent actionEvent) { + fileChooser.setFileFilter(fileNameExtensionFilter); final int returnVal = fileChooser.showSaveDialog(ObfuscationOperationRuleTablePanel.this); if (returnVal == JFileChooser.APPROVE_OPTION) { List>> data = getModelObject() .getTableModel().getData(); final File selectedFile = fileChooser.getSelectedFile(); - RuntimeExceptionDecorator.decorate( - () -> XmlEncryptionExtensions.writeToFileAsXmlAndHex(aliases, data, selectedFile)); + + ApplicationModelBean modelObject = MysticCryptApplicationFrame.getInstance() + .getModelObject(); + KeyModel privateKeyInfo = modelObject.getMasterPwFileModelBean().getPrivateKeyInfo(); + PrivateKey privateKey = KeyModelExtensions.toPrivateKey(privateKeyInfo); + PublicKey publicKey = RuntimeExceptionDecorator + .decorate(() -> PrivateKeyExtensions.generatePublicKey(privateKey)); + PublicKeyStringEncryptor encryptor = new PublicKeyStringEncryptor(publicKey); + + String xml = ObjectToXmlExtensions.toXml(data); + byte[] encrypted = RuntimeExceptionDecorator.decorate(() -> encryptor.encrypt(xml)); + + RuntimeExceptionDecorator + .decorate(() -> StoreFileExtensions.toFile(selectedFile, encrypted)); } } protected void onImport(final ActionEvent actionEvent) { + fileChooser.setFileFilter(fileNameExtensionFilter); final int returnVal = fileChooser.showOpenDialog(ObfuscationOperationRuleTablePanel.this); if (returnVal == JFileChooser.APPROVE_OPTION) { final File selectedFile = fileChooser.getSelectedFile(); try { - List>> data = XmlDecryptionExtensions - .readFromFileAsXmlAndHex(aliases, selectedFile, "io.github.astrapi69.**"); + KeyModel privateKeyInfo = MysticCryptApplicationFrame.getInstance().getModelObject() + .getMasterPwFileModelBean().getPrivateKeyInfo(); + PrivateKey privateKey = KeyModelExtensions.toPrivateKey(privateKeyInfo); + + byte[] encrypted = ReadFileExtensions.readFileToBytearray(selectedFile); + + PrivateKeyStringDecryptor decryptor = new PrivateKeyStringDecryptor(privateKey); + + String xml = RuntimeExceptionDecorator.decorate(() -> decryptor.decrypt(encrypted)); + + List>> data = XmlToObjectExtensions + .toObject(xml); + getModelObject().getTableModel().setData(data); getModelObject().getTableModel().fireTableDataChanged(); } - catch (final IOException | DecoderException e) + catch (final IOException e) { log.log(Level.SEVERE, e.getLocalizedMessage(), e); } @@ -185,6 +224,9 @@ protected String onSetText() fileChooser = new JFileChooser( MysticCryptApplicationFrame.getInstance().getConfigurationDirectory()); + + fileNameExtensionFilter = new FileNameExtensionFilter( + "Mystic crypt obfuscation files (*.obf)", "obf"); } protected void onInitializeGroupLayout() diff --git a/src/main/java/io/github/astrapi69/mystic/crypt/panel/obfuscate/simple/ObfuscationRuleTablePanel.java b/src/main/java/io/github/astrapi69/mystic/crypt/panel/obfuscate/simple/ObfuscationRuleTablePanel.java index 39792e89..9b536f82 100644 --- a/src/main/java/io/github/astrapi69/mystic/crypt/panel/obfuscate/simple/ObfuscationRuleTablePanel.java +++ b/src/main/java/io/github/astrapi69/mystic/crypt/panel/obfuscate/simple/ObfuscationRuleTablePanel.java @@ -23,13 +23,27 @@ import java.awt.event.ActionEvent; import java.io.File; import java.io.IOException; +import java.security.PrivateKey; +import java.security.PublicKey; import java.util.List; import java.util.Map; import java.util.logging.Level; import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.TableColumn; +import io.github.astrapi69.crypt.data.key.KeyModelExtensions; +import io.github.astrapi69.crypt.data.key.PrivateKeyExtensions; +import io.github.astrapi69.crypt.data.model.KeyModel; +import io.github.astrapi69.crypt.data.obfuscation.rule.ObfuscationOperationRule; +import io.github.astrapi69.file.read.ReadFileExtensions; +import io.github.astrapi69.file.write.StoreFileExtensions; +import io.github.astrapi69.mystic.crypt.ApplicationModelBean; +import io.github.astrapi69.mystic.crypt.key.PrivateKeyStringDecryptor; +import io.github.astrapi69.mystic.crypt.key.PublicKeyStringEncryptor; +import io.github.astrapi69.xstream.ObjectToXmlExtensions; +import io.github.astrapi69.xstream.XmlToObjectExtensions; import lombok.Getter; import lombok.NonNull; import lombok.extern.java.Log; @@ -60,6 +74,7 @@ public class ObfuscationRuleTablePanel extends BasePanel private Map> aliases; private javax.swing.JButton btnExport; private javax.swing.JButton btnImport; + private FileNameExtensionFilter fileNameExtensionFilter; private JFileChooser fileChooser; private JLabel lblKeyRules; private JScrollPane scpKeyRules; @@ -110,30 +125,58 @@ protected void onEditObfuscationRule(ObfuscationRule selec protected void onExport(final ActionEvent actionEvent) { + fileChooser.setFileFilter(fileNameExtensionFilter); final int returnVal = fileChooser.showSaveDialog(ObfuscationRuleTablePanel.this); if (returnVal == JFileChooser.APPROVE_OPTION) { + ApplicationModelBean modelObject = MysticCryptApplicationFrame.getInstance() + .getModelObject(); + KeyModel privateKeyInfo = modelObject.getMasterPwFileModelBean().getPrivateKeyInfo(); + PrivateKey privateKey = KeyModelExtensions.toPrivateKey(privateKeyInfo); + PublicKey publicKey = RuntimeExceptionDecorator + .decorate(() -> PrivateKeyExtensions.generatePublicKey(privateKey)); + PublicKeyStringEncryptor encryptor = new PublicKeyStringEncryptor(publicKey); + List>> data = getModelObject() .getTableModel().getData(); + String xml = ObjectToXmlExtensions.toXml(data); + byte[] encrypted = RuntimeExceptionDecorator.decorate(() -> encryptor.encrypt(xml)); + final File selectedFile = fileChooser.getSelectedFile(); - RuntimeExceptionDecorator.decorate( - () -> XmlEncryptionExtensions.writeToFileAsXmlAndHex(aliases, data, selectedFile)); + RuntimeExceptionDecorator + .decorate(() -> StoreFileExtensions.toFile(selectedFile, encrypted)); } } protected void onImport(final ActionEvent actionEvent) { + fileChooser.setFileFilter(fileNameExtensionFilter); final int returnVal = fileChooser.showOpenDialog(ObfuscationRuleTablePanel.this); if (returnVal == JFileChooser.APPROVE_OPTION) { final File selectedFile = fileChooser.getSelectedFile(); try { - getModelObject().getTableModel().setData(XmlDecryptionExtensions - .readFromFileAsXmlAndHex(aliases, selectedFile, "io.github.astrapi69.**")); + ApplicationModelBean modelObject = MysticCryptApplicationFrame.getInstance() + .getModelObject(); + KeyModel privateKeyInfo = modelObject.getMasterPwFileModelBean() + .getPrivateKeyInfo(); + PrivateKey privateKey = KeyModelExtensions.toPrivateKey(privateKeyInfo); + + byte[] encrypted = ReadFileExtensions.readFileToBytearray(selectedFile); + + PrivateKeyStringDecryptor decryptor = new PrivateKeyStringDecryptor(privateKey); + + String xml = RuntimeExceptionDecorator.decorate(() -> decryptor.decrypt(encrypted)); + + List>> data = XmlToObjectExtensions + .toObject(xml); + + getModelObject().getTableModel().setData(data); getModelObject().getTableModel().fireTableDataChanged(); + } - catch (final IOException | DecoderException e) + catch (final IOException e) { log.log(Level.SEVERE, e.getLocalizedMessage(), e); } @@ -179,6 +222,8 @@ protected void onInitializeComponents() fileChooser = new JFileChooser( MysticCryptApplicationFrame.getInstance().getConfigurationDirectory()); + fileNameExtensionFilter = new FileNameExtensionFilter( + "Mystic crypt obfuscation files (*.obf)", "obf"); } protected void onInitializeGroupLayout() diff --git a/src/main/java/io/github/astrapi69/mystic/crypt/panel/privatekey/NewPrivateKeyPanel.java b/src/main/java/io/github/astrapi69/mystic/crypt/panel/privatekey/NewPrivateKeyPanel.java index 248b8609..2caee17e 100644 --- a/src/main/java/io/github/astrapi69/mystic/crypt/panel/privatekey/NewPrivateKeyPanel.java +++ b/src/main/java/io/github/astrapi69/mystic/crypt/panel/privatekey/NewPrivateKeyPanel.java @@ -274,7 +274,6 @@ protected void onClear(final ActionEvent actionEvent) getTxtFilenameOfPrivateKey().setText(""); getModelObject().setKeySize(KeySize.KEYSIZE_2048); getModelObject().setFilenameOfPrivateKey(""); - getModelObject().setPrivateKeyInfo(null); getModelObject().setPrivateKeyDirectory(null); getModelObject().setPrivateKeyFile(null); btnSaveStateMachine.onClear();