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 ;}} // encryption 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 )); // return the uppercase hexadecimal string return base6 4_encode ($ data);} // decrypt 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;} fu Nction 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 = 'asdfwef '; $ 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, encrypt ** @ param data the string to be encrypted * @ param key to encrypt the private key. The length cannot be less than 8 bits * @ return the encrypted byte array, * @ throws CryptException Exception */public static String encode (String key, String data) throws Exception {return encode (key, data. getBytes ();}/*** DES algorithm, encryption ** @ param data string to be encrypted * @ param key to encrypt the private key, the length cannot be less than 8 characters * @ return: the encrypted byte array. Generally, the * @ throws CryptException exception is used in combination with Base64 encoding. */public static String encode (String key, byte [] data) throws Exception {try {shorteyspec dks = new shorteyspec (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, decryption ** @ param data string to be decrypted * @ param key to decrypt the private key, the length cannot be less than 8 characters * @ return the decrypted byte array * @ throws Exception */public static byte [] decode (String key, byte [] data) throws Exception {try {SecureRandom sr = new 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 );}} /*** get the encoded value * @ param key * @ param data * @ return * @ throws Exception */public static String decodeValue (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; Password:" + Main. encode ("asdfwef5", "abcd "));}}