Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Sakshi Lende
- //Cryptography
- //Spec: Shift alphabet forwards or backwards depending on inputted key
- public class Cryptography {
- public int key;
- public String ciphertext, plaintext = "";
- public char position;
- // public String message, encryptedMessage = "";
- public char ch;
- public Cryptography() {
- key = 0;
- plaintext = "";
- ciphertext = "";
- }
- public Cryptography(int k, String message) {
- key = k;
- plaintext = message;
- }
- public String getEncrypt() {
- if (key > 25) {
- return "Please enter a key between 1 and 25";
- } else {
- for (int i = 0; i < plaintext.length(); i++) {
- position = plaintext.charAt(i);
- if (position >= 'a' && position <= 'z') {
- position = (char) (position + key);
- if (position > 'z') {
- position = (char) (position - 'z' + 'a' - 1);
- }
- ciphertext += position;
- }
- else {
- ciphertext += position;
- }
- }
- return ciphertext;
- }
- }
- public String getDecrypt() {
- if (key > 25) {
- return "Please enter a key between 1 and 25";
- } else {
- for (int i = 0; i < plaintext.length(); i++) {
- position = plaintext.charAt(i);
- if (position >= 'a' && position <= 'z') {
- position = (char) (position - key);
- if (position > 'z') {
- position = (char) (position + 'z' - 'a' + 1);
- }
- ciphertext += position;
- }
- else {
- ciphertext += position;
- }
- }
- return ciphertext.substring(4);
- }
- }
- public String test()
- {
- for (int i = 0; i < plaintext.length(); ++i)
- {
- ch = plaintext.charAt(i);
- if (ch >= 'a' && ch <= 'z')
- {
- ch = (char) (ch + key);
- if (ch > 'z')
- {
- ch = (char) (ch - 'z' + 'a' - 1);
- }
- ciphertext += ch;
- }
- else if (ch >= 'A' && ch <= 'Z')
- {
- ch = (char) (ch + key);
- if (ch > 'Z')
- {
- ch = (char) (ch - 'Z' + 'A' - 1);
- }
- ciphertext += ch;
- }
- else
- {
- ciphertext += ch;
- }
- }
- return ciphertext;
- }
- public String pleaseWork()
- {
- for (int i = 0; i < plaintext.length(); i++)
- {
- ch = plaintext.charAt(i);
- if (ch >= 'a' && ch <= 'z')
- {
- ch = (char) (ch + key);
- if (ch > 'z')
- {
- ch = (char) (ch - 'z' + 'a' - 1);
- }
- ciphertext += ch;
- }
- else
- {
- ciphertext += ch;
- }
- }
- return ciphertext.substring(4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement