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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A custom Cipher class

Status
Not open for further replies.

Knackers

Technical User
Apr 26, 2001
59
0
0
AU
I am new to Java so bare with me! I would like to write my own trivial Cipher class so that I can encrypt message going back and forth from a client / server program that I have written.

The cipher algorithm is super simple. All I want to do is encode/ decode a character on its ASCII value according to the following:

encode:
160 - character's ascii value = encoded number

decode:
160 - encoded number = character's ascii value

eg I wish to send X
160 - asciiToChar(X) = encoded number
160 - 88 = 72
so the number 72 is sent from my client to my server.

simmarly, decryption:

160 - 72 = 88
charToAscii(88) is X

The class will obviously have two methods in it encode() and decode(). What I want to do is pass a String to this class which is either encoded or decoded into another string. I am guessing that the string will have to be converted into an array of chars to do the flip on each element, but am not sure how to go about it...

Any help?




 
To get the string into a char array:
Code:
String s = "something";
char[] cArray = new char[s.length()];

for (int i=0; i<s.length(); i++)
{
   cArray[i] = s.charAt[i];
}

Going back is easier, because String has a constructor for it:

String news = new String( cArray );
 
Thanks idarke - that is exactly what I have done

<code>
//MyCipher.java

public class MyCipher{

public MyCipher(){ }

public String encrypt (String msg){
String toEncrypt = msg;
String encrMessage = &quot;&quot;;
char[] arrayOfChars = new char[toEncrypt.length()];

for ( int i = 0; i < toEncrypt.length(); ++i ) {
char c = toEncrypt.charAt( i );
int j = (int) c;
int k = 159 - j;

String aChar = new Character((char)k).toString();

//get the char value of the String
char d = aChar.charAt(0);
arrayOfChars= d;
}

//Build a message string from the encrypted characters
for (int i = 0; i < arrayOfChars.length; i++){
encrMessage = encrMessage + arrayOfChars;
}

return encrMessage;
}//end encryption method



public String decrypt (String encrMessage){
String toDecrypt = encrMessage;
String decryptMessage = &quot;&quot;;
char[] origChars = toDecrypt.toCharArray();

for ( int i = 0; i < toDecrypt.length(); ++i ) {
char c = toDecrypt.charAt(i);
int j = (int) c;
int k = 159 - j;

//convert the encoded integer value back to a String
String aChar = new Character((char)k).toString();

//get the char value of the String
char d = aChar.charAt(0);
origChars= d;
}

//Build a message string from the encrypted characters
for (int i = 0; i < origChars.length; i++){
decryptMessage = decryptMessage + origChars;
}

//System.out.println(&quot;The final encrypted message is: &quot; + encrMessage);
return decryptMessage;
}//end decryption method

}//end MyCipher class
</code>
 
You could also use:

chars[] chrs = new chars[theString.length];
theString.getChars(0, (theString.length-1), chrs, 0);

this will place each char of the string in the chrs array...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top