Skip to content

Commit

Permalink
obfuscation table model can now be extracted to a seperated file
Browse files Browse the repository at this point in the history
  • Loading branch information
astrapisixtynine committed Apr 1, 2024
1 parent 951e5d7 commit d40fd13
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 19 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -137,7 +136,7 @@ public static File saveToFileWithPasswordAndPrivateKey(
SecretKey symmetricKey;
PublicKeyGenericEncryptor<String> genericEncryptor;
PrivateKey privateKey;
CryptModel<Cipher, PublicKey, byte[]> encryptModel;
CryptModel<Cipher, PublicKey, byte[]> encryptionModel;
String xml;
char[] masterPw;
PublicKey publicKey;
Expand All @@ -154,7 +153,7 @@ public static File saveToFileWithPasswordAndPrivateKey(
publicKey = RuntimeExceptionDecorator
.decorate(() -> PrivateKeyExtensions.generatePublicKey(privateKey));

encryptModel = CryptModel.<Cipher, PublicKey, byte[]> builder().key(publicKey).build();
encryptionModel = CryptModel.<Cipher, PublicKey, byte[]> builder().key(publicKey).build();

symmetricKey = RuntimeExceptionDecorator.decorate(
() -> SecretKeyFactoryExtensions.newSecretKey(AesAlgorithm.AES.getAlgorithm(), 128));
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +75,7 @@ public class ObfuscationOperationRuleTablePanel extends BasePanel<ObfuscationOpe
private javax.swing.JButton btnExport;
private javax.swing.JButton btnImport;
private JFileChooser fileChooser;
private FileNameExtensionFilter fileNameExtensionFilter;
private JLabel lblKeyRules;
private JScrollPane scpKeyRules;
private GenericJTable<KeyValuePair<Character, ObfuscationOperationRule<Character, Character>>> tblKeyRules;
Expand Down Expand Up @@ -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<KeyValuePair<Character, ObfuscationOperationRule<Character, Character>>> 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<KeyValuePair<Character, ObfuscationOperationRule<Character, Character>>> 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<KeyValuePair<Character, ObfuscationOperationRule<Character, Character>>> 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);
}
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +74,7 @@ public class ObfuscationRuleTablePanel extends BasePanel<ObfuscationModelBean>
private Map<String, Class<?>> aliases;
private javax.swing.JButton btnExport;
private javax.swing.JButton btnImport;
private FileNameExtensionFilter fileNameExtensionFilter;
private JFileChooser fileChooser;
private JLabel lblKeyRules;
private JScrollPane scpKeyRules;
Expand Down Expand Up @@ -110,30 +125,58 @@ protected void onEditObfuscationRule(ObfuscationRule<Character, Character> 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<KeyValuePair<Character, ObfuscationRule<Character, Character>>> 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<KeyValuePair<Character, ObfuscationRule<Character, Character>>> 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);
}
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d40fd13

Please sign in to comment.