Results 1 to 2 of 2
  1. #1
    jgonzalez14 is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default cipher.. Won't work

    I am trying to figure out why this cipher i have will not work. can anyone help?
    Java Code:
    import javax.swing.*;
    public class Animal
    {
        private String inputString;
        private int intOffset;
        
        public Animal()
        {
            inputString = null;
            intOffset = 0;
            
        }
        public static void main(String [] args)
        {
            Animal c2 = new Animal();
            int choose = JOptionPane.showConfirmDialog(null,"If you want to Encrypet Text Click (Yes) ,or Decrypt Click (No) or Cancel to exit :");
            
            if (choose == JOptionPane.YES_OPTION)
            {
                String plainText = c2.encrypt();
                JOptionPane.showMessageDialog(null,plainText);
            }
            else if(choose == JOptionPane.NO_OPTION)
            
            {
                String cipherText = c2.decrypt();
                JOptionPane.showMessageDialog(null,cipherText);
            }
            else
                System.exit(0);
        }
        
        public String encrypt()
        {
            inputString = ((String)JOptionPane.showInputDialog("Enter PlainText") ).toLowerCase().trim();
            intOffset = Integer.parseInt(JOptionPane.showInputDialog("Enter Key") );
            String letters = "abcdefghijklmnopqrstuvwxyz";
            StringBuffer sb = new StringBuffer();
            
            int holder = inputString.length();
            for(int i = 0; i < holder; i++)
                {
                    String tmp = ""+inputString.charAt(i);
                    int offset = letters.indexOf(tmp);
                    offset += intOffset;
                    if( offset > 25 ){
                        int newOffset = 0;
                        newOffset = offset % 26;
                        sb.append( letters.charAt(newOffset) );
                    }
                    else
                        {
                            sb.append( letters.charAt(offset) );
                        }
                        //
                }//forloop
                return sb.toString();
        }
        
        public String decrypt()
        {
            inputString = ((String)JOptionPane.showInputDialog("Enter CipherText") ).toLowerCase().trim();
            intOffset = Integer.parseInt(JOptionPane.showInputDialog("Enter Key") );
            String letters = "abcdefghijklmnopqrstuvwxyz";
            StringBuffer sb = new StringBuffer();
            
            int holder = inputString.length();
            for(int i = 0; i < holder; i++)
                {
                    String tmp = ""+inputString.charAt(i);
                    int offset = letters.indexOf(tmp);
                    offset -= intOffset;
                    
    offset %= 26;
    sb.append( letters.charAt(offset) );
                }
            return sb.toString();
        }
    }

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation This is not encipherment.

    Study
    Java Code:
    import javax.crypto.Cipher;
    and see if you can grasp any of it.

    ( no intent to be hard on you, it's just that this is what came to mind when I put your code in the editor to look at it... )
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Synchronization Doesn't seem to work
    By sherinpearl in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 06:30 PM
  2. Will this applet ever work?
    By willemjav in forum Java Applets
    Replies: 4
    Last Post: 04-20-2008, 05:40 PM
  3. Pass by ref. A work around?
    By diRisig in forum New To Java
    Replies: 0
    Last Post: 02-05-2008, 07:25 PM
  4. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 02:58 AM
  5. Work On Lucene
    By peiceonly in forum Lucene
    Replies: 1
    Last Post: 08-07-2007, 05:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •