How php3DES encryption is compatible with Java

Source: Internet
Author: User
Java source code: {code...} Source: www. cnblogs. commailin... Java source code:

Import java. security. security; import javax. crypto. cipher; import javax. crypto. secretKey; import javax. crypto. spec. secretKeySpec; public class ThreeDES {private static final String Algorithm = "DESede"; // defines the encryption Algorithm. DES, DESede, Blowfish // keybyte can be used as the encryption key, the length is 24 bytes // src is the encrypted data buffer (source) public static byte [] encryptMode (byte [] keybyte, byte [] src) {try {// Generate secret key secret EY = new SecretKeySpec (keybyte, Algorithm); // encrypt Cipher c1 = Cipher. getInstance (Algorithm); c1.init (Cipher. ENCRYPT_MODE, encryption EY); return c1.doFinal (src);} catch (java. security. noSuchAlgorithmException e1) {e1.printStackTrace ();} catch (javax. crypto. noSuchPaddingException e2) {e2.printStackTrace ();} catch (java. lang. exception e3) {e3.printStackTrace ();} return null;} // keybyte is the encryption key, the length is 24 bytes. // src is the encrypted buffer. public static byte [] decryptMode (byte [] keybyte, byte [] src) {try {// Generate secret key secret EY = new SecretKeySpec (keybyte, Algorithm); // decrypt Cipher c1 = Cipher. getInstance (Algorithm); c1.init (Cipher. DECRYPT_MODE, cipher EY); return c1.doFinal (src);} catch (java. security. noSuchAlgorithmException e1) {e1.printStackTrace ();} catch (javax. crypto. noSuchPaddingException e2) {e2.printStackTrace ();} catch (java. lang. exception e3) {e3.printStackTrace ();} return null;} // Convert to a hexadecimal String public static String byte2hex (byte [] B) {String hs = ""; string stmp = ""; for (int n = 0; n
  

Source: http://www.cnblogs.com/mailin...

Reply content:

Java source code:

Import java. security. security; import javax. crypto. cipher; import javax. crypto. secretKey; import javax. crypto. spec. secretKeySpec; public class ThreeDES {private static final String Algorithm = "DESede"; // defines the encryption Algorithm. DES, DESede, Blowfish // keybyte can be used as the encryption key, the length is 24 bytes // src is the encrypted data buffer (source) public static byte [] encryptMode (byte [] keybyte, byte [] src) {try {// Generate secret key secret EY = new SecretKeySpec (keybyte, Algorithm); // encrypt Cipher c1 = Cipher. getInstance (Algorithm); c1.init (Cipher. ENCRYPT_MODE, encryption EY); return c1.doFinal (src);} catch (java. security. noSuchAlgorithmException e1) {e1.printStackTrace ();} catch (javax. crypto. noSuchPaddingException e2) {e2.printStackTrace ();} catch (java. lang. exception e3) {e3.printStackTrace ();} return null;} // keybyte is the encryption key, the length is 24 bytes. // src is the encrypted buffer. public static byte [] decryptMode (byte [] keybyte, byte [] src) {try {// Generate secret key secret EY = new SecretKeySpec (keybyte, Algorithm); // decrypt Cipher c1 = Cipher. getInstance (Algorithm); c1.init (Cipher. DECRYPT_MODE, cipher EY); return c1.doFinal (src);} catch (java. security. noSuchAlgorithmException e1) {e1.printStackTrace ();} catch (javax. crypto. noSuchPaddingException e2) {e2.printStackTrace ();} catch (java. lang. exception e3) {e3.printStackTrace ();} return null;} // Convert to a hexadecimal String public static String byte2hex (byte [] B) {String hs = ""; string stmp = ""; for (int n = 0; n
   

Source: http://www.cnblogs.com/mailin...

Resolved self-answer

class EasyCrypt3Des {    private $_key = "helloworld";     public function __construct($key=null) {        if($key !== null)            $this->_key = $key;    }    public function encrypt($str) {        $td = $this->gettd();        $ret = mcrypt_generic($td, $this->pkcs5_pad($str, 8));        mcrypt_generic_deinit($td);        mcrypt_module_close($td);        return $this->strToHex($ret);    }    public function decrypt($str) {        $td = $this->gettd();        $ret = $this->pkcs5_unpad(mdecrypt_generic($td, $this->hexToStr($str)));        mcrypt_generic_deinit($td);        mcrypt_module_close($td);        return $ret;    }    private function pkcs5_pad($text, $blocksize) {        $pad = $blocksize - (strlen($text) % $blocksize);        return $text . str_repeat(chr($pad), $pad);    }    private function pkcs5_unpad($text) {        $pad = ord($text{strlen($text) - 1});        if ($pad > strlen($text)) {            return false;        }        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {            return false;        }        return substr($text, 0, -1 * $pad);    }    private function getiv() {        return pack('H16', '0000000000000000');    }    private function gettd() {        $iv = $this->getiv();        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, '');        mcrypt_generic_init($td, $this->_key, $iv);        return $td;    }    private function strToHex($string){        $hex = '';        for ($i=0; $i
    

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.