下面是java的代码片段:
public static string des_key=36位的key;
执行方法:
desutil.decrypt(rs.getstring(i+1),global.des_key );
类:
public class desutil { private final static string des = des; private final static string padding=des/ecb/pkcs5padding; private final static log log= logfactory.getlog(desutil.class); public static byte[] encrypt(byte[] src, byte[] key)throws exception { securerandom sr = new securerandom(); deskeyspec dks = new deskeyspec(key); secretkeyfactory keyfactory = secretkeyfactory.getinstance(des); secretkey securekey = keyfactory.generatesecret(dks); cipher cipher = cipher.getinstance(padding); cipher.init(cipher.encrypt_mode, securekey, sr); return cipher.dofinal(src); } public static byte[] decrypt(byte[] src, byte[] key)throws exception { securerandom sr = new securerandom(); deskeyspec dks = new deskeyspec(key); secretkeyfactory keyfactory = secretkeyfactory.getinstance(des); secretkey securekey = keyfactory.generatesecret(dks); cipher cipher = cipher.getinstance(padding); cipher.init(cipher.decrypt_mode, securekey, sr); return cipher.dofinal(src); } public final static string decrypt(string data,string key){ try { return new string(decrypt(base64.decode(data.getbytes()), key.getbytes())); }catch(exception e) { log.error(exception -->desutil); e.printstacktrace(); } return null; } public final static string decrypt(string data,string key, string charset){ try { return new string(decrypt(base64.decode(data.getbytes()), key.getbytes()), charset); }catch(exception e) { log.error(exception -->desutil ); e.printstacktrace(); } return null; } public final static string encrypt(string code,string key){ try { return base64.encodebytes(encrypt(code.getbytes(utf-8), key.getbytes())); }catch(exception e) { log.error(exception -->desutil); e.printstacktrace(); } return null; }/* public static void main(string[] args) { /** * convert byte[] string * * @param src * @return */ public static string bytesto16hexstring(byte[] src) { stringbuilder stringbuilder = new stringbuilder(); if (src == null || src.length
回复内容: 和java端进行对接的,我用php解密,key是36字节的,所以没有头绪了。我想着是不是有把这36字节转化成16或者32字节的通用方法?先谢谢大家了。
下面是java的代码片段:
public static string des_key=36位的key;
执行方法:
desutil.decrypt(rs.getstring(i+1),global.des_key );
类:
public class desutil { private final static string des = des; private final static string padding=des/ecb/pkcs5padding; private final static log log= logfactory.getlog(desutil.class); public static byte[] encrypt(byte[] src, byte[] key)throws exception { securerandom sr = new securerandom(); deskeyspec dks = new deskeyspec(key); secretkeyfactory keyfactory = secretkeyfactory.getinstance(des); secretkey securekey = keyfactory.generatesecret(dks); cipher cipher = cipher.getinstance(padding); cipher.init(cipher.encrypt_mode, securekey, sr); return cipher.dofinal(src); } public static byte[] decrypt(byte[] src, byte[] key)throws exception { securerandom sr = new securerandom(); deskeyspec dks = new deskeyspec(key); secretkeyfactory keyfactory = secretkeyfactory.getinstance(des); secretkey securekey = keyfactory.generatesecret(dks); cipher cipher = cipher.getinstance(padding); cipher.init(cipher.decrypt_mode, securekey, sr); return cipher.dofinal(src); } public final static string decrypt(string data,string key){ try { return new string(decrypt(base64.decode(data.getbytes()), key.getbytes())); }catch(exception e) { log.error(exception -->desutil); e.printstacktrace(); } return null; } public final static string decrypt(string data,string key, string charset){ try { return new string(decrypt(base64.decode(data.getbytes()), key.getbytes()), charset); }catch(exception e) { log.error(exception -->desutil ); e.printstacktrace(); } return null; } public final static string encrypt(string code,string key){ try { return base64.encodebytes(encrypt(code.getbytes(utf-8), key.getbytes())); }catch(exception e) { log.error(exception -->desutil); e.printstacktrace(); } return null; }/* public static void main(string[] args) { /** * convert byte[] string * * @param src * @return */ public static string bytesto16hexstring(byte[] src) { stringbuilder stringbuilder = new stringbuilder(); if (src == null || src.length
用冗余字符补全到48字节。
