Thank you so much for your help. Here is my entire program. It consist of several different encryption methods, and one encrypt/decrypt function.
Enjoy
~Roy~
import java.lang.*;
class StringEncrypt_1 {
public static void main( String [] args ){
if( args.length != 1 ){
System.out.println( "usage: java StringEncrypt_1 secret-message" );
return;
}
String original = args[0]; //uses whatever you gave as the command-line arg
StringEncrypt_1 the_object = new StringEncrypt_1();
the_object.leftEncryptTest1( original );
the_object.leftEncryptTest2( original );
the_object.rightEncryptTest( original );
the_object.replaceEncryptTest( original );
the_object.paranoidEncryptTest( original );
the_object.isMarkerMessageTest( original );
the_object.railTest( original );
} //main
public String leftEncrypt( String s ){
return leftEncrypt( s, 1 ); //reduced body to 1 line!
} //leftEncrypt
public String leftEncrypt( String s, int i ){
return swapPieces( s, i);
} //leftEncrypt
public String swapPieces( String s, int i ){
String left_piece = s.substring(0,i); //step 1
//System.out.println( "left_piece=" + left_piece );
String right_piece = s.substring(i) ; //step 2
//System.out.println( "right_piece=" + right_piece );
String reorder = right_piece + left_piece ; //step 3 (glue them together)
//System.out.println( "glued back=" + reorder );
return reorder ; //step 4
} //swapPieces
public String rightEncrypt ( String s, int i ) {
return swapPieces(s, s.length()-i );
}
public String replaceEncrypt( String s ){
/* comment to describe how method works:
a <=> r
e <=> s
i <=> n
o <=> t
u <=> g
*/
String result = s; //just an alias
result = result.replace( 'a', 'R' );
//System.out.println( "step1:" + result );
result = result.replace( 'r', 'A' );
//System.out.println( "step2:" + result );
result = result.replace( 'e', 'S' );
result = result.replace( 's', 'E' );
result = result.replace( 'i', 'N' );
result = result.replace( 'n', 'I' );
result = result.replace( 'o', 'T' );
result = result.replace( 't', 'O' );
result = result.replace( 'u', 'G' );
result = result.replace( 'g', 'U' );
return result.toLowerCase();
}
public String paranoidEncrypt( String s ){
String left_shift_1 = leftEncrypt( s ); //use leftEncrypt,
String replaced = replaceEncrypt( left_shift_1 ); //then pass results to replaceEncrypt
return replaced;
} //paranoidEncrypt
public boolean isMarkerMessage( String s ){
boolean b1 = s.equals(leftEncrypt ( s) ) ;
return b1;
}
public String pack( String s ){
String packed = "";
final char SPACE = ' ';
for( int i = 0; i < s.length(); i++ )
if( s.charAt( i ) != SPACE ) packed = packed + s.charAt( i );
return packed;
}
public String railFenceEncode( String packed ){
String pack = pack(packed);
String s1 = "";
String s2 = "";
for( int j = 0; j < pack.length(); j++ ){
s1 = s1 + pack.charAt(j);
if(j < pack.length() - 1) {
j++;
s2 = s2 + pack.charAt(j);
}
}
String rail_fence_encoded = s1.concat(s2);
return rail_fence_encoded;
}
public String railFenceDecode( String rail_fence_encoded ){
String rail_fence_decoded = "";
String rail_fence_packed_encoded = railFenceEncode(rail_fence_encoded);
int length = (int)
Math.ceil((double) rail_fence_packed_encoded.length()/2);
String s1_encoded = rail_fence_packed_encoded.substring(0, length);
String s2_encoded = rail_fence_packed_encoded.substring(length);
for( int k = 0; k < s1_encoded.length(); k++ ){
rail_fence_decoded = rail_fence_decoded + s1_encoded.charAt(k);
if(k < s2_encoded.length() ) rail_fence_decoded = rail_fence_decoded + s2_encoded.charAt(k);
}
return rail_fence_decoded;
}
//////////////// Begin Test Methods /////////////////////////
private void leftEncryptTest1( String clear ){
String encrypted = leftEncrypt( clear );
System.out.println( "Clear: " + clear );
System.out.println( "leftEncrypt1: " + encrypted );
System.out.println( ); //blank line
} //leftEncryptTest1
private void leftEncryptTest2( String clear ){
String encrypted2 = leftEncrypt( clear, 2 );
System.out.println( "Clear: " + clear );
System.out.println( "leftEncrypt2: " + encrypted2 );
System.out.println( );
String encrypted3 = leftEncrypt( clear, 3 );
System.out.println( "Clear: " + clear );
System.out.println( "leftEncrypt3: " + encrypted3 );
System.out.println( );
} //leftEncryptTest2
private void rightEncryptTest( String clear ){
String encrypted2 = rightEncrypt( clear, 2 );
System.out.println( "Clear: " + clear );
System.out.println( "rightEncrypt2:" + encrypted2 );
System.out.println( );
System.out.println( "Clear: " + clear );
String encrypted3 = rightEncrypt( clear, 3 );
System.out.println( "rightEncrypt3:" + encrypted3 );
System.out.println( );
} //rightEncryptTest
private void replaceEncryptTest( String clear ){
String encrypted = replaceEncrypt( clear );
System.out.println( "Clear: " + clear );
System.out.println( "replEncrypt: " + encrypted );
System.out.println( );
} //replaceEncryptTest
private void paranoidEncryptTest( String clear ){
String encrypted = paranoidEncrypt( clear );
System.out.println( "Clear: " + clear );
System.out.println( "paranEncrypt: " + encrypted );
System.out.println( );
} //paranoidEncryptTest
private void isMarkerMessageTest( String clear ){
boolean check = isMarkerMessage( clear );
System.out.println( "Clear: " + clear );
System.out.println( "Marker check: " + check );
System.out.println( );
} //isMarkerMessageTest
private void railTest( String clear ){
String packed = pack( clear );
String rail_fence_encoded = railFenceEncode( clear );
String rail_fence_decoded = railFenceDecode( clear );
System.out.println( "rail fence encoding = " + rail_fence_encoded );
System.out.println( "rail fence decoding = " + rail_fence_decoded );
System.out.println( "clear =" + clear );
} //railTest
} //StringEncrypt_1