KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conf > Rot13PasswordEncoder


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.conf;
22
23 /**
24  * The ROT-13 password encoder passes the text of the database password
25  * through a simple Caesar cipher to obscure the password text. The ROT-13
26  * cipher only processes letters -- numbers and symbols are left untouched.
27  * ROT-13 is also a symmetrical cipher and therefore provides no real
28  * encryption since applying the cipher to the encrypted text produces the
29  * original source text. See the Wikipedia entry on
30  * <a HREF="http://en.wikipedia.org/wiki/Rot-13">ROT13</a>
31  * for more information on this topic.
32  *
33  * @since 3.0
34  * @author Michael Gentry
35  */

36 public class Rot13PasswordEncoder implements PasswordEncoding
37 {
38   /* (non-Javadoc)
39    * @see org.apache.cayenne.conf.PasswordEncoding#decodePassword(java.lang.String, java.lang.String)
40    */

41   public String JavaDoc decodePassword(String JavaDoc encodedPassword, String JavaDoc salt)
42   {
43     return rotate(encodedPassword);
44   }
45
46   /* (non-Javadoc)
47    * @see org.apache.cayenne.conf.PasswordEncoding#encodePassword(java.lang.String, java.lang.String)
48    */

49   public String JavaDoc encodePassword(String JavaDoc normalPassword, String JavaDoc salt)
50   {
51     return rotate(normalPassword);
52   }
53
54   /**
55    * Applies a ROT-13 Caesar cipher to the supplied value. Each letter in
56    * the supplied value is substituted with a new value rotated by 13 places
57    * in the alphabet. See <a HREF="http://en.wikipedia.org/wiki/ROT13">ROT13</a>
58    * for more information.
59    * <p>
60    * A Unix command to perform a ROT-13 cipher is:
61    * <pre>tr "[a-m][n-z][A-M][N-Z]" "[n-z][a-m][N-Z][A-M]"</pre>
62    *
63    * @param value The text to be rotated.
64    * @return The rotated text.
65    */

66   public String JavaDoc rotate(String JavaDoc value)
67   {
68     int length = value.length();
69     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
70
71     for (int i = 0; i < length; i++)
72     {
73       char c = value.charAt(i);
74
75       // If c is a letter, rotate it by 13. Numbers/symbols are untouched.
76
if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
77         c += 13; // The first half of the alphabet goes forward 13 letters
78
else if ((c >= 'n' && c <= 'z') || (c >= 'A' && c <= 'Z'))
79         c -= 13; // The last half of the alphabet goes backward 13 letters
80

81       result.append(c);
82     }
83
84     return result.toString();
85   }
86
87   /**
88    * Small test program to run text through the ROT-13 cipher. This program
89    * can also be run by hand to encode/decode values manually. The values
90    * passed on the command line are printed to standard out.
91    *
92    * @param args The array of text values (on the command-line) to be run
93    * through the ROT-13 cipher.
94    */

95   public static void main(String JavaDoc[] args)
96   {
97     Rot13PasswordEncoder encoder = new Rot13PasswordEncoder();
98     
99     for (int i = 0; i < args.length; i++)
100     {
101       String JavaDoc string = args[i];
102
103       System.out.println(encoder.rotate(string));
104     }
105   }
106 }
107
Popular Tags