Foro de elhacker.net

Programación => Java => Mensaje iniciado por: Debci en 15 Noviembre 2009, 17:39 pm



Título: 2o Problema, encriptador con GUI
Publicado por: Debci en 15 Noviembre 2009, 17:39 pm
HOla a todos, antes encontré una clase que encriptaba texto  :o y le hice una gui, la cosa es que la gui cifra bien, pero no descifra!

Codigo de la gui:

Código
  1. package main;
  2.  
  3. /**
  4.  *
  5.  * @author debci
  6.  */
  7. import encriptador.*;
  8. public class Main extends javax.swing.JFrame {
  9. public static String contraseñaEncriptada;
  10.    /** Creates new form Main */
  11.    public Main() {
  12.        initComponents();
  13.    }
  14.  
  15.    /** This method is called from within the constructor to
  16.      * initialize the form.
  17.      * WARNING: Do NOT modify this code. The content of this method is
  18.      * always regenerated by the Form Editor.
  19.      */
  20.    @SuppressWarnings("unchecked")
  21.    // <editor-fold defaultstate="collapsed" desc="Generated Code">
  22.    private void initComponents() {
  23.  
  24.        jTabbedPane1 = new javax.swing.JTabbedPane();
  25.        jPanel1 = new javax.swing.JPanel();
  26.        jLabel1 = new javax.swing.JLabel();
  27.        textoIntroducido = new javax.swing.JTextField();
  28.        claveIntroducida = new javax.swing.JTextField();
  29.        jLabel2 = new javax.swing.JLabel();
  30.        btnEncriptar = new javax.swing.JButton();
  31.        contraseEncr = new javax.swing.JLabel();
  32.        jLabel3 = new javax.swing.JLabel();
  33.        jPanel2 = new javax.swing.JPanel();
  34.        btnDesencriptar = new javax.swing.JButton();
  35.        entradaHash = new javax.swing.JTextField();
  36.        entradaClave = new javax.swing.JTextField();
  37.        jLabel4 = new javax.swing.JLabel();
  38.        jLabel5 = new javax.swing.JLabel();
  39.        jLabel6 = new javax.swing.JLabel();
  40.        clavedesencriptada = new javax.swing.JLabel();
  41.        jMenuBar1 = new javax.swing.JMenuBar();
  42.        jMenu1 = new javax.swing.JMenu();
  43.        jMenu2 = new javax.swing.JMenu();
  44.        jMenuItem1 = new javax.swing.JMenuItem();
  45.  
  46.        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  47.        setTitle("Encriptador Basico");
  48.  
  49.        jLabel1.setText("Introduzca el texto que desea cifrar:");
  50.  
  51.        jLabel2.setText("Introduzca la clave de cifrado:");
  52.  
  53.        btnEncriptar.setText("cifrar");
  54.        btnEncriptar.addActionListener(new java.awt.event.ActionListener() {
  55.            public void actionPerformed(java.awt.event.ActionEvent evt) {
  56.                btnEncriptarActionPerformed(evt);
  57.            }
  58.        });
  59.  
  60.        contraseEncr.setText("Contraseña cifrada");
  61.        contraseEncr.setToolTipText("Contraseña cifrada");
  62.  
  63.        jLabel3.setText("Contraseña cifrada:");
  64.  
  65.        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  66.        jPanel1.setLayout(jPanel1Layout);
  67.        jPanel1Layout.setHorizontalGroup(
  68.            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  69.            .addGroup(jPanel1Layout.createSequentialGroup()
  70.                .addContainerGap()
  71.                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  72.                    .addComponent(textoIntroducido, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
  73.                    .addComponent(jLabel2)
  74.                    .addComponent(claveIntroducida, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
  75.                    .addComponent(jLabel1)
  76.                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  77.                        .addComponent(contraseEncr)
  78.                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
  79.                        .addComponent(btnEncriptar))
  80.                    .addComponent(jLabel3))
  81.                .addContainerGap())
  82.        );
  83.        jPanel1Layout.setVerticalGroup(
  84.            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  85.            .addGroup(jPanel1Layout.createSequentialGroup()
  86.                .addContainerGap()
  87.                .addComponent(jLabel1)
  88.                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  89.                .addComponent(textoIntroducido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  90.                .addGap(18, 18, 18)
  91.                .addComponent(jLabel2)
  92.                .addGap(7, 7, 7)
  93.                .addComponent(claveIntroducida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  94.                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  95.                .addComponent(jLabel3)
  96.                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  97.                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  98.                    .addComponent(btnEncriptar)
  99.                    .addComponent(contraseEncr))
  100.                .addContainerGap())
  101.        );
  102.  
  103.        contraseEncr.getAccessibleContext().setAccessibleParent(this);
  104.  
  105.        jTabbedPane1.addTab("cifrar", jPanel1);
  106.  
  107.        btnDesencriptar.setText("descifrar");
  108.        btnDesencriptar.addActionListener(new java.awt.event.ActionListener() {
  109.            public void actionPerformed(java.awt.event.ActionEvent evt) {
  110.                btnDesencriptarActionPerformed(evt);
  111.            }
  112.        });
  113.  
  114.        jLabel4.setText("Clave cifrada:");
  115.  
  116.        jLabel5.setText("Clave de descifrado:");
  117.  
  118.        jLabel6.setText("La clave descifrada es:");
  119.  
  120.        clavedesencriptada.setText("Clave descifrada");
  121.  
  122.        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
  123.        jPanel2.setLayout(jPanel2Layout);
  124.        jPanel2Layout.setHorizontalGroup(
  125.            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  126.            .addGroup(jPanel2Layout.createSequentialGroup()
  127.                .addContainerGap()
  128.                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  129.                    .addGroup(jPanel2Layout.createSequentialGroup()
  130.                        .addComponent(jLabel4)
  131.                        .addContainerGap(167, Short.MAX_VALUE))
  132.                    .addGroup(jPanel2Layout.createSequentialGroup()
  133.                        .addComponent(entradaHash, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
  134.                        .addGap(87, 87, 87))
  135.                    .addGroup(jPanel2Layout.createSequentialGroup()
  136.                        .addComponent(jLabel5)
  137.                        .addContainerGap(124, Short.MAX_VALUE))
  138.                    .addGroup(jPanel2Layout.createSequentialGroup()
  139.                        .addComponent(entradaClave, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
  140.                        .addGap(87, 87, 87))
  141.                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
  142.                        .addComponent(clavedesencriptada)
  143.                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
  144.                        .addComponent(btnDesencriptar))
  145.                    .addGroup(jPanel2Layout.createSequentialGroup()
  146.                        .addComponent(jLabel6)
  147.                        .addContainerGap(108, Short.MAX_VALUE))))
  148.        );
  149.        jPanel2Layout.setVerticalGroup(
  150.            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  151.            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
  152.                .addContainerGap()
  153.                .addComponent(jLabel4)
  154.                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  155.                .addComponent(entradaHash, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  156.                .addGap(18, 18, 18)
  157.                .addComponent(jLabel5)
  158.                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  159.                .addComponent(entradaClave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  160.                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  161.                .addComponent(jLabel6)
  162.                .addGap(7, 7, 7)
  163.                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  164.                    .addComponent(btnDesencriptar)
  165.                    .addComponent(clavedesencriptada))
  166.                .addContainerGap())
  167.        );
  168.  
  169.        jTabbedPane1.addTab("descifrar", jPanel2);
  170.  
  171.        jMenu1.setText("Archivo");
  172.        jMenuBar1.add(jMenu1);
  173.  
  174.        jMenu2.setText("Acerca de...");
  175.        jMenu2.addActionListener(new java.awt.event.ActionListener() {
  176.            public void actionPerformed(java.awt.event.ActionEvent evt) {
  177.                jMenu2ActionPerformed(evt);
  178.            }
  179.        });
  180.  
  181.        jMenuItem1.setText("Ver creditos del programa");
  182.        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
  183.            public void actionPerformed(java.awt.event.ActionEvent evt) {
  184.                jMenuItem1ActionPerformed(evt);
  185.            }
  186.        });
  187.        jMenu2.add(jMenuItem1);
  188.  
  189.        jMenuBar1.add(jMenu2);
  190.  
  191.        setJMenuBar(jMenuBar1);
  192.  
  193.        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  194.        getContentPane().setLayout(layout);
  195.        layout.setHorizontalGroup(
  196.            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  197.            .addGroup(layout.createSequentialGroup()
  198.                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
  199.                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  200.        );
  201.        layout.setVerticalGroup(
  202.            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  203.            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
  204.        );
  205.  
  206.        pack();
  207.    }// </editor-fold>
  208.  
  209.    private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {                                      
  210.  
  211.    }                                      
  212.  
  213.    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  214.        Acerca acerca = new Acerca();
  215.        acerca.setVisible(true);
  216.    }                                          
  217.  
  218.    private void btnEncriptarActionPerformed(java.awt.event.ActionEvent evt) {
  219.        String palabraNormal = textoIntroducido.getText();
  220.        String ClaveIntroducida = claveIntroducida.getText();
  221.  
  222.  
  223.        encriptador encriptador = new encriptador();
  224.        contraseñaEncriptada = encriptador.codificar(palabraNormal, ClaveIntroducida).trim();
  225.        contraseEncr.setText(contraseñaEncriptada);
  226.        contraseEncr.setVisible(true);
  227.  
  228.    }
  229.  
  230.    private void btnDesencriptarActionPerformed(java.awt.event.ActionEvent evt) {
  231.        String hashIntroducido = entradaHash.getText();
  232.        String claveDesencriptadora = entradaClave.getText();
  233.        entradaHash.setText(contraseñaEncriptada);
  234.        encriptador encriptador = new encriptador();
  235.        String claveDesencriptadaLimpia = encriptador.decodificar(hashIntroducido);
  236.        clavedesencriptada.setText(claveDesencriptadaLimpia);
  237.    }
  238.  
  239.    /**
  240.     * @param args the command line arguments
  241.     */
  242.    public static void main(String args[]) {
  243.        java.awt.EventQueue.invokeLater(new Runnable() {
  244.  
  245.            public void run() {
  246.                new Main().setVisible(true);
  247.                contraseEncr.setVisible(false);
  248.                clavedesencriptada.setVisible(false);
  249.            }
  250.        });
  251.    }
  252.  
  253.    // Variables declaration - do not modify
  254.    private javax.swing.JButton btnDesencriptar;
  255.    private javax.swing.JButton btnEncriptar;
  256.    private javax.swing.JTextField claveIntroducida;
  257.    private static javax.swing.JLabel clavedesencriptada;
  258.    private static javax.swing.JLabel contraseEncr;
  259.    private javax.swing.JTextField entradaClave;
  260.    private javax.swing.JTextField entradaHash;
  261.    private javax.swing.JLabel jLabel1;
  262.    private javax.swing.JLabel jLabel2;
  263.    private javax.swing.JLabel jLabel3;
  264.    private javax.swing.JLabel jLabel4;
  265.    private javax.swing.JLabel jLabel5;
  266.    private javax.swing.JLabel jLabel6;
  267.    private javax.swing.JMenu jMenu1;
  268.    private javax.swing.JMenu jMenu2;
  269.    private javax.swing.JMenuBar jMenuBar1;
  270.    private javax.swing.JMenuItem jMenuItem1;
  271.    private javax.swing.JPanel jPanel1;
  272.    private javax.swing.JPanel jPanel2;
  273.    private javax.swing.JTabbedPane jTabbedPane1;
  274.    private javax.swing.JTextField textoIntroducido;
  275.    // End of variables declaration
  276.  
  277. }
  278.  


