Share Pastebin
Guest
Public paste!

Murrdawg

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