Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Probably a simple question...

Status
Not open for further replies.

jisoo22

Programmer
Apr 30, 2001
277
US
Hello all,

I'm a beginner in the realm of Java and am currently learning it in school. I have a simple piece of software that is an example of simple word encryption (ie moving a letter from the right to the left aka pig-latin as well as replacing certain letters with others). I have two functions created, one for moving letters around the string that's entered and another that replaces certain letters with others. My problem is that I don't know how to run a string through BOTH functions. Conseqently you'd get "abcdefghigklmnopqrstuvwxyz" with the a moved to the end and vowels turned into consenants (as well as other letters changed). The code is posted below and the part I need help with is the function called "paranoidEncrypt". Can anyone give me an example of how I'm supposed to go about this?

Thanks,
Jisoo22


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 );

} //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 rightEncrypt( String s, int i ){

return swapPieces2(s, i);

}//rightEncrypt (Calls method to move letters from the right and place them on the left.)

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', '0' );
//System.out.println( &quot;step1:&quot; + result );
result = result.replace( 'r', 'a' );
//System.out.println( &quot;step2:&quot; + result );
result = result.replace( '0', 'r' );

result = result.replace( 'e', '1' );
result = result.replace( 's', 'e' );
result = result.replace( '1', 's' );

result = result.replace( 'i', '2' );
result = result.replace( 'n', 'i' );
result = result.replace( '2', 'n' );

result = result.replace( 'o', '3' );
result = result.replace( 't', 'o' );
result = result.replace( '3', 't' );

result = result.replace( 'u', '4' );
result = result.replace( 'g', 'u' );
result = result.replace( '4', 'g' );

return result;

}//replaceEncrypt (Replaces letters with other letters as designated.)

public String paranoidEncrypt( String s ){
String left_shift_1 = ??? //use leftEncrypt
String replaced = ??? //then pass results to replaceEncrypt
return replaced;

} //paranoidEncrypt


public String swapPieces( String s, int i ){

String left_piece = s.substring(0, i); //step 1
//System.out.println( &quot;left_piece=&quot; + left_piece );
String right_piece = s.substring(i); //step 2
//System.out.println( &quot;right_piece=&quot; + right_piece );
String reorder = right_piece.concat(left_piece); //step 3 (glue them together)
//System.out.println( &quot;glued back=&quot; + reorder );
return reorder; //step 4

} //swapPieces (Moves letters from the left to the right)

public String swapPieces2( String s, int i ){

String left_piece = s.substring(0, (s.length() - i)); //step 1
//System.out.println( &quot;left_piece=&quot; + left_piece );
String right_piece = s.substring((s.length() - i), s.length()); //step 2
//System.out.println( &quot;right_piece=&quot; + right_piece );
String reorder = right_piece + left_piece; //step 3 (glue them together)
//System.out.println( &quot;glued back=&quot; + reorder );
return reorder; //step 4

} //swapPieces2 (Moves letters from the right to the left)


//////////////// Begin Test Methods /////////////////////////

private void leftEncryptTest1( String clear ){
String encrypted = leftEncrypt( clear );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;leftEncrypt1: &quot; + encrypted );
System.out.println( ); //blank line
} //leftEncryptTest1


private void leftEncryptTest2( String clear ){
String encrypted2 = leftEncrypt( clear, 2 );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;leftEncrypt2: &quot; + encrypted2 );
System.out.println( );

String encrypted3 = leftEncrypt( clear, 3 );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;leftEncrypt3: &quot; + encrypted3 );
System.out.println( );
} //leftEncryptTest2

private void rightEncryptTest( String clear ){
String encrypted2 = rightEncrypt( clear, 2 );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;rightEncrypt2:&quot; + encrypted2 );
System.out.println( );

System.out.println( &quot;Clear: &quot; + clear );
String encrypted3 = rightEncrypt( clear, 3 );
System.out.println( &quot;rightEncrypt3:&quot; + encrypted3 );
System.out.println( );
} //rightEncryptTest

private void replaceEncryptTest( String clear ){
String encrypted = replaceEncrypt( clear );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;replEncrypt: &quot; + encrypted );
System.out.println( );
} //replaceEncryptTest

private void paranoidEncryptTest( String clear ) {
String encrypted = paranoidEncrypt( clear );
System.out.println( &quot;Clear: &quot; + clear );
System.out.println( &quot;paranEncrypt: &quot; + encrypted );
System.out.println( );
} //paranoidEncryptTest


} //StringEncrypt_1

 
If I undersatnd what you wish to do correctly :

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

This is because your methods return a String - so you can set a String to that value returned by your methods.


However, have you tried looking at the java.util.crypto package - which will encrypt passwords etc. for you, and is probably less easy to decrypt for someone who wanted to - compared to static encryption methods like replacing certain characters with other characters.

Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top