source: branches/2.2/jabberit_messenger/java_source/src/nu/fw/jeti/applet/Crypto.java @ 3102

Revision 3102, 2.2 KB checked in by amuller, 14 years ago (diff)

Ticket #986 - Efetuado merge para o Branch 2.2( atualizacao do modulo)

  • Property svn:executable set to *
Line 
1package nu.fw.jeti.applet;
2
3import javax.crypto.Cipher;
4import javax.crypto.NoSuchPaddingException;
5import javax.crypto.spec.IvParameterSpec;
6import javax.crypto.spec.SecretKeySpec;
7
8/*
9 *      @author Serge Rehem
10 *      Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer 
11 *
12 *  This program is free software; you can redistribute it and/or modify
13 *  it under the terms of the GNU General Public License as published by
14 *  the Free Software Foundation; either version 2 of the License, or
15 *  (at your option) any later version.
16 *
17 *  This program is distributed in the hope that it will be useful,
18 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
19 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 *      GNU General Public License for more details.
21 *
22 *  You should have received a copy of the GNU General Public License
23 *  along with this program; if not, write to the Free Software
24 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25 *
26 *      For questions, comments etc,
27 *      use the website at http://jeti.jabberstudio.org
28 *  or jabber/mail me at jeti@jabber.org
29 *
30 *      Created on 14-nov-2003
31 */
32
33/*
34 *  This code is based on AES Interop Between PHP and Java (Part 1) post at 
35 *  http://propaso.com/blog/?cat=6                                           
36 */
37public class Crypto {
38
39        public static String decrypt(String cryptedPasswd, String secretKey) {
40                Cipher cipher;
41                try {
42                        cipher = Cipher.getInstance("AES/CBC/NoPadding");
43                        SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), "AES");
44
45                        IvParameterSpec ivSpec = new IvParameterSpec("@4321avaJtluafeD".getBytes());
46                       
47                        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
48                        byte[] outText = cipher.doFinal(hexToBytes(cryptedPasswd));
49                        return new String(outText).trim();
50                } catch (Exception e) {
51                        e.printStackTrace();
52                        return null;
53                }
54        }
55
56        private static byte[] hexToBytes(String str) {
57                if (str == null) {
58                        return null;
59                } else if (str.length() < 2) {
60                        return null;
61                } else {
62                        int len = str.length() / 2;
63                        byte[] buffer = new byte[len];
64                        for (int i = 0; i < len; i++) {
65                                buffer[i] = (byte) Integer.parseInt(str.substring(i * 2,
66                                                i * 2 + 2), 16);
67                        }
68                        return buffer;
69                }
70        }
71}
Note: See TracBrowser for help on using the repository browser.