Share Pastebin
Guest
Public paste!

Murrdawg

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