Java-could you help convert this DES encryption code to PHP? is this the DES algorithm ..

Source: Internet
Author: User
{Code ...}
Import sun. misc. BASE64Decoder; import sun. misc. BASE64Encoder; import javax. crypto. cipher; import javax. crypto. secretKey; import javax. crypto. secretKeyFactory; import javax. crypto. spec. DESKeySpec; import java. io. IOException; import java. security. secureRandom; public class PHPDESEncrypt {String key; public PHPDESEncrypt () {} public PHPDESEncrypt (String key) {this. key = key;} public byte [] desEncrypt (byte [] plainText) throws Exception {SecureRandom sr = new SecureRandom (); DESKeySpec dks = new DESKeySpec (key. getBytes (); SecretKeyFactory keyFactory = SecretKeyFactory. getInstance ("DES"); SecretKey key = keyFactory. generateSecret (dks); Cipher cipher = Cipher. getInstance ("DES"); cipher. init (Cipher. ENCRYPT_MODE, key, sr); byte data [] = plainText; byte encryptedData [] = cipher. doFinal (data); return encryptedData;} public byte [] desDecrypt (byte [] encryptText) throws Exception {SecureRandom sr = new SecureRandom (); DESKeySpec dks = new DESKeySpec (key. getBytes (); SecretKeyFactory keyFactory = SecretKeyFactory. getInstance ("DES"); SecretKey key = keyFactory. generateSecret (dks); Cipher cipher = Cipher. getInstance ("DES"); cipher. init (Cipher. DECRYPT_MODE, key, sr); byte encryptedData [] = encryptText; byte decryptedData [] = cipher. doFinal (encryptedData); return decryptedData;} public String encrypt (String input) throws Exception {return base64Encode (desEncrypt (input. getBytes ())). replaceAll ("\ s *", "");} public String decrypt (String input) throws Exception {byte [] result = base64Decode (input ); return new String (desDecrypt (result);} public String base64Encode (byte [] s) {if (s = null) return null; BASE64Encoder B = new sun. misc. BASE64Encoder (); return B. encode (s);} public byte [] base64Decode (String s) throws IOException {if (s = null) {return null;} BASE64Decoder decoder = new BASE64Decoder (); byte [] B = decoder. decodeBuffer (s); return B;} public static void main (String [] args) {try {PHPDESEncrypt d = new PHPDESEncrypt ("12345678"); String p = d. encrypt ("love"); System. out. println ("ciphertext:" + p);} catch (Exception e) {e. printStackTrace () ;}} public String getKey () {return key;} public void setKey (String key) {this. key = key ;}}

Reply content:
Import sun. misc. BASE64Decoder; import sun. misc. BASE64Encoder; import javax. crypto. cipher; import javax. crypto. secretKey; import javax. crypto. secretKeyFactory; import javax. crypto. spec. DESKeySpec; import java. io. IOException; import java. security. secureRandom; public class PHPDESEncrypt {String key; public PHPDESEncrypt () {} public PHPDESEncrypt (String key) {this. key = key;} public byte [] desEncrypt (byte [] plainText) throws Exception {SecureRandom sr = new SecureRandom (); DESKeySpec dks = new DESKeySpec (key. getBytes (); SecretKeyFactory keyFactory = SecretKeyFactory. getInstance ("DES"); SecretKey key = keyFactory. generateSecret (dks); Cipher cipher = Cipher. getInstance ("DES"); cipher. init (Cipher. ENCRYPT_MODE, key, sr); byte data [] = plainText; byte encryptedData [] = cipher. doFinal (data); return encryptedData;} public byte [] desDecrypt (byte [] encryptText) throws Exception {SecureRandom sr = new SecureRandom (); DESKeySpec dks = new DESKeySpec (key. getBytes (); SecretKeyFactory keyFactory = SecretKeyFactory. getInstance ("DES"); SecretKey key = keyFactory. generateSecret (dks); Cipher cipher = Cipher. getInstance ("DES"); cipher. init (Cipher. DECRYPT_MODE, key, sr); byte encryptedData [] = encryptText; byte decryptedData [] = cipher. doFinal (encryptedData); return decryptedData;} public String encrypt (String input) throws Exception {return base64Encode (desEncrypt (input. getBytes ())). replaceAll ("\ s *", "");} public String decrypt (String input) throws Exception {byte [] result = base64Decode (input ); return new String (desDecrypt (result);} public String base64Encode (byte [] s) {if (s = null) return null; BASE64Encoder B = new sun. misc. BASE64Encoder (); return B. encode (s);} public byte [] base64Decode (String s) throws IOException {if (s = null) {return null;} BASE64Decoder decoder = new BASE64Decoder (); byte [] B = decoder. decodeBuffer (s); return B;} public static void main (String [] args) {try {PHPDESEncrypt d = new PHPDESEncrypt ("12345678"); String p = d. encrypt ("love"); System. out. println ("ciphertext:" + p);} catch (Exception e) {e. printStackTrace () ;}} public String getKey () {return key;} public void setKey (String key) {this. key = key ;}}

There are several kinds of des encryption .. I don't know which one you are... There are two types, you can compare php Des encryption

This ...... The code you posted isjavaRight! You want to usephpOfDESAt least encryption is required for decryption.keyAnd the corresponding encryption mode, for exampleCBC,CFB,ECBEtc! After confirmation, it should be okay to use the dome upstairs for you.

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.