KickJava   Java API By Example, From Geeks To Geeks.

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


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-47 password encoder passes the text of the database password
25  * through a simple Caesar cipher to obscure the password text. The ROT-47
26  * cipher is similar to the ROT-13 cipher, but processes numbers and symbols
27  * as well. See the Wikipedia entry on
28  * <a HREF="http://en.wikipedia.org/wiki/Rot-13">ROT13</a>
29  * for more information on this topic.
30  *
31  * @since 3.0
32  * @author Michael Gentry
33  */

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

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

47   public String JavaDoc encodePassword(String JavaDoc normalPassword, String JavaDoc salt)
48   {
49     return rotate(normalPassword);
50   }
51
52   /**
53    * Applies a ROT-47 Caesar cipher to the supplied value. Each letter in
54    * the supplied value is substituted with a new value rotated by 47 places.
55    * See <a HREF="http://en.wikipedia.org/wiki/ROT13">ROT13</a> for more
56    * information (there is a subsection for ROT-47).
57    * <p>
58    * A Unix command to perform a ROT-47 cipher is:
59    * <pre>tr '!-~' 'P-~!-O'</pre>
60    *
61    * @param value The text to be rotated.
62    * @return The rotated text.
63    */

64   public String JavaDoc rotate(String JavaDoc value)
65   {
66     int length = value.length();
67     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
68
69     for (int i = 0; i < length; i++)
70     {
71       char c = value.charAt(i);
72
73       // Process letters, numbers, and symbols -- ignore spaces.
74
if (c != ' ')
75       {
76         // Add 47 (it is ROT-47, after all).
77
c += 47;
78
79         // If character is now above printable range, make it printable.
80
// Range of printable characters is ! (33) to ~ (126). A value
81
// of 127 (just above ~) would therefore get rotated down to a
82
// 33 (the !). The value 94 comes from 127 - 33 = 94, which is
83
// therefore the value that needs to be subtracted from the
84
// non-printable character to put it into the correct printable
85
// range.
86
if (c > '~')
87           c -= 94;
88       }
89
90       result.append(c);
91     }
92
93     return result.toString();
94   }
95
96   /**
97    * Small test program to run text through the ROT-47 cipher. This program
98    * can also be run by hand to encode/decode values manually. The values
99    * passed on the command line are printed to standard out.
100    *
101    * @param args The array of text values (on the command-line) to be run
102    * through the ROT-47 cipher.
103    */

104   public static void main(String JavaDoc[] args)
105   {
106     Rot47PasswordEncoder encoder = new Rot47PasswordEncoder();
107     
108     for (int i = 0; i < args.length; i++)
109     {
110       String JavaDoc string = args[i];
111
112       System.out.println(encoder.rotate(string));
113     }
114   }
115 }
116
Popular Tags