I'm working on a simple encryption program (for school). It is not currently "homework" just FYI, I'm just practicing the examples in the book. It uses a Caesar's Cipher method (an alphabetical shift).

I have not tested this code very much. If something is wrong with it I probably don't know about it. The problem is when this programs runs, it doesn't print the entire message. It prints the first word and then nothing. I just got a hunch in that perhaps it doesn't handle whitespace correctly though...

T0he code:

import java.util.*;
import Utility.Utility; //Import my utility class 
public class Cipher {
    char forDecrypting[] = new char [26]; //Standard reference alphabet for performing operations on
    char forEncrypting[] = new char [26]; //reference alphabet for performing DEcryption
    char shift = 0;

    public Cipher ()
    {
        this(3); //Call argument constructor
        this.shift = 3;
    }

    public Cipher (int shift) {
        /*Initializes encoding alphabets using an argument specified shift*/
        int c = 0;
        for(int j = 0; j < 26; j++) {
            this.forDecrypting[c] = (char) ((int) 'A' + (j + shift) % 26); //Store the regular alphabet for decrypting
            this.forEncrypting[c] = (char) ((int) 'A' + (j - shift + 26 ) % 26);  //Store the shifted alphabet for encrypting
            c+=1;
        }
    }

    public char transform (boolean encrypting, char ch, int msgLen) { //Changes the char in to its encrypted equivalent
        int index = 0;
        if(encrypting) { //If we want to encrypt the message
            index = Utility.Clamp( ((int) ( ch - 'A')), 0, 25);
            System.out.print(" index = " + ((char )index) + "  " );
            return forEncrypting[index]; //Return the encrypted char
        } else {
            index = Utility.Clamp( ((int) ( ch - 'A')), 0, 25);
            System.out.print(" index = " + ((char )index) + "  " );
        return forDecrypting[index]; //Else return the decrypted char

        }
    }

    public String encode (String plaintext) {
        char [] message = plaintext.toUpperCase().toCharArray(); //Put the plaintext in to an array
        char [] cipher = new char [message.length];
        for(int p = 0; p <= message.length - 1; p++) { //Iterate through the message, replacing each letter with its corresponding encrypted char

            cipher[p] = transform(true, message[p], message.length - 1);
        }
        return new String(cipher);

    }


    public String decode (String ciphertext) {
        char [] cipher = ciphertext.toUpperCase().toCharArray(); //Put the plaintext in to an array
        char [] plaintext = new char [cipher.length];
        for(int p = 0; p <= cipher.length - 1; p++) { //Iterate through the message, replacing each letter with its corresponding encrypted char

            plaintext[p] = transform(false, cipher[p], cipher.length - 1);

        }

        return new String(plaintext);
    }

    public static void main (String[] args) {

        System.out.print("A = " +((int) 'A') + " Z = " + ((int)'Z') );
        Cipher myCipher = new Cipher();
        String ciphertext = new String(myCipher.encode(args[0]));
        System.out.println("   Cipher is:  " + ciphertext + "  Message is:  " + new String (myCipher.decode(ciphertext)) );


    }
    }

The Utility.Clamp function simply forces the value to a range. IE. If the range is from zero to 25, and we specify 100, we get back 25, or if it is -9 we get back zero. If the value is anywhere in the range then it just returns the input value.

Also these are my programs arguments:

"If you can read this then crap if not then good"

and sample output as of now:

"A = 65 Z = 90 index = index = index = index = Cipher is: FC Message is: IF "

It's obviously designed with the assumption that it will only encode/decode A-Z, so it's not surprising that it fails with a blank in the input.
Having said that...
args[0] is maybe just the first word of your input, the other words being in args[1] etc, depending on how you typed the command.

Oh, duh! I was not sure if the Eclipse IDE took its text arguments in quotes or not... so I just had it as plain text. It displays the entire message and cipher now. Only problem is there's an "A" between each word where a space would be... I think that is because of the way the decoder does its job. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.