Created
March 31, 2011 14:33
-
-
Save rpkraemer/896449 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package criptografia; | |
| import criptografia.exception.EncodeException; | |
| /* | |
| * Criado em 30/03/2011 por Robson Kraemer | |
| */ | |
| public class Cifrador { | |
| private int key; | |
| public Cifrador(int key) { | |
| this.key = key; | |
| } | |
| public String encode(String msg) { | |
| StringBuilder response = new StringBuilder(); | |
| int ascii; | |
| for(char c : msg.toCharArray()) { | |
| ascii = (int) c; | |
| if(ascii > 99) | |
| response.append("_"); | |
| response.append(ascii); | |
| } | |
| try { | |
| response = deslocar(response, key); | |
| } catch (EncodeException e) { | |
| System.out.println(e.getMessage()); | |
| } | |
| return response.reverse().toString(); | |
| } | |
| public String decode(String msg) throws EncodeException { | |
| //Desfaz o processo de reversão da string | |
| StringBuilder reverseToNormal = new StringBuilder(msg).reverse(); | |
| //Desfaz o deslocamento | |
| try { | |
| reverseToNormal = deslocar(reverseToNormal, key * -1); | |
| } catch (EncodeException e) { | |
| System.out.println(e.getMessage()); | |
| } | |
| StringBuilder response = new StringBuilder(); | |
| String caractere = ""; | |
| int indice; | |
| for(int i = 0; i < reverseToNormal.length(); i+= indice) { | |
| if(reverseToNormal.charAt(i) == '_') { | |
| caractere = reverseToNormal.substring(i + 1, i + 4); | |
| indice = 4; | |
| } | |
| else { | |
| caractere = reverseToNormal.substring(i, i + 2); | |
| indice = 2; | |
| } | |
| try { | |
| char ch = (char) Integer.parseInt(caractere); | |
| response.append(ch); | |
| } catch (NumberFormatException e) { | |
| throw new EncodeException("Não foi possível realizar a descriptografia"); | |
| } | |
| } | |
| return response.toString(); | |
| } | |
| private StringBuilder deslocar(StringBuilder msg, int key) throws EncodeException { | |
| int ch; | |
| StringBuilder res = new StringBuilder(); | |
| for(char c : msg.toString().toCharArray()) { | |
| if(c == '_') { | |
| res.append('_'); | |
| continue; | |
| } | |
| ch = ((int) c + key); | |
| if(ch > 255) throw new EncodeException("Não foi possível realizar a criptografia"); | |
| res.append((char) ch); | |
| } | |
| return res; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package criptografia; | |
| import java.util.Scanner; | |
| /* | |
| * Criado em 30/03/2011 por Robson Kraemer | |
| */ | |
| public class Cifrar { | |
| /** | |
| * @param args | |
| */ | |
| public static void main(String[] args) { | |
| Scanner scan = new Scanner(System.in); | |
| System.out.print("Digite a frase -> "); | |
| String frase = scan.nextLine(); | |
| Cifrador c = new Cifrador(-12); | |
| frase = c.encode(frase); | |
| System.out.println(frase); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package criptografia; | |
| import java.util.Scanner; | |
| import criptografia.exception.EncodeException; | |
| /* | |
| * Criado em 30/03/2011 por Robson Kraemer | |
| */ | |
| public class Decifrar { | |
| /** | |
| * @param args | |
| */ | |
| public static void main(String[] args) { | |
| Scanner scan = new Scanner(System.in); | |
| System.out.print("Cole a frase cifrada -> "); | |
| String frase = scan.nextLine(); | |
| Cifrador c = new Cifrador(-12); | |
| try { | |
| frase = c.decode(frase); | |
| } catch (EncodeException e) { | |
| System.out.println(e.getMessage()); | |
| } | |
| System.out.println(frase); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment