1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
//import java.string.*;
import java.lang.*;
import javax.swing.*;
class Cesar {
static public String chaine;
static public StringBuffer copieChaine;
static public int ecart = 1;
public Cesar(String chaine, int leDecal)
{
ecart = leDecal; //Pour le decalage du cryptage
copieChaine = new StringBuffer(chaine);
System.out.println ("phase de cryptage");
cryptage();
/*System.out.println ("phase de decryptage");
decryptage();*/
}//constructeur
public void cryptage(){
for(int i = 0; i < chaine.length(); i++){
char car = chaine.charAt(i);
if(car > (char)('z'-ecart))
copieChaine.replace(i, i+1, ecart == 1 ? "a" : ""
+(char)('a'+ecart-1));
else if(chaine.charAt(i) == ' ')
copieChaine.replace(i, i+1, "$");
else
copieChaine.replace(i, i+1, ""+(char)(car+ecart));
}
System.out.println("Chaine d'origine : " + chaine);
System.out.println("Chaine cryptee : " + copieChaine);
}//methode de cryptage
public void decryptage(){
for(int i = 0; i < copieChaine.length(); i++){
char car = copieChaine.charAt(i);
if(car < (char)('a'+ecart))
copieChaine.replace(i, i+1, ecart == 1 ? "z" : ""
+(char)('z'-ecart-1));
else if(copieChaine.charAt(i) == '$')
copieChaine.replace(i, i+1, " ");
else
copieChaine.replace(i, i+1, ""+(char)(car-ecart));
}
System.out.println("Chaine d'origine : " + chaine);
System.out.println("Chaine decryptee : " + copieChaine);
}//methode de decryptage
public static void main(String args[]) {
String chardecal;
int decal;
chaine = JOptionPane.showInputDialog(null, "Quel est le texte ?");
chardecal = JOptionPane.showInputDialog(null, "Quelle est le decalage ?");
decal = Integer.parseInt(chardecal);
Cesar UnCryptage = new Cesar(chaine,decal);
}
} |
Partager