Skip to content

Commit

Permalink
Implementazione Base36 e correzione bug
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
FrancescoValentini committed Mar 17, 2023
1 parent 4a6961f commit 27de43f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion src/it/HackerInside/TextEncryptionUtility/AES256.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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];
Expand Down
31 changes: 31 additions & 0 deletions src/it/HackerInside/TextEncryptionUtility/Base36.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
22 changes: 15 additions & 7 deletions src/it/HackerInside/TextEncryptionUtility/Settings_Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 27de43f

Please sign in to comment.