KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snaq > db > RotDecoder


1 /*
2     DBPool - JDBC Connection Pool Manager
3     Copyright (c) Giles Winstanley
4 */

5 package snaq.db;
6
7 /**
8  * Decodes passwords using the simple Rot13 algorithm.
9  * This algorithm is very insecure, but is included as an example.
10  * @author Giles Winstanley
11  */

12 public class RotDecoder implements PasswordDecoder
13 {
14     private static final int offset = 13;
15
16     public char[] decode(String JavaDoc encoded)
17     {
18         return rot(encoded);
19     }
20
21     private char[] rot(String JavaDoc encoded)
22     {
23         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(encoded);
24         for (int a = 0; a < sb.length(); a++)
25         {
26             char c = sb.charAt(a);
27             if (c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z')
28             {
29                 char base = Character.isUpperCase(c) ? 'A' : 'a';
30                 int i = c - base;
31                 c = (char)(base + (i + offset) % 26);
32                 sb.setCharAt(a, c);
33             }
34         }
35         char[] out = new char[sb.length()];
36         sb.getChars(0, out.length, out, 0);
37         return out;
38     }
39
40 /* public static void main(String[] args) throws Exception
41     {
42         RotDecoder x = new RotDecoder();
43         System.out.println(x.rot(args[0]));
44     }*/

45 }
Popular Tags