Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Encrypt
- {
- //instance field
- String keyword; //"elvispresley"
- String alphabet;
- String plaintext; //"all your base are belong to us"
- int encrypted[];
- int length;
- public char ch;
- //constructor
- public Encrypt(String k, String p)
- { //assigning the Strings
- keyword = "elvispresley";
- putKeyword();
- String alphabet = "e l v i s p r y a b c d f g h j k m n o p q r t u w x z";
- buildAlphabet();
- plaintext = "all your base are belong to us";
- encrypted = new int[plaintext.length()];
- encrypt();
- }
- public void putKeyword()
- {
- StringBuffer alpha = new StringBuffer();
- alpha.append(keyword.charAt(0));
- for(int i = 1; i < keyword.length(); i++ )
- {
- boolean found = false;
- for(int j = 0; j < alpha.length(); j++)
- {
- if(keyword.charAt(j) == keyword.charAt(i))
- {
- found = true;
- }
- }
- if(!found)
- {
- alpha.append(keyword.charAt(i));
- }
- }
- alpha.toString();
- alphabet = alpha.toString();
- }
- public void buildAlphabet()
- {
- StringBuffer alpha = new StringBuffer();
- int len = alpha.length();
- for(char ch = 'a'; ch <= 'z'; ch++)
- {
- boolean found = false;
- for(int j = 0; j < len; j++)
- {
- if(alpha.charAt(j) == ch)
- {
- found = true;
- }
- else
- {
- alpha.append(keyword.charAt(ch));
- }
- alphabet = alpha.toString();
- }
- }
- }
- public void encrypt()
- {
- for(int i = 0; i < plaintext.length(); i++)
- {
- encrypted[i] = getIndex(plaintext.charAt(i));
- }
- }
- public int getIndex(char ch)
- {
- int o = 0;
- for(int i = 0; i < alphabet.length(); i++)
- {
- if(ch == alphabet.charAt(i))
- o = i;
- }
- return o;
- }
- public String getAlphabet()
- {
- return alphabet;
- }
- public int[] getEncrypted()
- {
- return encrypted;
- }
- public void printEncrypted()
- {
- final StringBuffer buf = new StringBuffer();
- buf.append("Array");
- for (int i = 0; i < length; i++)
- {
- if (i != 0) buf.append(", ");
- buf.append(encrypted[i]);
- }
- }
- }
Add Comment
Please, Sign In to add comment