Codigo de la clase encriptadora:


Código
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3.  
  4. /**
  5.  *
  6.  * @author debci
  7.  */
  8. public class encriptador {
  9. //instance
  10.    private String claveOriginal="";
  11.    private String textoOriginal="";
  12.    private String claveActual="";
  13.    private char[][] matriz;
  14.    //reader
  15.    //methods
  16.    public String decodificar(String texto) {
  17.        int repeticiones=0;
  18.        int jSize= claveActual.length();
  19.        while (repeticiones<jSize) {
  20.            boolean cambio=false;
  21.            int i=repeticiones;
  22.            claveActual="";
  23.            for (int j=0; j<jSize;j++)
  24.                claveActual=claveActual+matriz[0][j];
  25.            while ((!cambio)&&(i<jSize)) {
  26.                    if (claveOriginal.charAt(repeticiones)==claveActual.charAt(i)){
  27.                    modificarArreglo(i,repeticiones);
  28.                    cambio=!cambio;
  29.                }
  30.                else
  31.                    i++;
  32.            }
  33.            repeticiones++;
  34.    }
  35.    String output="";
  36.    for (int i=1; i<matriz.length;i++)
  37.        for (int j=0; j<matriz[0].length;j++)
  38.            output=output + matriz[i][j];
  39.    return output;
  40. }
  41.    public String codificar(String texto, String clave){
  42.        claveOriginal=clave;
  43.        textoOriginal=texto;
  44.        int jSize= claveOriginal.length();
  45.        int iSize= (textoOriginal.length()/jSize)+1;
  46.        if ((textoOriginal.length()%jSize)!=0)
  47.            iSize++;
  48.  
  49.        System.out.println("Filas :" + iSize + " Columnas: "+jSize);
  50.  
  51.        matriz= new char[iSize][jSize];
  52.        int charPos=0;
  53.        int keyPos=0;
  54.        for (int i=0; i<claveOriginal.length();i++) {
  55.            matriz[0][i]=claveOriginal.charAt(i);
  56.        }
  57.        for (int i=1; i<iSize;i++) {
  58.            for (int j=0;j<jSize;j++) {
  59.                try {
  60.                    matriz[i][j]=textoOriginal.charAt(charPos);
  61.                    charPos++;
  62.                }
  63.                catch (Exception e) {matriz[i][j]=' '; charPos++;}
  64.            }
  65.        }
  66.        /*debug
  67.         for (int i=0; i<iSize;i++) {
  68.             for (int j=0; j<jSize;j++) {
  69.                 System.out.print(matriz[i][j]);
  70.             }
  71.         }
  72.         */
  73.  
  74.        int repeticiones=0;
  75.        while (repeticiones<jSize) {
  76.            char[] filaCero= new char[jSize-repeticiones];
  77.            for (int j=repeticiones; j<jSize; j++) {
  78.                filaCero[j-repeticiones]=matriz[0][j];
  79.            }
  80.            int i=0;
  81.            boolean cambio=false;
  82.                    //tamaño de filaCero
  83.            while ((i<jSize-repeticiones)&&(!cambio)){
  84.                if (esElMenor(filaCero,filaCero[i])){
  85.                    modificarArreglo(i+repeticiones,repeticiones);
  86.                    cambio=!cambio;
  87.                }
  88.                i++;
  89.            }
  90.            repeticiones++;
  91.        }
  92.        /*debug
  93.         for (int i=0; i<iSize;i++) {
  94.             for (int j=0; j<jSize;j++) {
  95.                 System.out.print(matrizAuxiliar[i][j]);
  96.             }
  97.         }
  98.         */
  99.       String output="";
  100.        for (int j=0; j<jSize;j++) {
  101.            for (int i=0; i<iSize;i++) {
  102.                if (i==0) claveActual=claveActual + matriz[i][j];
  103.                output=output+matriz[i][j];
  104.            }
  105.        }
  106.        return output;
  107.        /*debug
  108.         for (int j=0; j<jSize;j++) {
  109.             for (int i=0; i<iSize;i++) {
  110.                 System.out.print(matrizFinal[j][i]);
  111.             }
  112.             System.out.println();
  113.         }
  114.         */
  115.  
  116.     }
  117.    public void modificarArreglo(int colActual, int rep) { //intercambia las columnas que pasan como param
  118.        /*for (int i=0; i<matriz.length;i++)
  119.             matrizAuxiliar[i][rep]=matriz[i][colActual];*/
  120.        char[]aux = new char[matriz.length];
  121.        for (int i=0; i<matriz.length;i++){
  122.            aux[i]=matriz[i][rep];
  123.            matriz[i][rep]=matriz[i][colActual];
  124.            matriz[i][colActual]=aux[i];
  125.        }
  126.    }
  127.  
  128.    public boolean esElMenor(char[] arreglo, char car) {
  129.        boolean menor=true; //asume que es el menor.
  130.        int i =0;
  131.        if (car==' ')
  132.            menor = false;
  133.        else {
  134.            while ((i<arreglo.length)&&(menor)) {
  135.                if ((arreglo[i]!=' ')&&(car!=' ')&&(arreglo[i]<car))
  136.                    menor=!menor;
  137.                else
  138.                    i++;
  139.                }
  140.            }
  141.        return menor;
  142.    }
Al introducir una clave de descifrado falla, la cosa es que la clase de encriptacion no trae un argumento concreto, es decir hay que itnroducir una palabra pero no la clave de cifrado, asi que ya me dirán que hago XDDDD

Saludos


Título: Re: 2o Problema, encriptador con GUI
Publicado por: sapito169 en 16 Noviembre 2009, 01:52 am
por que malogran su codigo poniento el gui de netebeans


Título: Re: 2o Problema, encriptador con GUI
Publicado por: Debci en 16 Noviembre 2009, 18:08 pm
por que malogran su codigo poniento el gui de netebeans
porque es mucho mas sencillo hacer la gui, sinceramente creo que hacerla por codigo es una perdida de tiempo(yo mismo empezé asi, y acabé artandome)
Alguien sabria ayudarme?

Saludos


Título: Re: 2o Problema, encriptador con GUI
Publicado por: Leyer en 16 Noviembre 2009, 23:24 pm
el error es muy facil la matriz no esta inicializada que quiero decir con esto que si no codificas. la matriz no se inicia. y el decodificador no puede iniciar. ademas tendrias que setear la clave otra cosas estas derrochando memoria creando 2 objetos del encriptor en lis listerner de los button solo usa uno :)

Saludos y suerte.


Título: Re: 2o Problema, encriptador con GUI
Publicado por: sapito169 en 17 Noviembre 2009, 19:51 pm
por que malogran su codigo poniento el gui de netebeans
porque es mucho mas sencillo hacer la gui, sinceramente creo que hacerla por codigo es una perdida de tiempo(yo mismo empezé asi, y acabé artandome)
Alguien sabria ayudarme?

Saludos

yo me referia que a nadie con dos dedos defrente le interesa un orrible codigo autogenerado que nisiquiera se puede editar con fasilidad y que no tiene nada que ver con ver en el problema


Título: Re: 2o Problema, encriptador con GUI
Publicado por: juancho77 en 18 Noviembre 2009, 06:09 am
me sumo a sapito, si quieres que alguien realmente lo vea y te ayude al menos sube el archivo del proyecto para que sea mas sencillo mirarlo. Ademas, si no funciona el descifrar no creo que el error este en la interfaz grafica. Seria una sana idea separar la capa de datos del programa de las otras interfaces.