KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > util > DesEncrypter


1 /*
2  * $$Id: DesEncrypter.java,v 1.3 2005/06/07 12:32:27 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on 23.07.2004
27  *
28  */

29 package org.jresearch.gossip.util;
30
31 import java.io.IOException JavaDoc;
32 import java.io.UnsupportedEncodingException JavaDoc;
33
34 import sun.misc.BASE64Decoder;
35 import sun.misc.BASE64Encoder;
36 import Acme.Crypto.DesCipher;
37
38 /**
39  * @author dbelov
40  *
41  */

42 public class DesEncrypter {
43
44     private DesCipher cipher;
45
46     private final static String JavaDoc LENGTH_SUFF = "@";
47
48     private final static String JavaDoc ENCODING = "UTF8";
49
50     /**
51      * @param passPhrase
52      */

53     public DesEncrypter(String JavaDoc passPhrase) {
54         cipher = new DesCipher(passPhrase.getBytes());
55     }
56
57     /**
58      * @param str
59      * @return
60      * @throws UnsupportedEncodingException
61      * @throws UnsupportedEncodingException
62      */

63     public String JavaDoc encrypt(String JavaDoc str) throws UnsupportedEncodingException JavaDoc {
64         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(str);
65         sb.append(LENGTH_SUFF);
66         str = sb.toString();
67         int padlength = padlength(str);
68         byte[] inputBytes = new byte[padlength], // The input text bytes.
69
cryptBytes = new byte[padlength]; // The encrypted bytes.
70
System.arraycopy(str.getBytes(), 0, inputBytes, 0,
71                 str.getBytes().length);
72         for (int i = 0; i < padlength; i += 8) {
73             cipher.encrypt(inputBytes, i, cryptBytes, i);
74         }
75         return new BASE64Encoder().encode(cryptBytes);
76     }
77
78     /**
79      * @param str
80      * @return
81      * @throws IOException
82      */

83     public String JavaDoc decrypt(String JavaDoc str) throws IOException JavaDoc {
84         byte[] inputBytes = new BASE64Decoder().decodeBuffer(str);// The input
85
// text bytes.
86
byte[] clearBytes = new byte[inputBytes.length]; // The decrypted
87
// bytes.
88
for (int i = 0; i < inputBytes.length; i += 8) {
89             cipher.decrypt(inputBytes, i, clearBytes, i);
90         }
91         String JavaDoc result = new String JavaDoc(clearBytes, ENCODING);
92
93         return result.substring(0, result.lastIndexOf(LENGTH_SUFF));
94     }
95
96     // To use block ciphers like DES, you need input that is in 8-byte blocks.
97
// If your input byte length isn't divisible by 8, we will need to pad it.
98

99     private static int padlength(String JavaDoc s) throws UnsupportedEncodingException JavaDoc {
100         int len = s.getBytes(ENCODING).length;
101         int mod = len % 8;
102         return len + ((mod == 0) ? 0 : 8 - mod);
103     }
104 }
Popular Tags