Il funzionamento è più o meno questo: vengono presi in input un file e una password (che più che una password è una "chiave"). La password viene tradotta carattere per carattere in byte, questi byte vengono sommati e il risultato viene ri-convertito in un byte. Dopodiché ogni singolo byte del file preso in input viene incrementato del valore del byte-password. Per decriptare i byte vengono decrementati dello stesso valore.
Funziona su qualunque tipo di file.
file KryptosMain.java:
Codice: Seleziona tutto
/*
Copyright 2008, 2009 Sebastiano Tronto <sebastiano@luganega.org>
This file is part of Kryptos.
Kryptos is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kryptos is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Nome-Programma; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
import java.util.Scanner;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class KryptosMain {
public static int contatore;
public static String pswd, percorso;
public static byte[] percorsoConEstensione, percorsoSenzaEstensione;
public static FileInputStream fileInput;
public static FileOutputStream fileOutput;
public static Scanner sc = new Scanner(System.in);
public static KryptosFrame frame;
public static void main ( String args[] ) {
frame = new KryptosFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize( 350, 150 );
frame.setVisible( true );
}
public static void Cripta( String percorso, String password ) {
try {
contatore = 0;
fileInput = new FileInputStream(percorso);
byte[] arrayStringa = password.getBytes();
for (int i = 0; i < arrayStringa.length; i++)
contatore += arrayStringa[i];
fileOutput = new FileOutputStream( percorso + ".krp" );
byte[] byteNonModificati = new byte[fileInput.available()];
fileInput.read( byteNonModificati );
int[] byteModificati = new int[byteNonModificati.length];
byte[] byteModificatiB = new byte[byteNonModificati.length];
for ( int i=0; i<byteNonModificati.length; i++ ) {
byteModificati[i] = (int)byteNonModificati[i] + contatore;
byteModificatiB[i] = (byte)byteModificati[i];
}
fileOutput.write(byteModificatiB);
JOptionPane.showMessageDialog( null, "Criptazione eseguita con succeso\n Il file criptato è:\n " + KryptosFrame.campoDiTesto.getText() + ".krp\n\nIl vecchio file NON è stato cancellato", "Kryptos - Informazione", JOptionPane.INFORMATION_MESSAGE );
} catch ( FileNotFoundException e ) {
JOptionPane.showMessageDialog( null, "Errore: file non trovato\naccertarsi di aver inserito il percorso corretto", "Kryptos - Errore", JOptionPane.ERROR_MESSAGE );
} catch ( IOException e ) {
JOptionPane.showMessageDialog( null, "Errore non previsto di Input/Output\nPer favore riportare il bug a sebastiano@luganega.org", "Kryptos - Errore", JOptionPane.ERROR_MESSAGE );
} catch ( Exception e ) {
JOptionPane.showMessageDialog( null, "Errore sconosciuto:\n\n" + e + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Kryptos - Errore", JOptionPane.ERROR_MESSAGE );
}
}
public static void Decripta( String percorso, String password ) {
try {
contatore = 0;
fileInput = new FileInputStream(percorso);
byte[] arrayStringa = password.getBytes();
percorsoConEstensione = percorso.getBytes();
percorsoSenzaEstensione = new byte[percorsoConEstensione.length - 4];
for ( int i = 0; i < percorsoSenzaEstensione.length; i++ )
percorsoSenzaEstensione[i] = percorsoConEstensione[i];
percorso = new String( percorsoSenzaEstensione );
for ( int i = 0; i < arrayStringa.length; i++ )
contatore += arrayStringa[i];
fileOutput = new FileOutputStream( percorso );
byte[] byteNonModificatiDec = new byte[fileInput.available()];
fileInput.read(byteNonModificatiDec);
int[] byteModificatiDec = new int[byteNonModificatiDec.length];
byte[] byteModificatiBDec = new byte[byteNonModificatiDec.length];
for (int i = 0; i < byteNonModificatiDec.length; i++) {
byteModificatiDec[i] = (int)byteNonModificatiDec[i] - contatore;
byteModificatiBDec[i] = (byte)byteModificatiDec[i];
}
fileOutput.write(byteModificatiBDec);
JOptionPane.showMessageDialog( null, "Decriptazione eseguita con succeso\n Il file decriptato è:\n " + percorso + "\n\nIl vecchio file NON è stato cancellato", "Kryptos - Informazione", JOptionPane.INFORMATION_MESSAGE );
} catch ( FileNotFoundException e ) {
JOptionPane.showMessageDialog( null, "Errore: file non trovato\naccertarsi di aver inserito il percorso corretto", "Kryptos - Errore", JOptionPane.ERROR_MESSAGE );
} catch ( IOException e ) {
JOptionPane.showMessageDialog( null, "Errore non previsto di Input/Output\nPer favore riportare il bug a <html><i>sebastiano@luganega.org", "Kryptos - Errore", JOptionPane.ERROR_MESSAGE );
} catch ( Exception e ) {
JOptionPane.showMessageDialog( null, "Errore sconosciuto:\n\n" + e + "\n\nPer favore riportare il bug a <html><i>sebastiano@luganega.org", "Kryptos - Errore", JOptionPane.ERROR_MESSAGE );
}
}
}Codice: Seleziona tutto
/*
Copyright 2008, 2009 Sebastiano Tronto <sebastiano@luganega.org>
This file is part of Kryptos.
Kryptos is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kryptos is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Nome-Programma; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
import javax.swing.*;
import javax.swing.filechooser.*;
import java.awt.*;
import java.awt.event.*;
public class KryptosFrame extends JFrame {
private class Listener implements ActionListener {
public void actionPerformed( ActionEvent e ) {
if ( e.getSource() == Exit ) {
System.exit(0);
} else
if ( e.getSource() == informazioni ) {
JOptionPane.showMessageDialog( null, "Kryptos is free software; you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation; either version 2 of the License, or\n(at your option) any later version.\n\nKryptos is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with Nome-Programma; if not, write to the Free Software\nFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n\n<html><i>Copyright 2008, 2009 Sebastiano Tronto sebastiano@luganega.org</i></html>", "Kryptos - informazioni", JOptionPane.INFORMATION_MESSAGE );
} else
if ( e.getSource() == selezione ) {
switch ( selezione.getSelectedIndex() ) {
case 0:
chooser.removeChoosableFileFilter( filter );
chooser.removeChoosableFileFilter( chooser.getAcceptAllFileFilter() );
chooser.addChoosableFileFilter( chooser.getAcceptAllFileFilter() );
break;
case 1:
chooser.addChoosableFileFilter( filter );
break;
}
} else
if ( e.getSource() == sfoglia ) {
valoreChooser = chooser.showOpenDialog( KryptosMain.frame );
switch ( valoreChooser ) {
case JFileChooser.APPROVE_OPTION:
campoDiTesto.setText( chooser.getSelectedFile().getPath() );
break;
case JFileChooser.ERROR_OPTION:
JOptionPane.showMessageDialog( null, "Errore sconosciuto:\n\n" + e + "\n\nPer favore riportare il bug a sebastiano@luganega.org", "Kryptos - Errore", JOptionPane.ERROR_MESSAGE );
break;
//case JFileChooser.CANCEL_OPTION:
}
} else
if ( e.getSource() == vai || e.getSource() == campoDiTesto || e.getSource() == campoPassword ) {
if ( campoPassword.getText().equals( "" ) )
JOptionPane.showMessageDialog( null, "Password non impostata: è necessaria una password come ''chiave'' per la criptazione/decriptazione del file", "Kryptos - Avviso", JOptionPane.WARNING_MESSAGE );
switch ( selezione.getSelectedIndex() ) {
case 0:
KryptosMain.Cripta( campoDiTesto.getText(), campoPassword.getText() );
break;
case 1:
KryptosMain.Decripta( campoDiTesto.getText(), campoPassword.getText() );
break;
}
}
}
}
Listener listener = new Listener();
KryptosMain main = new KryptosMain();
JFileChooser chooser = new JFileChooser();
public static JPanel pannelli[] = new JPanel[3];
public static JLabel testo1, testo2, testo3;
public static JTextField campoDiTesto;
public static JPasswordField campoPassword;
public static JButton sfoglia, vai;
public static JComboBox selezione;
public static FileNameExtensionFilter filter = new FileNameExtensionFilter( "File KRP", "krp" );
public static String arraySelezione[] = { "Cripta", "Decripta" };
public static boolean boolSelezione = true;
public static int valoreChooser;
public static MenuBar barraMenu = new MenuBar();
public static Menu file = new Menu( "File" );
public static Menu info = new Menu( "?" );
public static MenuItem Exit = new MenuItem( "Esci" );
public static MenuItem informazioni = new MenuItem( "Informazioni" );
public KryptosFrame() {
super( "Porky's Kryptos - versione 3.2" );
setMenuBar( barraMenu );
barraMenu.add( file );
barraMenu.add( info );
file.add( Exit );
info.add( informazioni );
Exit.addActionListener( listener );
informazioni.addActionListener( listener );
for ( int i = 0; i < pannelli.length; i++ )
pannelli[i] = new JPanel();
pannelli[0].setLayout( new FlowLayout() );
add( pannelli[0], BorderLayout.NORTH );
pannelli[1].setLayout( new FlowLayout() );
add( pannelli[1], BorderLayout.CENTER );
pannelli[2].setLayout( new FlowLayout() );
add( pannelli[2], BorderLayout.SOUTH );
testo1 = new JLabel( "Benvenuti in Kryptos, Il criptatore!" );
testo2 = new JLabel( "File: " );
testo3 = new JLabel( "Password: " );
campoDiTesto = new JTextField( "Inserire il percorso del file", 15 );
campoPassword = new JPasswordField( 9 );
sfoglia = new JButton( "Sfoglia" );
vai = new JButton( "Vai!" );
selezione = new JComboBox( arraySelezione );
pannelli[0].add( testo1 );
pannelli[1].add( testo2 );
pannelli[1].add( campoDiTesto );
pannelli[1].add( sfoglia );
pannelli[2].add( testo3 );
pannelli[2].add( campoPassword );
pannelli[2].add( selezione );
pannelli[2].add( vai );
sfoglia.addActionListener( listener );
vai.addActionListener( listener );
campoDiTesto.addActionListener( listener );
campoPassword.addActionListener( listener );
selezione.addActionListener( listener );
}
}una volta scaricato il file eseguite Esegui.bat (windos) o Esegui.sh (Linux (dopo aver dato al file i permessi di esecuzione) )
Versione testuale (stesso identico funzionamento ma senza grafica): http://porkynator.altervista.org/Kryptos-2.0.zip
Si accettano commenti/critiche/altro


