Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- public class Decoder
- {
- public static String encrypt(String phrase, String key)
- {
- String result = "";
- for (int i = 0; i < phrase.length(); i++)
- {
- char let = encryptLetter(phrase.charAt(i), key);
- result += let;
- }
- return result;
- }
- public static String decrypt(String phrase, String key)
- {
- String result = "";
- for (int i = 0; i < phrase.length(); i++)
- {
- char ch = decryptLetter(phrase.charAt(i), key);
- result += ch;
- }
- return result;
- }
- public static char encryptLetter(char letter, String key)
- {
- char result = ' ';
- if (Character.isLetter(letter))
- {
- result = key.charAt(Character.toUpperCase(letter) - 'A');
- }
- return result;
- }
- public static char decryptLetter(char letter, String key)
- {
- int index = key.indexOf(letter);
- if (index != -1)
- {
- letter = (char) ('A' + index);
- }
- return letter;
- }
- public static void main(String[] args) throws IOException
- {
- String startSeq = "xv fpu vot ipk nk chpwindvoth tqeyditi";
- String plaintext = "";
- String ciphertext = "";
- String keySeq = "prjitgcoxbsynwdemhuvlzfqka";
- plaintext = decrypt(startSeq, keySeq);
- System.out.println("Decoded text is " + plaintext);
- ciphertext = encrypt(plaintext, keySeq);
- System.out.println("Encoded text is " + ciphertext);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment