Download code

Jump to: navigation, search

Back to Rot13_(Java)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

Rot13.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Rot13_(Java)?oldid=19204
14 */
15 
16 package org.literateprograms.cyphers;
17 
18 import java.io.BufferedReader;
19 import java.io.InputStreamReader;
20 import java.io.IOException;
21 
22 public class Rot13 {
23 
24    /**
25     * Encode plain text using the Rot13 algorithm.
26     * @param plainText the plain text message.
27     * @returns the plain text message encoded using the Rot13 algorithm.
28     */
29    public static String encode(String plainText) {
30      // deal with the case that method is called with null argument
31      String encodedMessage = "";
32      if (plainText == null) return plainText;
33      for (int i = 0; i < plainText.length(); i++) {
34        char c = plainText.charAt(i);
35        if      (c >= 'a' && c <= 'm') c += 13;
36        else if (c >= 'n' && c <= 'z') c -= 13;
37        else if (c >= 'A' && c <= 'M') c += 13;
38        else if (c >= 'N' && c <= 'Z') c -= 13;
39 
40        encodedMessage += c;
41      }
42      return encodedMessage;
43    }
44 
45    public static void main(String [] args)
46    throws IOException {
47 
48       String input = ""; // data to be encoded
49 
50       if (args.length <= 3) {
51         int argNumber = 0;
52 	while (argNumber < args.length) {
53 	  if ("-e".equals(args[argNumber])) {
54 	    argNumber ++;
55 	    input = args[argNumber];
56 	    argNumber ++;
57 	  }
58 	  else if ("-h".equals(args[argNumber])) {
59 	     System.err.println(usage());
60 	     System.exit(0);
61 	  }
62 	  else {
63 	     System.err.println("Error: unexpected argument " + args[argNumber]);
64 	     System.err.println(usage());
65 	     System.exit(1);
66 	  }
67 	}
68       }
69       else {
70         System.err.println("Error: Too many arguments.\n" + usage());
71         System.exit(1);
72       }
73 
74       if (input.length() == 0) {
75         BufferedReader in = new BufferedReader(
76 	   new InputStreamReader(System.in));
77 	String s;
78 	while ((s = in.readLine()) != null) {
79 	  input += s + "\n";
80 	}
81       }
82 
83       System.out.println(encode(input));
84     
85       System.exit(0);
86 
87    }
88 
89    private static String usage() {
90      return "Usage: java -cp cyphers.jar uk.literateprograms.cyphers.Rot13 [-h] [-e \"text to be encoded\"] [< plaintext] [> cyphertext]\n" +
91      "where arguments are:\n" +
92      "-e encode text on the command line\n" +
93      "-h print this message";
94    } 
95 }


hijacker
hijacker
hijacker
hijacker

plaintext.txt

 1 Now is the Winter of our Discontent,
 2 Made glorious Summer by this Son of Yorke:
 3 And all the clouds that lowr'd vpon our house
 4 In the deepe bosome of the Ocean buried.
 5 Now are our browes bound with Victorious Wreathes,
 6 Our bruised armes hung vp for Monuments;
 7 Our sterne Alarums chang'd to merry Meetings;
 8 Our dreadfull Marches, to delightfull Measures.
 9 Grim-visag'd Warre, hath smooth'd his wrinkled Front:
10 And now, in stead of mounting Barbed Steeds,
11 To fright the Soules of fearfull Aduersaries,
12 He capers nimbly in a Ladies Chamber,
13 To the lasciuious pleasing of a Lute.


hijacker
hijacker
hijacker
hijacker

Rot13Test.java

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Rot13_(Java)?oldid=19204
14 */
15 
16 package org.literateprograms.cyphers;
17 
18 import junit.framework.TestCase;
19 
20 public class Rot13Test extends TestCase {
21 
22     private String plainText;
23     private String cypherText;
24 	
25     public void setUp() {
26         plainText = "Now is the Winter of our Discontent,\n" +
27 	            "Made glorious Summer by this Son of Yorke:\n" +
28 	            "And all the clouds that lowr'd vpon our house\n" +
29 	            "In the deepe bosome of the Ocean buried.\n" +
30 	            "Now are our browes bound with Victorious Wreathes,\n" +
31 	            "Our bruised armes hung vp for Monuments;\n" +
32 	            "Our sterne Alarums chang'd to merry Meetings;\n" +
33 	            "Our dreadfull Marches, to delightfull Measures.\n" +
34 	            "Grim-visag'd Warre, hath smooth'd his wrinkled Front:\n" +
35 	            "And now, in stead of mounting Barbed Steeds,\n" +
36 	            "To fright the Soules of fearfull Aduersaries,\n" +
37 	            "He capers nimbly in a Ladies Chamber,\n" +
38 	            "To the lasciuious pleasing of a Lute.";
39 	cypherText = "Abj vf gur Jvagre bs bhe Qvfpbagrag,\n" + 
40 	             "Znqr tybevbhf Fhzzre ol guvf Fba bs Lbexr:\n" + 
41 	             "Naq nyy gur pybhqf gung ybje'q icba bhe ubhfr\n" + 
42 	             "Va gur qrrcr obfbzr bs gur Bprna ohevrq.\n" + 
43 	             "Abj ner bhe oebjrf obhaq jvgu Ivpgbevbhf Jerngurf,\n" + 
44 	             "Bhe oehvfrq nezrf uhat ic sbe Zbahzragf;\n" + 
45 	             "Bhe fgrear Nynehzf punat'q gb zreel Zrrgvatf;\n" + 
46 	             "Bhe qernqshyy Znepurf, gb qryvtugshyy Zrnfherf.\n" + 
47 	             "Tevz-ivfnt'q Jneer, ungu fzbbgu'q uvf jevaxyrq Sebag:\n" + 
48 	             "Naq abj, va fgrnq bs zbhagvat Oneorq Fgrrqf,\n" + 
49 	             "Gb sevtug gur Fbhyrf bs srneshyy Nqhrefnevrf,\n" + 
50 	             "Ur pncref avzoyl va n Ynqvrf Punzore,\n" + 
51 	             "Gb gur ynfpvhvbhf cyrnfvat bs n Yhgr.";
52     }
53 
54     public void testEncode() {
55       assertEquals(cypherText, Rot13.encode(plainText));
56     }
57 
58     public void testDecode() {
59         assertEquals(plainText, Rot13.encode(cypherText));
60     }
61 
62 }


hijacker
hijacker
hijacker
hijacker

build.xml

1