Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. //Sakshi Lende
  2. //Cryptography
  3. //Spec: Shift alphabet forwards or backwards depending on inputted key
  4.  
  5. public class Cryptography {
  6.     public int key;
  7.     public String ciphertext, plaintext = "";
  8.     public char position;
  9.     // public String message, encryptedMessage = "";
  10.     public char ch;
  11.  
  12.     public Cryptography() {
  13.         key = 0;
  14.         plaintext = "";
  15.         ciphertext = "";
  16.     }
  17.  
  18.     public Cryptography(int k, String message) {
  19.         key = k;
  20.         plaintext = message;
  21.     }
  22.  
  23.     public String getEncrypt() {
  24.         if (key > 25) {
  25.             return "Please enter a key between 1 and 25";
  26.         } else {
  27.             for (int i = 0; i < plaintext.length(); i++) {
  28.                 position = plaintext.charAt(i);
  29.  
  30.                 if (position >= 'a' && position <= 'z') {
  31.                     position = (char) (position + key);
  32.  
  33.                     if (position > 'z') {
  34.                         position = (char) (position - 'z' + 'a' - 1);
  35.                     }
  36.                     ciphertext += position;
  37.                 }
  38.  
  39.                 else {
  40.                     ciphertext += position;
  41.                 }
  42.             }
  43.             return ciphertext;
  44.         }
  45.     }
  46.  
  47.     public String getDecrypt() {
  48.         if (key > 25) {
  49.             return "Please enter a key between 1 and 25";
  50.         } else {
  51.             for (int i = 0; i < plaintext.length(); i++) {
  52.                 position = plaintext.charAt(i);
  53.  
  54.                 if (position >= 'a' && position <= 'z') {
  55.                     position = (char) (position - key);
  56.  
  57.                     if (position > 'z') {
  58.                         position = (char) (position + 'z' - 'a' + 1);
  59.                     }
  60.                     ciphertext += position;
  61.                 }
  62.  
  63.                 else {
  64.                     ciphertext += position;
  65.                 }
  66.             }
  67.             return ciphertext.substring(4);
  68.  
  69.         }
  70.     }
  71.  
  72.     public String test()
  73.     {
  74.         for (int i = 0; i < plaintext.length(); ++i)
  75.         {
  76.             ch = plaintext.charAt(i);
  77.  
  78.             if (ch >= 'a' && ch <= 'z')
  79.             {
  80.                 ch = (char) (ch + key);
  81.  
  82.                 if (ch > 'z')
  83.                 {
  84.                     ch = (char) (ch - 'z' + 'a' - 1);
  85.                 }
  86.                 ciphertext += ch;
  87.             }
  88.  
  89.             else if (ch >= 'A' && ch <= 'Z')
  90.             {
  91.                 ch = (char) (ch + key);
  92.  
  93.                 if (ch > 'Z')
  94.                 {
  95.                     ch = (char) (ch - 'Z' + 'A' - 1);
  96.                 }
  97.                 ciphertext += ch;
  98.             }
  99.             else
  100.             {
  101.                 ciphertext += ch;
  102.             }
  103.         }
  104.         return ciphertext;
  105.     }
  106.  
  107.     public String pleaseWork()
  108.     {
  109.         for (int i = 0; i < plaintext.length(); i++)
  110.         {
  111.             ch = plaintext.charAt(i);
  112.  
  113.             if (ch >= 'a' && ch <= 'z')
  114.             {
  115.                 ch = (char) (ch + key);
  116.  
  117.                 if (ch > 'z')
  118.                 {
  119.                     ch = (char) (ch - 'z' + 'a' - 1);
  120.                 }
  121.  
  122.                 ciphertext += ch;
  123.             }
  124.             else
  125.             {
  126.                 ciphertext += ch;
  127.             }
  128.         }
  129.  
  130.         return ciphertext.substring(4);
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement