clanecollege

Decoder Solution

May 16th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. import java.io.IOException;
  2.  
  3. public class Decoder
  4. {
  5.     public static String encrypt(String phrase, String key)
  6.     {
  7.         String result = "";
  8.         for (int i = 0; i < phrase.length(); i++)
  9.         {
  10.             char let = encryptLetter(phrase.charAt(i), key);
  11.             result += let;
  12.         }
  13.         return result;
  14.  
  15.     }
  16.  
  17.     public static String decrypt(String phrase, String key)
  18.     {
  19.         String result = "";
  20.         for (int i = 0; i < phrase.length(); i++)
  21.         {
  22.             char ch = decryptLetter(phrase.charAt(i), key);
  23.             result += ch;
  24.         }
  25.         return result;
  26.  
  27.     }
  28.  
  29.     public static char encryptLetter(char letter, String key)
  30.     {
  31.         char result = ' ';
  32.         if (Character.isLetter(letter))
  33.         {
  34.             result = key.charAt(Character.toUpperCase(letter) - 'A');
  35.         }
  36.         return result;
  37.     }
  38.  
  39.     public static char decryptLetter(char letter, String key)
  40.     {
  41.         int index = key.indexOf(letter);
  42.         if (index != -1)
  43.         {
  44.             letter = (char) ('A' + index);
  45.         }
  46.         return letter;
  47.     }
  48.  
  49.     public static void main(String[] args) throws IOException
  50.     {
  51.         String startSeq = "xv fpu vot ipk nk chpwindvoth tqeyditi";
  52.         String plaintext = "";
  53.         String ciphertext = "";
  54.         String keySeq = "prjitgcoxbsynwdemhuvlzfqka";
  55.         plaintext = decrypt(startSeq, keySeq);
  56.         System.out.println("Decoded text is " + plaintext);
  57.         ciphertext = encrypt(plaintext, keySeq);
  58.         System.out.println("Encoded text is " + ciphertext);
  59.  
  60.     }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment