package com.vci.client.logon.base; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.IvParameterSpec; import sun.misc.BASE64Decoder; public class EncryptData { byte[] encryptKey; DESedeKeySpec spec; SecretKeyFactory keyFactory; SecretKey theKey; Cipher cipher; IvParameterSpec IvParameters; /** * * @param encKey 双方协定的共享密钥字符串,如:"www.liming.portal.com" */ public EncryptData(String encKey) { try { this.encryptKey = encKey.getBytes(); this.spec = new DESedeKeySpec(this.encryptKey); this.keyFactory = SecretKeyFactory.getInstance("DESede"); this.theKey = this.keyFactory.generateSecret(this.spec); this.cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding"); this.IvParameters = new IvParameterSpec(new byte[] { 12, 34, 56, 78, 90, 87, 65, 43 }); } catch (Exception e) { e.printStackTrace(); } } // /** * * @param sPwd,需解密的加密字符串,由UID和时间戳构成 * @return */ public String decrypt(String sPwd) { String decrypted_password = null; try { byte[] password = new BASE64Decoder().decodeBuffer(sPwd); this.cipher.init(2, this.theKey, this.IvParameters); byte[] decryptedPassword = password; byte[] decrypted_pwd = this.cipher.doFinal(decryptedPassword); decrypted_password = new String(decrypted_pwd); } catch (Exception e) { e.printStackTrace(); } return decrypted_password; } }