This article mainly describes the compatibility of PHP and Java des Encryption and decryption code sharing, suitable for such as the Java language Server is written, the client is written in PHP, and need des encryption decryption situation, the need for friends can refer to the next
Php
<?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
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 {DESK Eyspec 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 Excepti On exception */public static byte[] Decode (String key,byte[] data) throws Exception {try {securerandom sr = n EW SecureRandom (); 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 decode Value (String key,string data) {byte[] datas; String value = null; try {datas = decode (key, Base64.decode (Data.getbytes ())); Value = new String (datas); } catch (Exception e) {value = ""; } return value; } public static void Main (string[] args) throws Exception {System.out.println ("Ming: ABCD; Secret:" + Main.encode ("Asdfwef5 "," ABCD ")); }}
PHP and Java-compatible DES encryption and decryption code sharing