Java Encryption Program

Discussion in 'OT Technology' started by SiempreFresco, Dec 8, 2005.

  1. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
    Need some help writing a program that will take an input.txt and then encrypt that input.txt by taking whatever is inside of it ann remove any duplicate letters from that word. Then append to the text the remaining letters in the alphabet in reverse alphabetical order. ie. Unknown would become UNKOWZYXVTSRQPMLJIHGFEDCBA

    I need the program to be able to encrypt and decrypt. I have no idea how I would encrypt the file I know how to take the input.txt and an output.txt thats the easy part. Any help would be appreciated.
     
    Last edited: Dec 8, 2005
  2. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
  3. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,326
    Likes Received:
    566
    So you're building a simple substitution cipher with a keyword and reversed alphabet?

    An easy way would be to make an array c[26] and loop over it to check for duplicate letters.

    for the first letter in the keyword, check the first array element to see if it's duplicated. It won't be, so set it to the first letter ('u'). For subsequent letters, check only the filled elements, and so on. When you reach the end of the keyword, start over with the alphabet and do the same thing, until you drop the final letter into c[25].

    class Test
    {
    public static void main(String args[])
    {
    int j=0;
    String keyword = "unknown";
    String alphabet = "";
    char c[] = new char[26];
    boolean match;

    // i: keyword counter
    // j: array counter

    for(int i=0;i< keyword.length();i++)
    {
    match=false;
    for(int k=0;k<j;k++)
    {
    if(c[k] == keyword.charAt(i))
    {
    match=true;
    }
    }
    if(!match)
    {
    c[j++]=keyword.charAt(i);
    }
    }

    for(char a='z';a>='a';a--)
    {
    match=false;
    for(int k=0;k<j;k++)
    {
    if(c[k] == a)
    {
    match=true;
    }
    }
    if(!match)
    {
    c[j++]=a;
    }
    }

    for(int i=0;i<26;i++)
    {
    alphabet += c;
    }
    System.out.println('\n' + alphabet);
    }
    }
     
    Last edited: Dec 8, 2005
  4. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
    Figured it all out, your post helped me immensely. :bigthumb:
     
  5. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,326
    Likes Received:
    566
    Glad to help out.
     

Share This Page