Source code: gsoft/enc/EncryptionUtility.java
1 /*************************************************************************
2 Copyright (C) 2003 Steve Gee
3 stevesgee@cox.net
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *************************************************************************/
19
20 package gsoft.enc;
21
22 /** This class is to be used by CoreDataResources to store
23 * the algorythm for encryption and decryption of String variables.
24 * It is a quick hack, and needs a lot of work. */
25 public class EncryptionUtility
26 {
27
28 /** Encryption Algorythm used to encrypt and decrypt */
29 private EncryptionAlgorithm Algorithm = null;
30
31 /** Sets the EncryptionAlgorythm for the Utility */
32 public void setEncryptionAlgorithm(EncryptionAlgorithm alg){
33 Algorithm = alg;
34 }
35
36 /** Returns the EncryptionAlgorythm used by the Utility */
37 public EncryptionAlgorithm getEncryptionAlgorithm(){
38 if(Algorithm == null)
39 Algorithm = new DefaultEncryptionAlgorithm();
40 return Algorithm;
41 }
42
43 /** Will return the encrypted String clear
44 * using the currently set algorythm. */
45 public String decrypt(String cryptogram){
46 EncryptionAlgorithm e = this.getEncryptionAlgorithm();
47 int len = cryptogram.length();
48 int seed = 0;
49 char ret[] = new char[len];
50 for(int i = 0; i < len; i++){
51 seed = e.getSeed(cryptogram, i);
52 ret[i] = e.getDecryptedChar(cryptogram.charAt(i), seed);
53 }
54 return new String(ret);
55 }
56
57 /** Will encrypt and return the clear String
58 * Using the currently set algorythm. */
59 public String encrypt(String word){
60 EncryptionAlgorithm e = this.getEncryptionAlgorithm();
61 int len = word.length();
62 int seed = 0;
63 char ret[] = new char[len];
64 for(int i = 0; i < len; i++){
65 seed = e.getSeed(word, i);
66 ret[i] = e.getEncryptedChar(word.charAt(i), seed);
67 }
68 return new String(ret);
69 }
70
71 public static void main(String args[]){
72 EncryptionUtility eu = new EncryptionUtility();
73 String crypto = args[0];
74 System.out.println(crypto);
75 crypto = eu.encrypt(crypto);
76 System.out.println(crypto);
77 crypto = eu.decrypt(crypto);
78 System.out.println(crypto);
79 }
80
81
82 class DefaultEncryptionAlgorithm implements EncryptionAlgorithm
83 {
84 /** This algorithm adds 23 to the character, and then
85 * subtracts the amount of the seed which is the length of
86 * the string, unless the length of the string is 23, then
87 * it will subtract the length of the string + 3.
88 *
89 * If the result is less than 33, the result is subtracted
90 * from 126.
91 * If the result is greater than 126, the result less 126 is
92 * added to 33.
93 */
94
95 public char getEncryptedChar(char base, int seed){
96 int increment = (seed == 23)?seed + 3:seed;
97 int result = (base + 23) - increment;
98 if(result > 126){
99 result = (result - 126) + 33;
100 }
101 else if(result < 33){
102 result = 126 - (33 - result);
103 }
104 return (char)result;
105 }
106
107 public char getDecryptedChar(char crypt, int seed){
108 int increment = (seed == 23)?seed + 3:seed;
109 int result = (crypt + increment) - 23;
110 if(result > 126){
111 result = (result - 126) + 33;
112 }
113 else if(result < 33){
114 result = (result + 126) - 33;
115 }
116 return (char)result;
117 }
118
119 public int getSeed(String base, int current){
120 return base.length() + current;
121 }
122 }
123
124 }