Exemplo 1 INTERFACE public interface Criptografia extends java

Propaganda
Exemplo 1
INTERFACE
public interface Criptografia extends java.rmi.Remote {
public String criptografar (String a) throws java.rmi.RemoteException;
public String descriptografar (String a) throws java.rmi.RemoteException;
}
SERVIDOR
import java.rmi.Naming;
public class ServidorCriptografia {
public ServidorCriptografia() {
try {
Criptografia obj = new CriptografiaImpl();
Naming.rebind("//localhost/criptoService", obj);
}
catch (Exception e) {
System.out.println("Erro: " + e);
}
}
public static void main (String[] args) {
new ServidorCriptografia();
}
} // fim class ServidorCriptografia
IMPLEMENTAÇÃO DA INTERFACE
public class CriptografiaImpl extends java.rmi.server.UnicastRemoteObject implements Criptografia {
public CriptografiaImpl() throws java.rmi.RemoteException {
super();
}
public String criptografar (String a) throws java.rmi.RemoteException {
char charArray[];
char charAux=' ';
String output = "Texto: " + a;
int tam = a.length();
charArray = new char[tam];
output += "\n\nCriptografado: ";
//Obtem a cadeia de caracteres e a coloca em um vetor de char.
a.getChars(0, tam, charArray, 0);
for (int count = 0; count < charArray.length; count++)
{
if (charArray[count] == ' ')
charAux = ' ';
if (charArray[count] == 'A' ||
charArray[count]=='a')
charAux = 'f';
if (charArray[count] == 'B' ||
charArray[count]=='b')
charAux = 'j';
if (charArray[count] == 'C' ||
charArray[count]=='c')
charAux = 'h';
if (charArray[count] == 'D' ||
charArray[count]=='d')
charAux = 'r';
if (charArray[count] == 'E' ||
charArray[count]=='e')
charAux = 'p';
if (charArray[count] == 'F' ||
charArray[count]=='f')
charAux = 'l';
if (charArray[count] == 'G' ||
charArray[count]=='g')
charAux = 'k';
if (charArray[count] == 'H' ||
charArray[count]=='h')
charAux = 'z';
if (charArray[count] == 'I' ||
charArray[count]=='i')
charAux = 's';
if (charArray[count] == 'J' ||
charArray[count]=='j')
charAux = 'o';
if (charArray[count] == 'K' ||
charArray[count]=='k')
charAux = 'u';
if (charArray[count] == 'L' ||
charArray[count]=='l')
charAux = 'i';
if (charArray[count] == 'M' ||
charArray[count]=='m')
charAux = 'b';
if (charArray[count] == 'N' ||
charArray[count]=='n')
charAux = 'v';
if (charArray[count] == 'O' ||
charArray[count]=='o')
charAux = 'm';
if (charArray[count] == 'P' ||
charArray[count]=='p')
charAux = 'n';
if (charArray[count] == 'Q' ||
charArray[count]=='q')
charAux = 'c';
if (charArray[count] == 'R' ||
charArray[count]=='r')
charAux = 'x';
if (charArray[count] == 'S' ||
charArray[count]=='s')
charAux = 'a';
if (charArray[count] == 'T' ||
charArray[count]=='t')
charAux = 'd';
if (charArray[count] == 'U' ||
charArray[count]=='u')
charAux = 'g';
if (charArray[count] == 'V' ||
charArray[count]=='v')
charAux = 'q';
if (charArray[count] == 'W' ||
charArray[count]=='w')
charAux = 'y';
if (charArray[count] == 'X' ||
charArray[count]=='x')
charAux = 'e';
if (charArray[count] == 'Y' ||
charArray[count]=='y')
charAux = 'w';
if (charArray[count] == 'Z' ||
charArray[count]=='z')
charAux = 't';
charArray[count] = charAux;
output += charArray[count];
}
return output;
}
public String descriptografar (String a) throws java.rmi.RemoteException {
char charArray[];
char charAux = ' ';
String output = "Texto Criptografado: " + a;
int tam = a.length();
charArray = new char[tam];
output += "\n\nTexto Legivel ";
//Obtem a cadeia de caracteres e a coloca em um vetor de char.
a.getChars(0, tam, charArray, 0);
for(int count=0; count< charArray.length; count++)
{
if (charArray[count] == ' ')
charAux = ' ';
if (charArray[count] == 'A' ||
charArray[count]=='a')
charAux = 's';
if (charArray[count] == 'B' ||
charArray[count]=='b')
charAux = 'm';
if (charArray[count] == 'C' ||
charArray[count]=='c')
charAux = 'q';
if (charArray[count] == 'D' ||
charArray[count]=='d')
charAux = 't';
if (charArray[count] == 'E' ||
charArray[count]=='e')
charAux = 'x';
if (charArray[count] == 'F' ||
charArray[count]=='f')
charAux = 'a';
if (charArray[count] == 'G' ||
charArray[count]=='g')
charAux = 'u';
if (charArray[count] == 'H' ||
charArray[count]=='h')
charAux = 'c';
if (charArray[count] == 'I' ||
charArray[count]=='i')
charAux = 'l';
if (charArray[count] == 'J' ||
charArray[count]=='j')
charAux = 'b';
if (charArray[count] == 'K' ||
charArray[count]=='k')
charAux = 'g';
if (charArray[count] == 'L' ||
charArray[count]=='l')
charAux = 'f';
if (charArray[count] == 'M' ||
charArray[count]=='m')
charAux = 'o';
if (charArray[count] == 'N' ||
charArray[count]=='n')
charAux = 'p';
if (charArray[count] == 'O' ||
charArray[count]=='o')
charAux = 'j';
if (charArray[count] == 'P' ||
charArray[count]=='p')
charAux = 'e';
if (charArray[count] == 'Q' ||
charArray[count]=='q')
charAux = 'v';
if (charArray[count] == 'R' ||
charArray[count]=='r')
charAux = 'd';
if (charArray[count] == 'S' ||
charArray[count]=='s')
charAux = 'i';
if (charArray[count] == 'T' ||
charArray[count]=='t')
charAux = 'z';
if (charArray[count] == 'U' ||
charArray[count]=='u')
charAux = 'k';
if (charArray[count] == 'V' ||
charArray[count]=='v')
charAux = 'n';
if (charArray[count] == 'W' ||
charArray[count]=='w')
charAux = 'y';
if (charArray[count] == 'X' ||
charArray[count]=='x')
charAux = 'r';
if (charArray[count] == 'Y' ||
charArray[count]=='y')
charAux = 'w';
if (charArray[count] == 'Z' ||
charArray[count]=='z')
charAux = 'h';
charArray[count] = charAux;
output += charArray[count];
}
return output;
} // fim public String descriptografar
} // fim class CriptografiaImpl
CLIENTE
import javax.swing.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class ClienteCriptografia {
public static void main (String[] args) {
String A, resp = "";
// faz uma pergunta Sim = o ou Não = 1
int opcao = JOptionPane.showConfirmDialog(null,"Sim = Criptografia" + "\nNão = Descriptografia", "Escolha uma
opção", 0);
try {
Criptografia cripto = (Criptografia) Naming.lookup("//localhost/" + "criptoService");
if (opcao == 0) {
A = JOptionPane.showInputDialog("Entre com o TEXTO. Sem Acento");
resp = cripto.criptografar(A);
}
else {
A = JOptionPane.showInputDialog("Entre com o TEXTO. Sem Acento");
resp = cripto.descriptografar(A);
} // fim else
} // fim try
catch (MalformedURLException murle) {
System.out.println();
System.out.println("MalformedURLException");
System.out.println(murle);
}
catch (RemoteException re) {
System.out.println();
System.out.println("RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe) {
System.out.println();
System.out.println("NotBoundException");
System.out.println(nbe);
}
catch (java.lang.ArithmeticException ae) {
System.out.println();
System.out.println("java.lang.ArithmeticException");
System.out.println(ae);
}
catch (java.lang.StringIndexOutOfBoundsException str) {
System.out.println();
System.out.println("java.lang.StringIndexOutOfBoundsException");
System.out.println(str);
}
catch (java.lang.ArrayIndexOutOfBoundsException arr) {
System.out.println();
System.out.println("java.lang.ArrayIndexOutOfBoundsException");
System.out.println(arr);
}
JTextArea outputArea = new JTextArea();
outputArea.setText(resp);
JOptionPane.showMessageDialog(null, outputArea, "Texto Criptografado", JOptionPane.INFORMATION_MESSAGE);
} // fim static void main
} // fim class
Download