/[aagtl_public1]/src/com/zoffcc/applications/aagtl/StringEnc.java
aagtl

Contents of /src/com/zoffcc/applications/aagtl/StringEnc.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Sun Aug 5 13:48:36 2012 UTC (11 years, 7 months ago) by zoffadmin
File size: 10256 byte(s)
initial import of aagtl source code
1 /**
2 * aagtl Advanced Geocaching Tool for Android
3 * loosely based on agtl by Daniel Fett <fett@danielfett.de>
4 * Copyright (C) 2010 - 2012 Zoff.cc <aagtl@work.zoff.cc>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
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
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 package com.zoffcc.applications.aagtl;
22
23 import java.io.IOException;
24 import java.io.UnsupportedEncodingException;
25 import java.security.InvalidAlgorithmParameterException;
26 import java.security.InvalidKeyException;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.spec.AlgorithmParameterSpec;
29 import java.security.spec.InvalidKeySpecException;
30 import java.security.spec.KeySpec;
31
32 import javax.crypto.BadPaddingException;
33 import javax.crypto.Cipher;
34 import javax.crypto.IllegalBlockSizeException;
35 import javax.crypto.NoSuchPaddingException;
36 import javax.crypto.SecretKey;
37 import javax.crypto.SecretKeyFactory;
38 import javax.crypto.spec.PBEKeySpec;
39 import javax.crypto.spec.PBEParameterSpec;
40
41 public class StringEnc
42 {
43
44
45 Cipher ecipher;
46 Cipher dcipher;
47
48
49 /**
50 * Constructor used to create this object. Responsible for setting
51 * and initializing this object's encrypter and decrypter Chipher instances
52 * given a Secret Key and algorithm.
53 *
54 * @param key
55 * Secret Key used to initialize both the encrypter and
56 * decrypter instances.
57 * @param algorithm
58 * Which algorithm to use for creating the encrypter and
59 * decrypter instances.
60 */
61 StringEnc(SecretKey key, String algorithm)
62 {
63 try
64 {
65 ecipher = Cipher.getInstance(algorithm);
66 dcipher = Cipher.getInstance(algorithm);
67 ecipher.init(Cipher.ENCRYPT_MODE, key);
68 dcipher.init(Cipher.DECRYPT_MODE, key);
69 }
70 catch (NoSuchPaddingException e)
71 {
72 System.out.println("EXCEPTION: NoSuchPaddingException");
73 }
74 catch (NoSuchAlgorithmException e)
75 {
76 System.out.println("EXCEPTION: NoSuchAlgorithmException");
77 }
78 catch (InvalidKeyException e)
79 {
80 System.out.println("EXCEPTION: InvalidKeyException");
81 }
82 }
83
84
85 /**
86 * Constructor used to create this object. Responsible for setting
87 * and initializing this object's encrypter and decrypter Chipher instances
88 * given a Pass Phrase and algorithm.
89 *
90 * @param passPhrase
91 * Pass Phrase used to initialize both the encrypter and
92 * decrypter instances.
93 */
94 StringEnc(String passPhrase)
95 {
96
97 // 8-bytes Salt
98 byte[] salt = {(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32, (byte) 0x56, (byte) 0x34,
99 (byte) 0xE3, (byte) 0x03};
100
101 // Iteration count
102 int iterationCount = 19;
103
104 try
105 {
106
107 KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
108 SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
109
110 ecipher = Cipher.getInstance(key.getAlgorithm());
111 dcipher = Cipher.getInstance(key.getAlgorithm());
112
113 // Prepare the parameters to the cipthers
114 AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
115
116 ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
117 dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
118
119 }
120 catch (InvalidAlgorithmParameterException e)
121 {
122 System.out.println("EXCEPTION: InvalidAlgorithmParameterException");
123 }
124 catch (InvalidKeySpecException e)
125 {
126 System.out.println("EXCEPTION: InvalidKeySpecException");
127 }
128 catch (NoSuchPaddingException e)
129 {
130 System.out.println("EXCEPTION: NoSuchPaddingException");
131 }
132 catch (NoSuchAlgorithmException e)
133 {
134 System.out.println("EXCEPTION: NoSuchAlgorithmException");
135 }
136 catch (InvalidKeyException e)
137 {
138 System.out.println("EXCEPTION: InvalidKeyException");
139 }
140 }
141
142
143 /**
144 * Takes a single String as an argument and returns an Encrypted version
145 * of that String.
146 *
147 * @param str
148 * String to be encrypted
149 * @return <code>String</code> Encrypted version of the provided String
150 */
151 public String encrypt(String str)
152 {
153 try
154 {
155 // Encode the string into bytes using utf-8
156 byte[] utf8 = str.getBytes("UTF8");
157
158 // Encrypt
159 byte[] enc = ecipher.doFinal(utf8);
160
161 // Encode bytes to base64 to get a string
162 return Base64.encodeBytes(enc);
163
164 }
165 catch (BadPaddingException e)
166 {
167 }
168 catch (IllegalBlockSizeException e)
169 {
170 }
171 catch (UnsupportedEncodingException e)
172 {
173 }
174 catch (IOException e)
175 {
176 }
177 return null;
178 }
179
180
181 /**
182 * Takes a encrypted String as an argument, decrypts and returns the
183 * decrypted String.
184 *
185 * @param str
186 * Encrypted String to be decrypted
187 * @return <code>String</code> Decrypted version of the provided String
188 */
189 public String decrypt(String str)
190 {
191
192 try
193 {
194
195 // Decode base64 to get bytes
196 byte[] dec = Base64.decode(str);
197
198 // Decrypt
199 byte[] utf8 = dcipher.doFinal(dec);
200
201 // Decode using utf-8
202 return new String(utf8, "UTF8");
203
204 }
205 catch (BadPaddingException e)
206 {
207 }
208 catch (IllegalBlockSizeException e)
209 {
210 }
211 catch (UnsupportedEncodingException e)
212 {
213 }
214 catch (IOException e)
215 {
216 }
217 return null;
218 }
219
220 //
221 // /**
222 // * The following method is used for testing the String Encrypter class.
223 // * This method is responsible for encrypting and decrypting a sample
224 // * String using several symmetric temporary Secret Keys.
225 // */
226 // public static void testUsingSecretKey()
227 // {
228 // try
229 // {
230 //
231 // System.out.println();
232 // System.out.println("+----------------------------------------+");
233 // System.out.println("| -- Test Using Secret Key Method -- |");
234 // System.out.println("+----------------------------------------+");
235 // System.out.println();
236 //
237 // String secretString = "Attack at dawn!";
238 //
239 // // Generate a temporary key for this example. In practice, you would
240 // // save this key somewhere. Keep in mind that you can also use a
241 // // Pass Phrase.
242 // SecretKey desKey = KeyGenerator.getInstance("DES").generateKey();
243 // SecretKey blowfishKey = KeyGenerator.getInstance("Blowfish").generateKey();
244 // SecretKey desedeKey = KeyGenerator.getInstance("DESede").generateKey();
245 //
246 // // Create encrypter/decrypter class
247 // aagtlStringEncrypter desEncrypter = new aagtlStringEncrypter(desKey, desKey.getAlgorithm());
248 // aagtlStringEncrypter blowfishEncrypter = new aagtlStringEncrypter(blowfishKey, blowfishKey
249 // .getAlgorithm());
250 // aagtlStringEncrypter desedeEncrypter = new aagtlStringEncrypter(desedeKey, desedeKey
251 // .getAlgorithm());
252 //
253 // // Encrypt the string
254 // String desEncrypted = desEncrypter.encrypt(secretString);
255 // String blowfishEncrypted = blowfishEncrypter.encrypt(secretString);
256 // String desedeEncrypted = desedeEncrypter.encrypt(secretString);
257 //
258 // // Decrypt the string
259 // String desDecrypted = desEncrypter.decrypt(desEncrypted);
260 // String blowfishDecrypted = blowfishEncrypter.decrypt(blowfishEncrypted);
261 // String desedeDecrypted = desedeEncrypter.decrypt(desedeEncrypted);
262 //
263 // // Print out values
264 // System.out.println(desKey.getAlgorithm() + " Encryption algorithm");
265 // System.out.println(" Original String : " + secretString);
266 // System.out.println(" Encrypted String : " + desEncrypted);
267 // System.out.println(" Decrypted String : " + desDecrypted);
268 // System.out.println();
269 //
270 // System.out.println(blowfishKey.getAlgorithm() + " Encryption algorithm");
271 // System.out.println(" Original String : " + secretString);
272 // System.out.println(" Encrypted String : " + blowfishEncrypted);
273 // System.out.println(" Decrypted String : " + blowfishDecrypted);
274 // System.out.println();
275 //
276 // System.out.println(desedeKey.getAlgorithm() + " Encryption algorithm");
277 // System.out.println(" Original String : " + secretString);
278 // System.out.println(" Encrypted String : " + desedeEncrypted);
279 // System.out.println(" Decrypted String : " + desedeDecrypted);
280 // System.out.println();
281 //
282 // }
283 // catch (NoSuchAlgorithmException e)
284 // {
285 // }
286 // }
287
288
289 // /**
290 // * The following method is used for testing the String Encrypter class.
291 // * This method is responsible for encrypting and decrypting a sample
292 // * String using using a Pass Phrase.
293 // */
294 // public static void testUsingPassPhrase()
295 // {
296 //
297 // System.out.println();
298 // System.out.println("+----------------------------------------+");
299 // System.out.println("| -- Test Using Pass Phrase Method -- |");
300 // System.out.println("+----------------------------------------+");
301 // System.out.println();
302 //
303 // String secretString = "Attack at dawn!";
304 // String passPhrase = "My Pass Phrase";
305 //
306 // // Create encrypter/decrypter class
307 // aagtlStringEncrypter desEncrypter = new aagtlStringEncrypter(passPhrase);
308 //
309 // // Encrypt the string
310 // String desEncrypted = desEncrypter.encrypt(secretString);
311 //
312 // // Decrypt the string
313 // String desDecrypted = desEncrypter.decrypt(desEncrypted);
314 //
315 // // Print out values
316 // System.out.println("PBEWithMD5AndDES Encryption algorithm");
317 // System.out.println(" Original String : " + secretString);
318 // System.out.println(" Encrypted String : " + desEncrypted);
319 // System.out.println(" Decrypted String : " + desDecrypted);
320 // System.out.println();
321 //
322 // }
323
324
325 // public static void main(String[] args)
326 // {
327 // testUsingSecretKey();
328 // testUsingPassPhrase();
329 // }
330
331
332 }

   
Visit the aagtl Website