programma di crittografia

Stato
Discussione chiusa ad ulteriori risposte.

suns46

Utente Silver
11 Novembre 2008
14
4
0
61
ho un problema con il mio programma siccome dovrebbe criptare un intera frase ma cripta solo le lettere se qualcuno puo' aiutarmi a risolvere questo problema
grazie :)
metto un piu' tre a chi mi aiuta ecco il codice :

Codice:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Cesare extends JFrame implements ActionListener
{
	String chiaro ;
	String cifrato;
	private JPanel p1 = new JPanel();
	private JButton aggiungi = new JButton("cripta");
		 private JTextField tf1 = new JTextField();
         private JTextField tf2 = new JTextField();
	
	public Cesare()
	{
	     getContentPane().setLayout(new BorderLayout());
        setBounds(50,50,500,270);
	    p1.setLayout(new GridLayout(2,2,10,10));
        
	    setTitle("Criptografia");
        
       
	    
	    p1.add(tf1);
	    p1.add(tf2);
	    p1.add(aggiungi);
	    getContentPane().add(p1,"Center");
	     aggiungi.addActionListener(this);
	    
		 pack();
        show();
	}

	
	
		
		
		    
public void actionPerformed(ActionEvent event)
    {
        Object sorgente = event.getSource();
        if (sorgente == aggiungi)
        {
           String testo = tf1.getText();
            for(int i=0;i<testo.length();i++)
		    {
		      char chiaro1 = testo.charAt(i);
		      String chi = Character.toString (chiaro1);
		      if (chi.equals("a"))
		      {
		        String g = "d";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("b"))
		      {
		        String g = "e";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("c"))
		      {
		        String g = "f";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("d"))
		      {
		        String g = "g";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("e"))
		      {
		        String g = "h";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("f"))
		      {
		        String g = "i";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("g"))
		      {
		        String g = "l";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("h"))
		      {
		        String g = "m";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("i"))
		      {
		        String g = "n";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("l"))
		      {
		        String g = "o";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("m"))
		      {
		        String g = "p";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("n"))
		      {
		        String g = "q";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("o"))
		      {
		        String g = "r";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("p"))
		      {
		        String g = "s";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("q"))
		      {
		        String g = "t";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("r"))
		      {
		        String g = "u";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("s"))
		      {
		        String g = "v";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("t"))
		      {
		        String g = "z";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("u"))
		      {
		        String g = "a";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("v"))
		      {
		        String g = "b";
		        tf2.setText(""+g);
		      }
		      if (chi.equals("z"))
		      {
		        String g = "c";
		        tf2.setText(""+g);
		      }
		      else 
		      {
		      }
		      }
        }
    }
}
 
RE: programma di criptografia

Il problema non e' nel tuo programma, e' tra la tastiera e la sedia.
 
RE: programma di criptografia

beh te ogni volta azzeri la stringa di output "g".

tf2.setText(tf1.getText() + g);

dovresti fare una cosa del tipo ammesso che sia consentito, dovrei rispolverare un po' il Java.
In ogni caso ti consiglio di rivedere tutto questo codice ed inventarti qualcosa di più originale.

E' un piacere rivedere meh. ed il suo odio per il Java ahahah, è un piacere risentirci.
 
RE: programma di criptografia

1)dato che si chiama java si mette nell'apposita sezione
2)si dice "crittografia" e non "criptografia"
3)quoto meh
4)precisamente luxx,se fa tf2.setText(""+g); è ovvio che nella textfield ti mette uno solo carattere xD
5)tieni un codice leggermente più funzionale e veloce....
Codice:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Cesare extends JFrame implements ActionListener{
    String chiaro ;
    String cifrato;
    private JPanel p1 = new JPanel();
    private JButton aggiungi = new JButton("cripta");
    private JTextField tf1 = new JTextField();
    private JTextField tf2 = new JTextField();
    private String array_1[]={" ","a","b","c","d","e","f","g","h","i","l","m","n","o","p","q","r","s","t","u","v","z"};
    private String array_2[]={" ","d","e","f","g","h","i","l","m","n","o","p","q","r","s","t","u","v","z","a","b","c"};
    
    public Cesare(){
         getContentPane().setLayout(new BorderLayout());
        setBounds(50,50,500,270);
        p1.setLayout(new GridLayout(2,2,10,10));
        
        setTitle("Crittografia");
       
        p1.add(tf1);
        p1.add(tf2);
        p1.add(aggiungi);
        getContentPane().add(p1,"Center");
        aggiungi.addActionListener(this);
        
        pack();
        show();
    }
            
public void actionPerformed(ActionEvent event){
        Object sorgente = event.getSource();
        if (sorgente == aggiungi){
           String testo = tf1.getText();
            for(int i=0;i<testo.length();i++){
                for(int j=0;j<=array_1.length;j++){
                    if(array_1[j].equals(""+testo.charAt(i))){
                        tf2.setText(tf2.getText()+array_2[j]);
                        break;
                    }
                }
            }
            tf2.setText(tf2.getText().toUpperCase());
        }
    }
}
l'ho fatto al volo...dovrebbe funzionare...
6)non puoi mettere tu +3,al massimo +1 puoi mettere xD

