{"id":524,"title":"java\u548cphp\u5bf9\u63a5\u901a\u7528\u5bf9\u79f0\u52a0\u89e3\u5bc6\u65b9\u6cd5","good":0,"bad":0,"hit":1756,"created_at":"2021-03-11 03:12:05","content":"

java\u548cphp\u5bf9\u63a5\uff0c\u5bf9\u4e8e\u6570\u636e\u52a0\u89e3\u5bc6\u7684\u65b9\u6cd5\u3002<\/span><\/p>

<\/span><\/p>

java\u4ee3\u7801\uff1a<\/span><\/p>

<\/span><\/p>

import javax.crypto.Cipher;\nimport javax.crypto.spec.SecretKeySpec;\nimport java.util.Base64;\n \n\/**\n * @author baihe\n *\/ public class AES {\n \n    \/**\n * \u52a0\u5bc6\u7b97\u6cd5\n  *\n * @param sSrc\n  * @param sKey\n  * @return\n  * @throws Exception\n *\/  public static String Encrypt(String sSrc, String sKey) throws Exception {\n        if (sKey == null) {\n            System.out.print("Key\u4e3a\u7a7anull");\n            return null;\n        }\n        \/\/ \u5224\u65adKey\u662f\u5426\u4e3a16\u4f4d\n  if (sKey.length() != 16) {\n            System.out.print("Key\u957f\u5ea6\u4e0d\u662f16\u4f4d");\n            return null;\n        }\n        byte[] raw = sKey.getBytes("utf-8");\n        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");\n        Cipher cipher = Cipher.getInstance("AES\/ECB\/PKCS5Padding");\/\/"\u7b97\u6cd5\/\u6a21\u5f0f\/\u8865\u7801\u65b9\u5f0f"\n  cipher.init(Cipher.ENCRYPT_MODE, skeySpec);\n        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));\n \n        String result = Base64.getEncoder().encodeToString(encrypted);\n        return java.net.URLEncoder.encode(result, "UTF-8"); \/\/\u6b64\u5904\u4f7f\u7528BASE64\u505a\u8f6c\u7801\u529f\u80fd\uff0c\u540c\u65f6\u80fd\u8d77\u52302\u6b21\u52a0\u5bc6\u7684\u4f5c\u7528\u3002\n  }\n \n    \/\/ \u89e3\u5bc6\u7b97\u6cd5\n  public static String Decrypt(String sSrc, String sKey) throws Exception {\n        try {\n            \/\/ \u5224\u65adKey\u662f\u5426\u6b63\u786e\n  if (sKey == null) {\n                System.out.print("Key\u4e3a\u7a7anull");\n                return null;\n            }\n            \/\/ \u5224\u65adKey\u662f\u5426\u4e3a16\u4f4d\n  if (sKey.length() != 16) {\n                System.out.print("Key\u957f\u5ea6\u4e0d\u662f16\u4f4d");\n                return null;\n            }\n            byte[] raw = sKey.getBytes("utf-8");\n            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");\n            Cipher cipher = Cipher.getInstance("AES\/ECB\/PKCS5Padding");\n            cipher.init(Cipher.DECRYPT_MODE, skeySpec);\n            try {\n                sSrc = java.net.URLDecoder.decode(sSrc, "UTF-8");\n                byte[] encrypted1 = Base64.getDecoder().decode(sSrc);\/\/\u5148\u7528base64\u89e3\u5bc6\n  byte[] original = cipher.doFinal(encrypted1);\n                String originalString = new String(original, "utf-8");\n                return originalString;\n            } catch (Exception e) {\n                System.out.println(e.toString());\n                return null;\n            }\n        } catch (Exception ex) {\n            System.out.println(ex.toString());\n            return null;\n        }\n    }\n \n    public static void main(String[] args) throws Exception {\n        \/*\n * \u6b64\u5904\u4f7f\u7528AES-128-ECB\u52a0\u5bc6\u6a21\u5f0f\uff0ckey\u9700\u8981\u4e3a16\u4f4d\u3002  *\/  String cKey = "WeDoct_XiangYang";\n        \/\/ \u9700\u8981\u52a0\u5bc6\u7684\u5b57\u4e32\n  String cSrc = "{\\"doctorIdcard\\":\\"420682198102024516\\",\\"doctorBelongTeamid\\":\\"84\\",\\"idcard\\":\\"110101200003070236\\",\\"mobile\\":\\"13910043421\\",\\"name\\":\\"\\\\u5927\\\\u767d\\",\\"type\\":\\"1\\",\\"relationship\\":\\"10\\",\\"relationDesc\\":\\"\\\\u5b59\\\\u5973\\",\\"liveProvinceid\\":\\"420000000000\\",\\"liveCityid\\":\\"420600000000\\",\\"liveCountyid\\":\\"420682000000\\",\\"liveTownshipid\\":\\"420682104000\\",\\"liveVillageid\\":\\"420682104001\\",\\"liveDetailAddress\\":\\"\\\\u8944\\\\u9633\\\\u5e02\\",\\"crowdType\\":\\"1\\",\\"isTuberculosis\\":\\"0\\",\\"isHypertension\\":\\"0\\",\\"isDiabetes\\":\\"0\\",\\"isPsychologica\\":\\"0\\",\\"isDestitute\\":\\"0\\",\\"isDisabilit\\":\\"0\\",\\"isPoverty\\":\\"0\\",\\"isFlowPeople\\":\\"1\\",\\"isFiveInsured\\":\\"0\\",\\"isLowInsured\\":\\"0\\",\\"app_id\\":\\"xysignbaokang\\",\\"app_secret\\":\\"3RDfblpUvziDOmSk\\"}";\n \n        System.out.println(cSrc);\n        \/\/ \u52a0\u5bc6\n  String enString = AES.Encrypt(cSrc, cKey);\n        System.out.println("\u52a0\u5bc6\u540e\u7684\u5b57\u4e32\u662f\uff1a" + enString);\n \n        \/\/ \u89e3\u5bc6\n  String DeString = AES.Decrypt(enString, cKey);\n        System.out.println("\u89e3\u5bc6\u540e\u7684\u5b57\u4e32\u662f\uff1a" + DeString);\n    }\n}\n\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\n\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3aCSDN\u535a\u4e3b\u300c\u300a\u5206\u5206\u5408\u5408\u597d\uff0ccvcx\u300d\u7684\u539f\u521b\u6587\u7ae0\uff0c\u9075\u5faaCC 4.0 BY-SA\u7248\u6743\u534f\u8bae\uff0c\u8f6c\u8f7d\u8bf7\u9644\u4e0a\u539f\u6587\u51fa\u5904\u94fe\u63a5\u53ca\u672c\u58f0\u660e\u3002\n\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/blog.csdn.net\/cpongo3\/article\/details\/93625154<\/pre>

<\/span><\/p>

<\/p>

php\u4ee3\u7801\uff1a<\/p>

public static function WeDecrypt($input, $securityKey) {\n  $input = urldecode($input); \n  $input = openssl_decrypt($input, 'AES-128-ECB', $securityKey);    \n  if (!$input) {\n    return false;\n   }  \n   return $input; \n }\n \npublic static function WeDoctorEncrypt($input, $securityKey) {\n  \n  $inputArr = json_decode($input, true);\/\/\u8f6c\u4e3a\u6570\u7ec4\n  if (!is_array($inputArr) || empty($inputArr)) {\n    return false;\n   }    \n   $input = json_encode($inputArr, JSON_UNESCAPED_UNICODE);\/\/\u8f6c\u4e3ajson\u5b57\u7b26\u4e32   \n   \/\/\u8fdb\u884cAes\u52a0\u5bc6  \n   $data = openssl_encrypt($input, 'AES-128-ECB', $securityKey);\n    return urlencode($data); \n }<\/pre>

https:\/\/blog.csdn.net\/cpongo3\/article\/details\/93625154<\/a> <\/p>

<\/p>

php\u5b9e\u73b0DES\u52a0\u5bc6\u89e3\u5bc6\u62a5Call to undefined function mcrypt_create_iv()\u9519\u89e3\u51b3<\/p>

https:\/\/blog.csdn.net\/huaweichenai\/article\/details\/103503092<\/a> <\/p>

<\/p>"}