Thursday, May 14, 2009

Java - Rot94 - similar to rot13 - encode text





public class NinetyFiveEncode
{
public NinetyFiveEncode()
{
String testStr = "The rain in Spain falls mainly on the plain. Call me at (555)112-xxxx. {:-) Bye.";
String encoded = NinetyFiveEncode.rot94(testStr);
System.out.println( encoded );
String decoded = NinetyFiveEncode.rot94( encoded );
System.out.println( decoded );
}

/*
* Rot94 by Alan Lupsha (c)2009
*
* Takes a string of characters, and for every character
* between ASCII value 33 and 126 (! to ~), it adds 94 to
* the character value (wraps around if the result character
* is larger than 126)
*
* Any non-printable characters and the space character
* (i.e. any char smaller than 33 and larger than 126) gets
* copied over.
*
* Sample run:
*
* String testStr = "The rain in Spain falls mainly on the plain. Call me at (850)879-xxxx. {:-) Bye.";
* String encoded = NinetyFiveEncode.rot94(testStr);
* System.out.println( encoded );
* String decoded = NinetyFiveEncode.rot94( encoded );
* System.out.println( decoded );
*
* %96 C2:? :? $A2:? 72==D >2:?=J @? E96 A=2:?] r2== >6 2E Wgd_Xgfh\IIII] Li\X qJ6]
* The rain in Spain falls mainly on the plain. Call me at (850)879-xxxx. {:-) Bye.
*/
public static String rot94(String plainText)
{
if (plainText == null) return "";

// encode plainText
StringBuffer encodedMessage = new StringBuffer("");
int abyte;
for (int i = 0; i < abyte =" plainText.charAt(i);">= 33) && (abyte <= 126 ))
abyte = (abyte - '!' + 47) % 94 + '!';

encodedMessage.append( (char)abyte );
}
return encodedMessage.toString();
}


public static void main( String[] args )
{
NinetyFiveEncode ninetyFiveEncode = new NinetyFiveEncode();
}
}


No comments:

Post a Comment