You are on page 1of 10

ESTEGANOGRAFIA MIDI:

Jefferson Jaimes Bernate - CRIPTOGRAFIA


La aplicacin consta de 7 elementos, desarrollados en Java:

La aplicacin a travs de la interfaces tiene las siguientes opciones:


1. Permite cargar un archivo midi.
2. Permite Guardar un mensaje (Metamensaje tipo 1) en el archivo.
3. Puede reproducir (play and stop) el archivo.
4. Permite visualizar el texto guardado en el archivo.
5. Permite Cifrar el texto o incluir al archivo midi un mensaje en forma de texto claro.

Pantalla de la aplicacin.

Instrucciones de USO:
- Al momento del guardar el mensaje puede generar un mensaje cifrado para ser guardado en el archivo
midi.
- Luego de cifrar para descifrar puede volver a cifrar con la misma clave para ver el texto claro
nuevamente.
- La clave deben ser nmeros. Se hizo con nmeros solo para ilustrar el cambio de los caracteres una vez
se realiza la operacin XOR.
Modulo CargarMensaje.java
Recibe un String y un elemento tipo File, y utilizando unos de los tipos de MetaMessage (Tipo 1)
disponibles en el estndar midi.
##################################################################################################
public class CargarMensaje {

private File archivo;

public boolean guardar(File music, String TrackName) {

try {

Sequence musicS = MidiSystem.getSequence(music);


musicS.getTracks();

//General MIDI sysex;


MetaMessage mt = new MetaMessage();
byte[] b = {(byte) 0xF0, 0x7E, 0x7F, 0x09, 0x01, (byte) 0xF7};
SysexMessage sm = new SysexMessage();
sm.setMessage(b, 6);
MidiEvent me = new MidiEvent(sm, (long) 0);

//Se define el tipo de mensaje (0x01) a grabar y se adiciona al archivo;

mt = new MetaMessage();
mt.setMessage(0x01, TrackName.getBytes(), TrackName.length());
me = new MidiEvent(mt, (long) 0);
musicS.createTrack().add(me);
JFileChooser filemidi = new JFileChooser();
filemidi.showSaveDialog(filemidi);
this.archivo = filemidi.getSelectedFile();

//Genera el archivo name.mid;

File f = new File(archivo + ".mid");


MidiSystem.write(musicS, 1, f);
JOptionPane.showMessageDialog(null, "Se grab el mensaje");
} catch (Exception e) {
System.out.println("Exception caught " + e.toString());
} //catch

return true;
}

Sobre esta imagen es posible ver como es la estructura del archivo midi que se modifica con la aplicacin, el
cual escribe al final el Track con el mensaje:
FUENTE DE INFORMACION:
http://www.automatic-pilot.com/WEB-INF/classes/MidiApps/hardCodedComposer.java

##################################################################################################

Modulo CryptoMsj.java

Este mdulo permite cifrar el texto que se incluye en el archivo midi, por default la aplicacin puede incluir el
texto claro en el archivo. Realiza una XOR una clave de nmeros que el usuario puede indicar. La clave debe
ser al menos del mismo tamao del mensaje.

##################################################################################################

public class CryptoMsj {


byte cifrado[];
String cipher="";
public String cifrando (String clave, String cleartext) {
System.out.println("clave: " + clave + " texto: " + cleartext);
byte[] cifra = clave.getBytes();
byte[] msj = cleartext.getBytes();
cipher="";
if (cifra.length >= msj.length) {
cifrado= new byte[msj.length];
for (int i=0; i<msj.length; i++){
cifrado[i] = (byte) (msj[i]^cifra[i]);
cipher = cipher + (char)cifrado[i];
System.out.println(cifrado[i]);
}
}
System.out.println("El texto cifrado: "+cipher);

return (cipher);
}

##################################################################################################
Modulo MetaEvent.java

En un sub-modulo que permite capturar el mensaje tipo1 de archivo midi.


##################################################################################################

public class MetaEvent implements MetaEventListener {

private String message;

@Override
public void meta(MetaMessage meta) {
String mensaje ="";
final int type = meta.getType();
//byte [] msj = meta.getMessage();
if(type==1){
System.out.println("MEL - type: " + type );

for(byte m:meta.getMessage()){
mensaje = mensaje + (char)m;

}
System.out.println("Mensaje: " + mensaje );
this.setMessage(mensaje);
}
}

public String getMessage() {


return message;
}

public void setMessage(String message) {


this.message = message;
}
}
##################################################################################################

Modulo PlayMidi.java

Es el modulo que permite reproducir o detener la reproduccin del archivo midi:


##################################################################################################

public class PlayMidi {

private Sequencer sequencer;

public boolean reproducir(File filemidi) throws MidiUnavailableException, InvalidMidiDataException, IOException {


this.sequencer = MidiSystem.getSequencer();
sequencer.setSequence(MidiSystem.getSequence(filemidi));
sequencer.open();
sequencer.start();

return (true);

public boolean stop() {

this.sequencer.stop();
return true;
}
public Sequencer getSequencer() {
return sequencer;
}

public void setSequencer(Sequencer sequencer) {


this.sequencer = sequencer;
}

}
##################################################################################################

Modulo Presenta1.java

Es el modulo donde se realiza toda la programacin de la interface:

##################################################################################################

public class Presenta1 extends javax.swing.JFrame {

PlayMidi playMidi = new PlayMidi();


CargarMensaje saveMsj = new CargarMensaje();
ReadMetaMensaje readMsj = new ReadMetaMensaje();
CryptoMsj cifrar = new CryptoMsj();
ValidarClave validarKey= new ValidarClave();
private File globalfile;

/**
* Creates new form Presenta1
*/
public Presenta1() {
initComponents();
}

/**

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:

private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:


}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.playMidi.stop();

// TODO add your handling code here:


}

private void mostrarMensaje(String guardado) {

JOptionPane.showMessageDialog(null, "Ya puede ver el mensaje!");


jTextField3.setText(guardado);

private void bcifrarActionPerformed(java.awt.event.ActionEvent evt) {


jTextField4.setText("");
if (jTextField5.getText().length()>= jTextField1.getText().length()){
if ((validarKey.claveOk( jTextField5.getText(), jTextField1.getText()))==false) {
jTextField4.setText(cifrar.cifrando(jTextField5.getText(), jTextField1.getText()));

}
else
JOptionPane.showMessageDialog(null, "La clave deben ser numeros");

} else
JOptionPane.showMessageDialog(null, "texto y clave almenos de igual tamao!");

// TODO add your handling code here:


}

private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void CargarMidiActionPerformed(java.awt.event.ActionEvent evt) {


JFileChooser filemidi = new JFileChooser();
//filemidi.addChoosableFileFilter(new );
int returnVal = filemidi.showOpenDialog(this);
// TODO add your handling code here:

this.globalfile = filemidi.getSelectedFile();

jTextField2.setText(globalfile.getName());
jTextField2.setBackground(Color.GREEN);
jTextField1.setText("");
jTextField3.setText("");

System.out.println("archivoooo cargado " + globalfile.getName());


}

private void VerMsgActionPerformed(java.awt.event.ActionEvent evt) {

jTextField3.setText("");
System.out.println("archivoooo ver " + globalfile.getName());
if (globalfile != null && globalfile.getName() != null) {
try {
jTextField3.setText(readMsj.leer(globalfile));
System.out.println("texto de archivo " + jTextField3.getText());

} catch (InvalidMidiDataException ex) {


Logger.getLogger(Presenta1.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Presenta1.class.getName()).log(Level.SEVERE, null, ex);
} catch (MidiUnavailableException ex) {
Logger.getLogger(Presenta1.class.getName()).log(Level.SEVERE, null, ex);
}

private void GuardarMsgActionPerformed(java.awt.event.ActionEvent evt) {

String mensaje = jTextField1.getText();


saveMsj.guardar(globalfile, mensaje);

// TODO add your handling code here:


}

private void PlayActionPerformed(java.awt.event.ActionEvent evt) {

try {
playMidi.reproducir(globalfile);

} catch (MidiUnavailableException ex) {


Logger.getLogger(Presenta1.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidMidiDataException ex) {
Logger.getLogger(Presenta1.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Presenta1.class.getName()).log(Level.SEVERE, null, ex);
}
}

private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Presenta1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Presenta1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Presenta1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Presenta1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Presenta1().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton CargarMidi;
private javax.swing.JButton GuardarMsg;
private javax.swing.JButton Play;
private javax.swing.JButton VerMsg;
private javax.swing.JButton bcifrar;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
// End of variables declaration

private void and(boolean b) {


throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
##################################################################################################
Modulo ReadMetaMensaje.java

Este permite lanzar la lectura del Metamensaje tipo 1 sobre el archivo que se ha seleccionado previamente.
##################################################################################################

public class ReadMetaMensaje {

private String mensaje = "";

public String leer(File path) throws InvalidMidiDataException, IOException, MidiUnavailableException {

Sequence sequence = MidiSystem.getSequence(path);


try (Sequencer sequencer = MidiSystem.getSequencer()) {
sequencer.open();

MetaEvent metaEvent=new MetaEvent();

sequencer.addMetaEventListener(metaEvent);

sequencer.setSequence(sequence);

sequencer.start();
Thread.sleep(1000L);
sequencer.stop();

// System.out.println("HOLAAAAAAAAAAAA BEBE"+metaEvent.getMessage());
return metaEvent.getMessage();

} catch (InterruptedException ex) {


Logger.getLogger(ReadMetaMensaje.class.getName()).log(Level.SEVERE, null, ex);
}
return "";
}
}
##################################################################################################

Modulo ValidarClave.java

Se decidi utilizar una clave para cifrar los mensajes que se quieran incluir en el archivo con una XOR, pero
esta clave debe ser solo numrica, este mdulo se encarga de validar que sea as:
##################################################################################################

public class ValidarClave {

private int count = 0;


private boolean revalidar = false;

public boolean claveOk(String clave, String texto) {


String base10 ="0123456789";
count=0;
for (int i = 0; i < clave.length(); i++) {

for (int j = 0; j < 9; j++) {


if (clave.charAt(i) == base10.charAt(j)) {
count = count +1;
System.out.println(" Cuenta: "+ count);
}
}
}
if (count != clave.length()) {
revalidar = true;
} else
revalidar = false;

return revalidar;
}
}
##################################################################################################

You might also like