本文主要是介绍wechat和PHP进行DES加解密统一,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
WECHAT小程序引入crypto-js包
var CryptoJS = require('../../crypto/crypto-js.js');onLoad: function (options) {var keyHex = CryptoJS.enc.Utf8.parse("wechat");var data = '试试能不能解'console.log("原始字符串:" + data);var encrypted = CryptoJS.DES.encrypt(data, keyHex,{mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});var encryptData = encrypted.toString();console.log("加密之后的结果:" + encryptData);var decrypted = CryptoJS.DES.decrypt({ ciphertext: CryptoJS.enc.Base64.parse(encryptData) }, keyHex,{mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});var decryptedData = decrypted.toString(CryptoJS.enc.Utf8);console.log("解密之后的结果:" + decryptedData);}PHP:function myDecrypt($data){//解密$key = "wechat";$decrypted= mcrypt_decrypt(MCRYPT_DES,$key,base64_decode($data),MCRYPT_MODE_ECB);$dec_s = strlen($decrypted);$padding = ord($decrypted[$dec_s-1]);$decrypted = substr($decrypted, 0, -$padding);return $decrypted;}function myEncrypt($data){//加密//初始向量$key = "wechat";$encrypted= mcrypt_encrypt(MCRYPT_DES,$key,PaddingPKCS7($data),MCRYPT_MODE_ECB);return base64_encode($encrypted);}function PaddingPKCS7($data) {$block_size = mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_ECB);$padding_char = $block_size - (strlen($data) % $block_size);$data .= str_repeat(chr($padding_char),$padding_char);return $data;}$conent= myEncrypt('试试能不能解');echo "加密测试:【".$conent."】";$str_decrypt=myDecrypt($conent);echo "还原[".$str_decrypt."]";
工具包下载:
https://download.csdn.net/download/thl331860203/11961203
这篇关于wechat和PHP进行DES加解密统一的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!