package nu.fw.jeti.applet; import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; /* * @author Serge Rehem * Jeti, a Java Jabber client, Copyright (C) 2003 E.S. de Boer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * For questions, comments etc, * use the website at http://jeti.jabberstudio.org * or jabber/mail me at jeti@jabber.org * * Created on 14-nov-2003 */ /* * This code is based on AES Interop Between PHP and Java (Part 1) post at * http://propaso.com/blog/?cat=6 */ public class Crypto { public static String decrypt(String cryptedPasswd, String secretKey) { Cipher cipher; try { cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec("@4321avaJtluafeD".getBytes()); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] outText = cipher.doFinal(hexToBytes(cryptedPasswd)); return new String(outText).trim(); } catch (Exception e) { e.printStackTrace(); return null; } } private static byte[] hexToBytes(String str) { if (str == null) { return null; } else if (str.length() < 2) { return null; } else { int len = str.length() / 2; byte[] buffer = new byte[len]; for (int i = 0; i < len; i++) { buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16); } return buffer; } } }