search

Home  >  Q&A  >  body text

Guys, how do you implement Java's PbeWithMd5AndDes encryption and decryption in PHP?

The java code is as follows

/**
 * 加密方法
 * @param key 密钥
 * @param plainText 明文
 * @return 密文
 */
public static String encrypt(String key, String plainText){
   String encryptTxt = "";
   try {
      byte[] salt = new byte[8];
      MessageDigest md = MessageDigest.getInstance("MD5");
      md.update(key.getBytes());
      byte[] digest = md.digest();
      for (int i = 0; i < 8; i++) {
         salt[i] = digest[i];
      }
      PBEKeySpec pbeKeySpec = new PBEKeySpec(key.toCharArray());
      SecretKeyFactory keyFactory = SecretKeyFactory
            .getInstance("PBEWithMD5AndDES");
      SecretKey skey = keyFactory.generateSecret(pbeKeySpec);
      PBEParameterSpec paramSpec = new PBEParameterSpec(salt, ITERATIONS);
      Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
      cipher.init(Cipher.ENCRYPT_MODE, skey, paramSpec);
      byte[] cipherText = cipher.doFinal(plainText.getBytes());
      String saltString = new String(Base64.encode(salt));
      String ciphertextString = new String(Base64.encode(cipherText));
      return saltString + ciphertextString;
   } catch (Exception e) {
      e.printStackTrace();
   }
   return "";
}

How to use PHP to decrypt the data encrypted by this java code

七1606 days ago1285

reply all(0)I'll reply

No reply
  • Cancelreply