Skip to content

Commit

Permalink
Miglioramenti generali di sicurezza
Browse files Browse the repository at this point in the history
Aggiunta Bouncy Castle e adozione del formato BCFKS per il keystore, miglioramento alla funzione di Zeroize, modificata l'estensione del keystore di default
  • Loading branch information
FrancescoValentini committed Aug 22, 2023
1 parent 9704258 commit 5c06255
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="miglayout15-swing.jar" sourcepath="miglayout-src.zip"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/bcprov-ext-jdk18on-176"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ local.properties


# Keystore
.jks
.jks
.bcfks
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.security.Key;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.spec.InvalidKeySpecException;
Expand Down Expand Up @@ -287,7 +288,7 @@ public void actionPerformed(ActionEvent e) {
try {
TextEncryptionUtil_Main.ks = KeyStoreUtils.loadKeyStore(walletpwd,TextEncryptionUtil_Main.keyStoreFile); // Reload Keystore
TextEncryptionUtil_Main.keyStorePassword = walletpwd;
} catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | IOException e1) {
} catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | IOException | NoSuchProviderException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.security.cert.CertificateException;
import java.util.Enumeration;
import java.util.Random;
Expand All @@ -44,6 +46,8 @@
import javax.swing.event.PopupMenuListener;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.swing.event.PopupMenuEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
Expand All @@ -57,7 +61,7 @@ public class TextEncryptionUtil_Main {

private JFrame frmHackerinsideTextEncryption;
// Impostazioni KeyStore
public static String keyStoreFile = "KeyStore.jks";
public static String keyStoreFile = "KeyStore.bcfks";
public static KeyStore ks;
public static boolean keystoreExist = false;
static String keyStorePassword = "";
Expand All @@ -67,6 +71,7 @@ public class TextEncryptionUtil_Main {
* Launch the application.
*/
public static void main(String[] args) {
Security.addProvider(new BouncyCastleProvider());
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Expand Down Expand Up @@ -97,7 +102,7 @@ public TextEncryptionUtil_Main() {
try {
ks = KeyStoreUtils.loadKeyStore(password,keyStoreFile);
keystoreExist = true;
} catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | IOException e) {
} catch (NoSuchAlgorithmException | CertificateException | KeyStoreException | IOException | NoSuchProviderException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.toString());
System.exit(-1);;
Expand All @@ -106,7 +111,7 @@ public TextEncryptionUtil_Main() {
JOptionPane.showMessageDialog(null, "KeyStore not found, a new one will be created.");
try {
keyStoreWizard();
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Expand Down Expand Up @@ -390,9 +395,9 @@ public static void zeroize() throws IOException { // Emergency Zeroize
ks.deleteEntry(alias);

}
KeyStoreUtils.saveKeyStore(ks,"0000",keyStoreFile);
KeyStoreUtils.saveKeyStore(ks,randomString(128),keyStoreFile);
zeroize(keyStoreFile);

ks = null;

} catch (Exception e1) {
// TODO Auto-generated catch block
Expand All @@ -405,15 +410,19 @@ public static void zeroize() throws IOException { // Emergency Zeroize
}

}

public static boolean twoFactor() { // Codice One Time
public static String randomString(int len) { // Genera stringhe casuali
char[] chars = "ABCDEFGHJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
Random rnd = new Random();
SecureRandom rnd = new SecureRandom();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++)
for (int i = 0; i < len; i++)
sb.append(chars[rnd.nextInt(chars.length)]);

return sb.toString();
}

String generated = sb.toString();
public static boolean twoFactor() { // Codice One Time
String generated = randomString(5);
JTextField pwd = new JTextField(10);
JOptionPane.showMessageDialog(null,"OTP: " + generated);
int action = JOptionPane.showConfirmDialog(null, pwd,"OTP",JOptionPane.OK_CANCEL_OPTION);
Expand Down Expand Up @@ -449,11 +458,11 @@ public static boolean twoFactor() { // Codice One Time
}


public static void keyStoreWizard() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { // Configurazione iniziale di un nuovo keystore
public static void keyStoreWizard() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, NoSuchProviderException { // Configurazione iniziale di un nuovo keystore
String pwd = passwordInput("New KeyStore password");
ks = KeyStore.getInstance("JCEKS");
ks.load(null, pwd.toCharArray());
KeyStoreUtils.saveKeyStore(ks,pwd,keyStoreFile);

ks = KeyStoreUtils.newKeystore(pwd, keyStoreFile);

JOptionPane.showMessageDialog(null,"KeyStore initialized successfully.");
}

Expand Down

0 comments on commit 5c06255

Please sign in to comment.