From 27de43fa593088722a5d9d1093d730a1e815b77e Mon Sep 17 00:00:00 2001 From: Francesco Valentini Date: Fri, 17 Mar 2023 22:50:25 +0100 Subject: [PATCH] Implementazione Base36 e correzione bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dopo aver implementato la codifica Base36, è stato risolto il bug che obbligava l'utente a riavviare il programma quando inseriva una nuova chiave dopo la creazione di un nuovo keystore. --- README.md | 2 ++ .../TextEncryptionUtility/AES256.java | 6 +++- .../TextEncryptionUtility/Base36.java | 31 +++++++++++++++++++ .../Settings_Window.java | 22 ++++++++----- .../TextEncryptionUtil_Main.java | 4 +++ 5 files changed, 57 insertions(+), 8 deletions(-) create mode 100644 src/it/HackerInside/TextEncryptionUtility/Base36.java diff --git a/README.md b/README.md index 7108bba..755368f 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Per criptare un file procedere come segue: - Base58 - Esadecimale (Hex) - PGP Word list + - Base36 - **SPACING:** Aggiunge uno spazio dopo "n" caratteri (Disabilitato con codifica PGP Word list) - **TextArea Wrapping:** Opzione di "a capo automatico" della textArea @@ -102,3 +103,4 @@ Non mi assumo nessuna responsabilità di eventuali danni provocati da questo cod - https://en.wikipedia.org/wiki/Galois/Counter_Mode - https://github.com/multiformats/java-multibase/blob/master/src/main/java/io/ipfs/multibase/Base58.java - https://neilmadden.blog/2016/05/20/ephemeral-elliptic-curve-diffie-hellman-key-agreement-in-java/ +- https://en.wikipedia.org/wiki/Base36 \ No newline at end of file diff --git a/src/it/HackerInside/TextEncryptionUtility/AES256.java b/src/it/HackerInside/TextEncryptionUtility/AES256.java index 9925b38..17a8536 100644 --- a/src/it/HackerInside/TextEncryptionUtility/AES256.java +++ b/src/it/HackerInside/TextEncryptionUtility/AES256.java @@ -159,6 +159,9 @@ public static String encryptDecryptString(int mode, String input, SecretKey key, e1.printStackTrace(); } + }else if(encoding.equalsIgnoreCase("base36")) {// Base36 Encoding + return Base36.encode(os.toByteArray()); + } else { return bytesToHex(os.toByteArray()); @@ -183,8 +186,9 @@ else if(mode==Cipher.DECRYPT_MODE){ // TODO Auto-generated catch block e1.printStackTrace(); } + }else if(encoding.equalsIgnoreCase("base36")) {// Base36 Decoding + decodedData = Base36.decode(input); } - is = new BufferedInputStream(new ByteArrayInputStream(decodedData)); byte[] iv = new byte[16]; diff --git a/src/it/HackerInside/TextEncryptionUtility/Base36.java b/src/it/HackerInside/TextEncryptionUtility/Base36.java new file mode 100644 index 0000000..0a88f51 --- /dev/null +++ b/src/it/HackerInside/TextEncryptionUtility/Base36.java @@ -0,0 +1,31 @@ +package it.HackerInside.TextEncryptionUtility; + +import java.math.BigInteger; + +public class Base36 { + + private static final char[] BASE_36_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); + + public static String encode(byte[] bytes) { + BigInteger bigInt = new BigInteger(1, bytes); + StringBuilder sb = new StringBuilder(); + while (bigInt.compareTo(BigInteger.ZERO) > 0) { + BigInteger[] quotientAndRemainder = bigInt.divideAndRemainder(BigInteger.valueOf(36)); + sb.append(BASE_36_ALPHABET[quotientAndRemainder[1].intValue()]); + bigInt = quotientAndRemainder[0]; + } + return sb.reverse().toString(); + } + + public static byte[] decode(String base36String) { + BigInteger bigInt = new BigInteger(base36String, 36); + byte[] bytes = bigInt.toByteArray(); + if (bytes[0] == 0) { + byte[] trimmedBytes = new byte[bytes.length - 1]; + System.arraycopy(bytes, 1, trimmedBytes, 0, trimmedBytes.length); + return trimmedBytes; + } + return bytes; + } + +} diff --git a/src/it/HackerInside/TextEncryptionUtility/Settings_Window.java b/src/it/HackerInside/TextEncryptionUtility/Settings_Window.java index b18e56a..fd50e67 100644 --- a/src/it/HackerInside/TextEncryptionUtility/Settings_Window.java +++ b/src/it/HackerInside/TextEncryptionUtility/Settings_Window.java @@ -130,7 +130,7 @@ private void initialize() { cmbEncoding.setBounds(93, 11, 200, 36); panel.add(cmbEncoding); - cmbEncoding.setModel(new DefaultComboBoxModel(new String[] {"Base64", "Base58", "Hex", "PGP Word list"})); + cmbEncoding.setModel(new DefaultComboBoxModel(new String[] {"Base64", "Base58", "Hex", "PGP Word list", "Base36"})); JCheckBox chckbCompression = new JCheckBox("GZIP Compression"); chckbCompression.setBackground(Color.WHITE); @@ -259,7 +259,7 @@ public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) { if(!txtbAliasChiave.getText().toString().isEmpty()) { String walletpwd = passwordInput("Keystore Password"); - if(chckbxChiaveEsterna.isSelected()) { + if(chckbxChiaveEsterna.isSelected() && !walletpwd.equalsIgnoreCase("")) { SecretKey originalKey = new SecretKeySpec(hexStringToByteArray(txtbSecretKey.getText().toString()), 0,hexStringToByteArray(txtbSecretKey.getText().toString()).length , "AES"); try { KeyStoreUtils.addSecretKey(TextEncryptionUtil_Main.ks, walletpwd, txtbAliasChiave.getText().toString(), originalKey); @@ -282,12 +282,20 @@ public void actionPerformed(ActionEvent e) { e1.printStackTrace(); } JOptionPane.showMessageDialog(null, "Key Added!"); - try { - TextEncryptionUtil_Main.ks = KeyStoreUtils.loadKeyStore(walletpwd,TextEncryptionUtil_Main.keyStoreFile); // Reload Keystore - } catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | IOException e1) { - // TODO Auto-generated catch block - e1.printStackTrace(); + + if(!walletpwd.equalsIgnoreCase("")) { + try { + TextEncryptionUtil_Main.ks = KeyStoreUtils.loadKeyStore(walletpwd,TextEncryptionUtil_Main.keyStoreFile); // Reload Keystore + TextEncryptionUtil_Main.keyStorePassword = walletpwd; + } catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + }else { + JOptionPane.showMessageDialog(null, "Empty keystore password!"); } + }else { JOptionPane.showMessageDialog(null, "Empty key alias!"); } diff --git a/src/it/HackerInside/TextEncryptionUtility/TextEncryptionUtil_Main.java b/src/it/HackerInside/TextEncryptionUtility/TextEncryptionUtil_Main.java index bee23b1..2b5ce40 100644 --- a/src/it/HackerInside/TextEncryptionUtility/TextEncryptionUtil_Main.java +++ b/src/it/HackerInside/TextEncryptionUtility/TextEncryptionUtil_Main.java @@ -342,6 +342,8 @@ else if(encoding == 2) encrypted = AES256.encryptDecryptString(Cipher.ENCRYPT_MODE, text, key,"hex",compression); else if(encoding == 3) encrypted = AES256.encryptDecryptString(Cipher.ENCRYPT_MODE, text, key,"pgpWordlist",compression); + else if(encoding == 4) + encrypted = AES256.encryptDecryptString(Cipher.ENCRYPT_MODE, text, key,"base36",compression); return encrypted; @@ -361,6 +363,8 @@ else if(encoding == 2) else if(encoding == 3) decrypted = AES256.encryptDecryptString(Cipher.DECRYPT_MODE, text, key,"pgpWordlist",compression); + else if(encoding == 4) + decrypted = AES256.encryptDecryptString(Cipher.DECRYPT_MODE, text, key,"base36",compression); return decrypted; }