|
/**
* Autore :Alenadri Enrico
* Classe : III G
* Data : 28/01/2009
* Nome File:Crytti.java
*/
import javax.swing.JOptionPane;
public class Crytti {
/**
* @param args the command line arguments
*/
public static void main (String[] args )
{
boolean ripDom = true,
ripLet=true;
char letteraM,
letteraK;
int lunFrase=0,
numeroM=0,
numeroK=0,
i,
k,
lunChiave=0,
conKey=1;
frase="",
mex="",
key="";
char[] testo = new char[200];
char[][] tabCri = {
{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'},
{'b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a'},
{'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b'},
{'d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c'},
{'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d'},
{'f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e'},
{'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f'},
{'h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g'},
{'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h'},
{'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i'},
{'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j'},
{'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k'},
{'m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l'},
{'n','o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m'},
{'o','p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n'},
{'p','q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o'},
{'q','r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'},
{'r','s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q'},
{'s','t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r'},
{'t','u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s'},
{'u','v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t'},
{'v','w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u'},
{'w','x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v'},
{'x','y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w'},
{'y','z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x'},
{'z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y'},
};
do{
try{
domanda = JOptionPane. showInputDialog("Cosa Vuoi fare? 'crittare o decrittare' ");
domanda = domanda.toLowerCase();
if(domanda.equals("crittare"))ripDom=false;
if(domanda.equals("decrittare"))ripDom=false;
{
ripDom=true;
}
}while(ripDom==true);
if(domanda.equals("crittare"))
{
do{
ripLet=false;
try{
frase = JOptionPane. showInputDialog("Iinserisci la frase da Crittare: ");
frase = frase.toLowerCase();
lunFrase = frase.length();
{
ripLet=true;
}
}while(ripLet==true);
do{
ripDom=false;
try{
key = JOptionPane. showInputDialog("Inserisci la chiave di Crittaggio: ");
key = key.toLowerCase();
lunChiave = key.length();
if(key.equals(""))ripDom=true;
{
ripDom=true;
}
}while(ripDom==true);
for(i=1;i<=lunFrase;i++)
{
letteraM = frase.charAt(i-1);
if(letteraM == ' ')
testo[i]=' ';
else
{
letteraK = key.charAt(conKey-1);
numeroM = letteraM-97;
numeroK = letteraK-97;
testo[i] = tabCri[numeroM][numeroK];
if(conKey==lunChiave)conKey=0;
conKey++;
}
}
for(i=1;i<=lunFrase;i++)
{
if(testo[i]==' ')
mex=mex+" ";
else
{
mex+=testo[i];
}
}
System. out. println("Messaggio Crittato:\n"+mex );
JOptionPane. showMessageDialog(null, "Testo crittato : \n"+mex + "\n\nChiave di Decrittaggio :\n"+key );
}
else
{
do{
ripLet=false;
try{
mex = JOptionPane. showInputDialog("Inserisci la stringa da decrittare:");
mex = mex.toLowerCase();
lunFrase = mex.length();
{
ripLet=true;
}
}while(ripLet==true);
do{
ripDom=false;
try{
key = JOptionPane. showInputDialog("Inserisci la chiave di Crittaggio: ");
key = key.toLowerCase();
lunChiave = key.length();
if(key.equals(""))ripDom=true;
{
ripDom=true;
}
}while(ripDom==true);
for(k=1;k<=lunFrase;k++)
{
letteraM = mex.charAt(k-1);
if(letteraM == ' ')
testo[k] = ' ';
else
{
letteraK = key.charAt(conKey-1);
numeroK = letteraK-97;
for(i=0;i<=25;i++)
{
if(tabCri[numeroK][i]==letteraM)
{
numeroM = i+1;
i=26;
}
}
testo[k]= (char) (numeroM+96);
if(conKey==lunChiave)conKey=0;
conKey++;
}
}
for(i=1;i<=lunFrase;i++)
{
if(testo[i]==' ')
frase+=" ";
else
{
frase+=testo[i];
}
}
JOptionPane. showMessageDialog(null, "Ecco il testo in chiaro : \n"+frase );
}
}
}
|
|