Share Pastebin
Guest
Public paste!

Murrdawg

By: a guest | Mar 21st, 2010 | Syntax: None | Size: 1.33 KB | Hits: 92 | Expires: Never
Copy text to clipboard
  1.  
  2.  
  3. public class Crypto extends ShiftedCipher
  4. {
  5.  
  6.        
  7.         public static void main(String[] args){
  8.                 ShiftedCipher window = new ShiftedCipher();
  9.  
  10.                 window.setTitle("Shifted Cipher");
  11.                 window.pack();
  12.                 window.setVisible(true);
  13.  
  14.                 String str = "Murray McClafferty";
  15.                 int key = 3;
  16.        
  17.                 String encrypted = encrypt(str, key);
  18.                 System.out.println(encrypted);
  19.        
  20.                 String decrypted = decrypt(encrypted, key);
  21.                 System.out.println(decrypted);
  22.         }
  23.  
  24.         public static String encrypt(String str, int key) {
  25.                 String encrypted = "";
  26.                 for(int i = 0; i < str.length(); i++) {
  27.                         int c = str.charAt(i);
  28.                         char chara=str.charAt(i);
  29.                         if (Character.isUpperCase(chara)){
  30.                                 c = c + (key % 26);
  31.                                 if (c > 'Z')
  32.                                 c = c - 26;
  33.                         } else if (Character.isLowerCase(chara)) {
  34.                                 c = c + (key % 26);
  35.                                 if (c > 'z')
  36.                                 c = c - 26;
  37.                         }
  38.                         encrypted += (char) c;
  39.                 }
  40.                 return encrypted;
  41.         }
  42.        
  43.         public static String decrypt(String str, int key)
  44.         {
  45.                 String decrypted = "";
  46.                 for(int i = 0; i < str.length(); i++) {
  47.                         int c = str.charAt(i);
  48.                         char chara=str.charAt(i);
  49.                         if (Character.isUpperCase(chara)) {
  50.                                 c = c - (key % 26);
  51.                                 if (c < 'A')
  52.                                 c = c + 26;
  53.                         } else if (Character.isLowerCase(chara)) {
  54.                                 c = c - (key % 26);
  55.                                 if (c < 'a')
  56.                                 c = c + 26;
  57.                         }
  58.                         decrypted += (char) c;
  59.                 }
  60.                 return decrypted;
  61.         }
  62.  
  63. }