edit: ora che ci penso,meh se ti sta sul pazzo java mettiti nel tuo angolino e non scassare...per quanto il tuo post sia veritiero (e l'ho detto) o nella tua risposta c'è qualcosa di vagamente inerente alla discussione o che possa aiutare o non postare xkè di cazzate ne abbiamo già troppe e le tue non le vogliamo!
 
poi se ti puo essere utile x la crittografia io ho anche una src.. :blush:.

Codice:
import java.io.*;

public class CaesarCipher {

	public static void main(String[] args) {
		if (args.length < 3) {
			System.out.println("Wrong number of arguments!!");
			System.exit(1);
		}

		File srcFile = new File(args[0]);
		if (!srcFile.exists()) {
			System.out.println("file " + args[0] + " does not exists!!");
			System.exit(2);
		}
		File dstFile = new File(args[1]);

		try {
			if (srcFile.getCanonicalFile().equals(dstFile.getCanonicalFile())) {
				System.out.print(args[0] + " and " + args[1]
				+ " refer to the same file!!");
				System.exit(3);
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		byte key = 0;
		try {
			key = Byte.parseByte(args[2]);
		} catch (NumberFormatException nfe) {
			System.out.println(args[2] + " is not a valid key!!");
			System.exit(4);
		}
		BufferedInputStream src = null;
		BufferedOutputStream dst = null;
		try {
			src = new BufferedInputStream(new FileInputStream(srcFile), 1024);
			dst = new BufferedOutputStream(new FileOutputStream(dstFile), 1024);
		} catch (FileNotFoundException fnfe) {
			System.out.println("error processing files!!\n" + fnfe);
			System.exit(5);
		}
		int v;
		try {
			try {
				while ((v = src.read()) != -1)
					dst.write((byte) v + key);

			} finally {
				src.close();
				dst.close();
			}
		} catch (IOException e) {
			System.out.println("I/O error while processing...");
			e.printStackTrace();
			System.exit(6);
		}

	}
}
 
[ot]
R4z0r_Cr4sH ha detto:
edit: ora che ci penso,meh se ti sta sul pazzo java mettiti nel tuo angolino e non scassare...per quanto il tuo post sia veritiero (e l'ho detto) o nella tua risposta c'è qualcosa di vagamente inerente alla discussione o che possa aiutare o non postare xkè di cazzate ne abbiamo già troppe e le tue non le vogliamo!

Non e' di certo per il java, ma per come aveva fatto la "funzione" per crittare.

Riguardo alle troppe cazzate solo le mie non volete? Che razzisti :(
[/ot]
 
RE: programma di criptografia

Malex ha detto:
*coof coff* ed allora perché nn hai coretto? XD

già xD ma andavo leggermente di fretta xD e poi voi moderatori a che servite?:p

LyB:usa il tag code invece di quote xD :)
[ot]finchè è offtopic ci puo anche stare ma in altri topic oltre alle cazzate ci vorrei qualcosa di utile e inerente altrimenti non scrivere proprio...
questo discorso vale per tutti indistintamente,questa volta ho beccato te,se becco qualcun'altro gli dirò la stessissima cosa...
fossi razzista ti bannerei a vista ogni volte...come accade con altri xD[/ot]
 
Stato
Discussione chiusa ad ulteriori risposte.