PHP and Java-compatible des encryption and decryption code sharing

Source: Internet
Author: User
Tags crypt
This article mainly introduces the des encryption and decryption code sharing that is compatible with PHP and Java. it is suitable for cases where the server is written in JAVA and the client is written in PHP and des encryption and decryption is required, for more information about php code, see:

<? 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 "));}}

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.