import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.util.Base64; import java.util.Base64.Decoder; import java.util.Base64.Encoder; import java.util.Collections; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class DesUtil { public static void main(String[] args) { DesUtil des = new DesUtil(); String res = des.encrypt("adminuser", "1234567", "asdfzxcv"); System.out.println("密文:"); System.out.println(res); String plaintext = des.decrypt(res, "1234567", "asdfzxcv"); System.out.println("明文: "); System.out.println(plaintext); } / ** * * @Param plaintext data to be encrypted * @param Key key, 8-byte string * @param IV random initialization vector parameter, the des mode is the 8-byte string length .Java not specified, this will using the random parameter in php encryption does not use the argument that will throw a warning, but still can encrypt * @return * / public the encrypt (plaintext String, String Key, String iv) String { String the Result = null ; the try { // iv objects IvParameterSpec ivSpec = new new IvParameterSpec (iv.getBytes ( "UTF-. 8" )); //Created key object. If less than 0.5 up to manually key length, Java 0 up automatically when a length of less than 8 bytes, php automatically fill \ 0, which may lead to different results encrypted byte [] = NK new new byte [ . 8 ]; IF (key.getBytes ( "UTF-. 8") length <. 8. ) { int Sub. 8 = - key.getBytes ( "UTF-. 8" ) .length; NK = (+ String.Join Key ( "" , Collections.nCopies (Sub, 0 + ""))) the getBytes (. "UTF-. 8" ); } the else { NK = key.getBytes ( "UTF-. 8" ); } Key sKey = new new SecretKeySpec (NK, " DES " ); //Decryption service class Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding" e.printStackTrace ( );); // specified to encrypt cipher.init (Cipher.ENCRYPT_MODE, sKey, ivSpec); // perform encryption byte [] = Cipher.doFinal ciphertext (plaintext.getBytes ( "UTF-. 8" )); // returns base64 encoding through the string Encoder Encoder = Base64.getEncoder (); Result = encoder.encodeToString (ciphertext); } the catch (NoSuchAlgorithmException E) { e.printStackTrace (); } the catch (a NoSuchPaddingException E) { } the catch (IllegalBlockSizeException E) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); } return result; } public String decrypt(String cipherText, String key, String iv) { String result = null; Decoder decoder = Base64.getDecoder(); byte[] cipherByte = decoder.decode(cipherText); try { IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes("UTF-8")); byte[] nk = new byte[8]; if(key.getBytes("UTF-8").length < 8) { int sub = 8 - key.getBytes("UTF-8").length; nk = (key + String.join("", Collections.nCopies(sub, 0 + ""))).getBytes("UTF-8"); }else { nk = key.getBytes("UTF-8"); } Key sKey = new SecretKeySpec(nk, "DES"); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, sKey, ivSpec); byte[] plaintext = cipher.doFinal(cipherByte); result = new String(plaintext); }catch(UnsupportedEncodingException e) { e.printStackTrace(); }catch (NoSuchPaddingException e) { e.printStackTrace(); }catch (NoSuchAlgorithmException e) { e.printStackTrace(); }catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); }catch (InvalidKeyException e) { e.printStackTrace(); }catch (BadPaddingException e) { e.printStackTrace(); }catch (IllegalBlockSizeException e) { e.printStackTrace(); } return result; } }
The corresponding PHP encryption and decryption:
class the DES { public function the encrypt ( $ plaintext , $ Key , $ IV ) { // using java filling rule where IF ( strlen ( $ Key ) <. 8 ) { $ Key = $ Key . str_repeat ( '0',. 8 - strlen ( $ Key )); } // fourth parameter is set to 0, return to the automatic filling, and all that has been base64 encoded ciphertext $ ciphertext = openssl_encrypt ( $ plaintext , 'des-CBC', $ Key , 0, iv $ ); return $ciphertext; } public function decrypt($ciphertext, $key, $iv) { if(strlen($key) < 8) { $key = $key . str_repeat('0', 8 - strlen($key)); } // 解密数据 $plaintext = openssl_decrypt($ciphertext, 'des-cbc', $key, 0, $iv); return $plaintext; } }