PHP Code:
<?phpclass Des{var $key; var $iv;//Offset function DES ($key, $iv =0) {$this->key = $key; if ($iv = = 0) {$this->iv = $key; }else {$this->iv = $iv;}} Cryptographic function Encrypt ($STR) {$size = Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC); $str = $this->pkcs5pad ($str , $size); $data =MCRYPT_CBC (mcrypt_des, $this->key, $str, Mcrypt_encrypt, $this->iv);//$data =strtoupper (Bin2Hex ($data)) ; Returns the uppercase hexadecimal string return Base64_encode ($data);} Decryption function Decrypt ($str) {$str = Base64_decode ($STR),//$strBin = $this->hex2bin (strtolower ($STR)); $str = Mcrypt_ CBC (Mcrypt_des, $this->key, $str, Mcrypt_decrypt, $this->iv); $str = $this->pkcs5unpad ($str); return $str;} function Hex2bin ($hexData) {$binData = ""; for ($i = 0; $i < strlen ($hexData); $i + = 2) {$binData. = Chr (Hexdec (substr ($hexData, $i, 2)));} return $binData;} function Pkcs5pad ($text, $blocksize) {$pad = $blocksize-(strlen ($text)% $blocksize), return $text. Str_repeat (Chr ( $pad), $pad);} function Pkcs5unpad ($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);} $str = ' ABCD '; $key = ' asdfwef5 '; $crypt = new DES ($key); $mstr = $crypt->encrypt ($str); $str = $crypt->decrypt ($MSTR); echo $str. ' <=> ' $mstr;?>
Java code:
Package com.test; Import it.sauronsoftware.base64.Base64; Import Java.security.key;import Java.security.securerandom;import Java.security.spec.AlgorithmParameterSpec; Import Javax.crypto.cipher;import Javax.crypto.secretkeyfactory;import Javax.crypto.spec.deskeyspec;import Javax.crypto.spec.IvParameterSpec; public class main{public static final String algorithm_des = "des/cbc/pkcs5padding"; /** * des algorithm, encryption * * @param data to encrypt the string * @param key Encryption private key, length can not be less than 8 bit * @return encrypted byte array, generally combined with BASE64 encoding * @throws Cryptexception Exception */public static string encode (string key,string data) throws Exception {return encode (key, data . GetBytes ()); }/** * des algorithm, encryption * * @param data to encrypt the string * @param key Encryption private key, length can not be less than 8 bit * @return encrypted byte array, generally combined with BASE64 encoding * @thro WS Cryptexception Exception */public static String encode (string key,byte[] data) throws Exception {try {Deskeyspec DKs = new Deskeyspec (Key.getbytes ()); Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES"); The length of the key cannot be less than 8 bytes key Secretkey = Keyfactory.generatesecret (DKS); Cipher Cipher = cipher.getinstance (algorithm_des); Ivparameterspec IV = new Ivparameterspec (Key.getbytes ()); Algorithmparameterspec Paramspec = IV; Cipher.init (Cipher.encrypt_mode, Secretkey,paramspec); byte[] bytes = cipher.dofinal (data); Return Byte2hex (bytes); return new String (Base64.encode (bytes)); } catch (Exception e) {throw new Exception (e); }}/** * des algorithm, Decrypt * * @param data to decrypt the string * @param key to decrypt the private key, length cannot be less than 8 bits * @return decrypted byte array * @throws exceptio N Exception */public static byte[] Decode (String key,byte[] data) throws Exception {try {securerandom sr = new Se Curerandom (); Deskeyspec DKs = new Deskeyspec (Key.getbytes ()); Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES"); The length of the key cannot be less than 8 bytes key Secretkey = Keyfactory.generatesecret (DKS); Cipher Cipher = cipher.getinstance (algorithm_des); Ivparameterspec IV = new Ivparameterspec (Key.getbytes ()); Algorithmparameterspec Paramspec = IV; Cipher.init (Cipher.decrypt_mode, Secretkey,paramspec); return cipher.dofinal (data); } catch (Exception e) {throw new Exception (e); }}/** * Gets the encoded value * @param key * @param data * @return * @throws Exception */public static String Decodev Alue (String key,string data) {byte[] datas; String value = null;try {datas = decode (key, Base64.decode (Data.getbytes ())); value = new String (datas);} catch (Except Ion e) {value = "";} return value; } public static void Main (string[] args) throws Exception {System.out.println ("Ming: ABCD; Secret:" + Main.encode ("Asdfwef5", " ABCD ")); }}
http://www.bkjia.com/PHPjc/824790.html www.bkjia.com true http://www.bkjia.com/PHPjc/824790.html techarticle PHP Code: Phpclass des{var $key; var $iv;//Offset function DES ($key, $iv =0) {$this-key = $key; if ($iv = = 0) {$this-iv = $key;} else {$this-iv = $iv;}} Cryptographic function Encrypt ($str ...