Php3des encryption problem compatible with Java

Source: Internet
Author: User
Tags decrypt
3des

Key provided by the other party
56781234d56f012bcd5e701a3cdd6709

Need to translate the same encryption results into 16 binary with PHP

That is, if there are 8 bits such as 00000000
Converted to 16 after encryption is 16 bits

They provide a software I encrypt 12345678
Convert 12345678 with Bin2Hex to 16 binary string result to 3132333435363738
And then encrypt it to get the result of b2a85cf088d9ff03



Now use PHP to implement the same encryption results as it! I tried a lot of times to correspond to, ask the big God paste Code
Java encryption Code is attached:
Package Com.huateng.crypto.des;import Com.huateng.crypto.cryptoexception;import Java.io.printstream;import Java.security.invalidalgorithmparameterexception;import Java.security.invalidkeyexception;import Java.security.nosuchalgorithmexception;import Java.security.spec.invalidkeyspecexception;import Java.util.Arrays ; Import Javax.crypto.badpaddingexception;import Javax.crypto.cipher;import javax.crypto.IllegalBlockSizeException ; import Javax.crypto.nosuchpaddingexception;import Javax.crypto.secretkey;import javax.crypto.SecretKeyFactory; Import Javax.crypto.spec.desedekeyspec;import Javax.crypto.spec.ivparameterspec;public class TripleDesTool{public    Static byte[] Decrypt (byte[] input, byte[] key) throws Cryptoexception {byte[] decrypteddata;      try {secretkeyfactory keyfactory = null;      try {keyfactory = secretkeyfactory.getinstance ("Desede");      } catch (NoSuchAlgorithmException ex) {throw new Cryptoexception (Ex.getmessage (), ex); } SecretKey secretkey = null;      try {secretkey = Keyfactory.generatesecret (new Desedekeyspec (key));      } catch (Invalidkeyspecexception ex) {throw new Cryptoexception (Ex.getmessage (), ex);      } Cipher Cipher = null;      try {cipher = cipher.getinstance ("desede/cbc/nopadding");      } catch (Nosuchpaddingexception ex) {throw new Cryptoexception (Ex.getmessage (), ex);      } catch (NoSuchAlgorithmException ex) {throw new Cryptoexception (Ex.getmessage (), ex);      } Ivparameterspec IV = new IVPARAMETERSPEC (new byte[] {0, 0, 0, 0, 0, 0, 0, 0});      try {cipher.init (2, Secretkey, iv);      } catch (Invalidalgorithmparameterexception ex) {throw new Cryptoexception (Ex.getmessage (), ex);      try {decrypteddata = cipher.dofinal (input);      } catch (Badpaddingexception ex) {throw new Cryptoexception (Ex.getmessage (), ex); } catch (Illegalblocksizeexception ex) {throw new CRyptoexception (Ex.getmessage (), ex);      } catch (IllegalStateException ex) {throw new Cryptoexception (Ex.getmessage (), ex);    }} catch (InvalidKeyException ex) {throw new Cryptoexception (Ex.getmessage (), ex);  } return decrypteddata; } public static byte[] Encrypt (byte[] input, byte[] key) throws cryptoexception {int len = (Input.length/8 + (i Nput.length% 8 = = 0?    0:1)) * 8;    byte[] Plaindata = new Byte[len];    byte[] EncryptedData = new Byte[8];    Arrays.fill (Plaindata, 32);    System.arraycopy (input, 0, plaindata, 0, input.length);      try {desedekeyspec dks = null;      DKs = new Desedekeyspec (key);      Secretkeyfactory keyfactory = null;      try {keyfactory = secretkeyfactory.getinstance ("Desede");      } catch (NoSuchAlgorithmException ex) {throw new Cryptoexception (Ex.getmessage (), ex);      } Secretkey secretkey = null;      try {secretkey = Keyfactory.generatesecret (DKS); } catch(Invalidkeyspecexception ex)      {throw new Cryptoexception (Ex.getmessage (), ex);      } Cipher Cipher = null;      try {cipher = cipher.getinstance ("desede/cbc/nopadding");      } catch (Nosuchpaddingexception ex) {throw new Cryptoexception (Ex.getmessage (), ex);      } catch (NoSuchAlgorithmException ex) {throw new Cryptoexception (Ex.getmessage (), ex);      } Ivparameterspec IV = new IVPARAMETERSPEC (new byte[] {0, 0, 0, 0, 0, 0, 0, 0});      try {cipher.init (1, Secretkey, iv);      } catch (Invalidalgorithmparameterexception ex) {throw new Cryptoexception (Ex.getmessage (), ex);      } try {EncryptedData = cipher.dofinal (Plaindata);      } catch (Badpaddingexception ex) {throw new Cryptoexception (Ex.getmessage (), ex);      } catch (Illegalblocksizeexception ex) {throw new Cryptoexception (Ex.getmessage (), ex); } catch (IllegalStateException ex) {throw new Cryptoexception (Ex.getmessage (), ex);    }} catch (InvalidKeyException ex) {throw new Cryptoexception (Ex.getmessage (), ex);  } return EncryptedData;     public static void Main (string[] args) throws Exception {byte[] input = {1,-100, 24, 68,-33,-12,-45,-4};    for (int i = 0; i < input.length; i++) {System.out.print (Input[i] + ",");    } System.out.println ("");    System.out.println (""); byte[] output1 = Decrypt (input, new byte[] {-118,-59,-33, 70, 60, 29,-123, 70, 35, 53,-85,-88, 90, 20,-3, 111,-118    ,-59,-33, 70, 60, 29,-123, 70});    for (int i = 0; i < output1.length; i++) {System.out.print (Output1[i] + ",");  } System.out.println (""); }}



Thank you!




Reply to discussion (solution)

1 floor own account, online etc.! There are ideas and welcome.


I got it, man. Who are those 100 points?
echo bin2hex (' 88888888 '); exit;

$key = ' 56781234d56f012bcd5e701a3cdd670956781234d56f012b ';
$key = Pack (' H48 ', $key);
echo $key; exit;
$iv = Pack (' h* ', ' 0 ');

function Fmt3desex ($s) {
$key = ' 56781234d56f012bcd5e701a3cdd670956781234d56f012b ';
$key = Pack (' H48 ', $key);
$TD = Mcrypt_module_open (Mcrypt_3des, ', MCRYPT_MODE_CBC, ');
$iv = Pack (' H10 ', "0102030405060708"); Like C # new byte[]{1,2,3,4,5,6,7,8}
$iv = Pack (' H10 ', ' 0000000000000000 ');
Mcrypt_generic_init ($TD, $key, $IV);
$encrypted _data = Mcrypt_generic ($TD, $s);
Mcrypt_generic_deinit ($TD);
Mcrypt_module_close ($TD);
Return Bin2Hex ($encrypted _data);
}

echo fmt3desex (' 88888888 ');

The answer is posted to other skeptical classmates.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.