php 3DES加密如何相容Java

來源:互聯網
上載者:User
Java源碼:

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"; //定義 密碼編譯演算法,可用 DES,DESede,Blowfish        //keybyte為加密金鑰,長度為24位元組    //src為被加密的資料緩衝區(源)    public static byte[] encryptMode(byte[] keybyte, byte[] src) {       try {            //產生密鑰            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);            //加密            Cipher c1 = Cipher.getInstance(Algorithm);            c1.init(Cipher.ENCRYPT_MODE, deskey);            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為加密金鑰,長度為24位元組    //src為加密後的緩衝區    public static byte[] decryptMode(byte[] keybyte, byte[] src) {          try {            //產生密鑰            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);            //解密            Cipher c1 = Cipher.getInstance(Algorithm);            c1.init(Cipher.DECRYPT_MODE, deskey);            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;    }    //轉換成十六進位字串    public static String byte2hex(byte[] b) {        String hs="";        String stmp="";        for (int n=0;n

來源:http://www.cnblogs.com/mailin...

回複內容:

Java源碼:

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"; //定義 密碼編譯演算法,可用 DES,DESede,Blowfish        //keybyte為加密金鑰,長度為24位元組    //src為被加密的資料緩衝區(源)    public static byte[] encryptMode(byte[] keybyte, byte[] src) {       try {            //產生密鑰            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);            //加密            Cipher c1 = Cipher.getInstance(Algorithm);            c1.init(Cipher.ENCRYPT_MODE, deskey);            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為加密金鑰,長度為24位元組    //src為加密後的緩衝區    public static byte[] decryptMode(byte[] keybyte, byte[] src) {          try {            //產生密鑰            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);            //解密            Cipher c1 = Cipher.getInstance(Algorithm);            c1.init(Cipher.DECRYPT_MODE, deskey);            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;    }    //轉換成十六進位字串    public static String byte2hex(byte[] b) {        String hs="";        String stmp="";        for (int n=0;n

來源:http://www.cnblogs.com/mailin...

已解決 自問自答

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
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.