import sun.misc.base64decoder;import sun.misc.base64encoder;import javax.crypto.cipher;import javax.crypto.secretkey;import javax.crypto.secretkeyfactory;import javax.crypto.spec.deskeyspec;import java.io.ioexception;import java.security.securerandom;public class phpdesencrypt { string key; public phpdesencrypt() { } public phpdesencrypt(string key) { this.key = key; } public byte[] desencrypt(byte[] plaintext) throws exception { securerandom sr = new securerandom(); deskeyspec dks = new deskeyspec(key.getbytes()); secretkeyfactory keyfactory = secretkeyfactory.getinstance(des); secretkey key = keyfactory.generatesecret(dks); cipher cipher = cipher.getinstance(des); cipher.init(cipher.encrypt_mode, key, sr); byte data[] = plaintext; byte encrypteddata[] = cipher.dofinal(data); return encrypteddata; } public byte[] desdecrypt(byte[] encrypttext) throws exception { securerandom sr = new securerandom(); deskeyspec dks = new deskeyspec(key.getbytes()); secretkeyfactory keyfactory = secretkeyfactory.getinstance(des); secretkey key = keyfactory.generatesecret(dks); cipher cipher = cipher.getinstance(des); cipher.init(cipher.decrypt_mode, key, sr); byte encrypteddata[] = encrypttext; byte decrypteddata[] = cipher.dofinal(encrypteddata); return decrypteddata; } public string encrypt(string input) throws exception { return base64encode(desencrypt(input.getbytes())).replaceall(\\s*, ); } public string decrypt(string input) throws exception { byte[] result = base64decode(input); return new string(desdecrypt(result)); } public string base64encode(byte[] s) { if (s == null) return null; base64encoder b = new sun.misc.base64encoder(); return b.encode(s); } public byte[] base64decode(string s) throws ioexception { if (s == null) { return null; } base64decoder decoder = new base64decoder(); byte[] b = decoder.decodebuffer(s); return b; } public static void main(string[] args) { try { phpdesencrypt d = new phpdesencrypt(12345678); string p = d.encrypt(love); system.out.println(密文: + p); } catch (exception e) { e.printstacktrace(); } } public string getkey() { return key; } public void setkey(string key) { this.key = key; }}
回复内容: import sun.misc.base64decoder;import sun.misc.base64encoder;import javax.crypto.cipher;import javax.crypto.secretkey;import javax.crypto.secretkeyfactory;import javax.crypto.spec.deskeyspec;import java.io.ioexception;import java.security.securerandom;public class phpdesencrypt { string key; public phpdesencrypt() { } public phpdesencrypt(string key) { this.key = key; } public byte[] desencrypt(byte[] plaintext) throws exception { securerandom sr = new securerandom(); deskeyspec dks = new deskeyspec(key.getbytes()); secretkeyfactory keyfactory = secretkeyfactory.getinstance(des); secretkey key = keyfactory.generatesecret(dks); cipher cipher = cipher.getinstance(des); cipher.init(cipher.encrypt_mode, key, sr); byte data[] = plaintext; byte encrypteddata[] = cipher.dofinal(data); return encrypteddata; } public byte[] desdecrypt(byte[] encrypttext) throws exception { securerandom sr = new securerandom(); deskeyspec dks = new deskeyspec(key.getbytes()); secretkeyfactory keyfactory = secretkeyfactory.getinstance(des); secretkey key = keyfactory.generatesecret(dks); cipher cipher = cipher.getinstance(des); cipher.init(cipher.decrypt_mode, key, sr); byte encrypteddata[] = encrypttext; byte decrypteddata[] = cipher.dofinal(encrypteddata); return decrypteddata; } public string encrypt(string input) throws exception { return base64encode(desencrypt(input.getbytes())).replaceall(\\s*, ); } public string decrypt(string input) throws exception { byte[] result = base64decode(input); return new string(desdecrypt(result)); } public string base64encode(byte[] s) { if (s == null) return null; base64encoder b = new sun.misc.base64encoder(); return b.encode(s); } public byte[] base64decode(string s) throws ioexception { if (s == null) { return null; } base64decoder decoder = new base64decoder(); byte[] b = decoder.decodebuffer(s); return b; } public static void main(string[] args) { try { phpdesencrypt d = new phpdesencrypt(12345678); string p = d.encrypt(love); system.out.println(密文: + p); } catch (exception e) { e.printstacktrace(); } } public string getkey() { return key; } public void setkey(string key) { this.key = key; }}
des加密有好几种。。不知道你这种是哪个。。。这里有两种,你可以对照一下php des加密
这个……看你贴的代码是java吧!你要用php的des解密需要至少得需要知道加密key和对应的加密模式例如是cbc,cfb,ecb等!确认好之后,用楼上的给你的dome应该没问题的