ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

php接入钉钉注册回调

2021-02-17 11:30:05  阅读:413  来源: 互联网

标签:加密 注册 接入 text base64 module key mcrypt php


示例框架-Yii2.0+mysql+crm+crm管理系统

1:首先需要下载钉钉官方的提供的php加密Demo
在这里插入图片描述

2:相信在做注册审批回调的博友们已经将接入了钉钉sdk,我就不一一展示1了接入流程了—直接上发起代码示例(aeskey必须是43为切记)
在这里插入图片描述

3:官方开放平台提供的PHP Demo:pkcs7Encoder.php文件修改

function Prpcrypt($k){
    $this->key = base64_decode($k . "=");}修改为构造函数形式:function __construct($k){
    $this->key = base64_decode($k . "=");}12345678

4:官方开放平台提供的PHP Demo:DingtalkCrypt.php文件修改

//加密
	public function encrypt($text, $corpid)
	{
		try {
			//获得16位随机字符串,填充到明文之前
			$random = $this->getRandomStr();
			$text = $random . pack("N", strlen($text)) . $text . $corpid;
			// 网络字节序
			// $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
			// $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
			$iv = substr($this->key, 0, 16);
			//使用自定义的填充方式对明文进行补位填充
			$pkc_encoder = new PKCS7Encoder;
			$text = $pkc_encoder->encode($text);
			// mcrypt_generic_init($module, $this->key, $iv);
			// //加密
			// $encrypted = mcrypt_generic($module, $text);
			// mcrypt_generic_deinit($module);
			// mcrypt_module_close($module);

			//print(base64_encode($encrypted));
			//使用BASE64对加密后的字符串进行编码
			$encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv );
			return array(ErrorCode::$OK, base64_encode($encrypted));
		} catch (Exception $e) {
			print $e;
			return array(ErrorCode::$EncryptAESError, null);
		}
	}
	//解密
	public function decrypt($encrypted, $corpid)
	{
		try {
			$ciphertext_dec = base64_decode($encrypted);
			// $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
			$iv = substr($this->key, 0, 16);
			// mcrypt_generic_init($module, $this->key, $iv);
			// $decrypted = mdecrypt_generic($module, $ciphertext_dec);
			// mcrypt_generic_deinit($module);
			// mcrypt_module_close($module);
			$decrypted = openssl_decrypt($ciphertext_dec, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv);
		} catch (Exception $e) {
			return array(ErrorCode::$DecryptAESError, null);
		}
		try {
			//去除补位字符
			$pkc_encoder = new PKCS7Encoder;
			$result = $pkc_encoder->decode($decrypted);
			//去除16位随机字符串,网络字节序和AppId
			if (strlen($result) < 16)
				return "";
			$content = substr($result, 16, strlen($result));
			$len_list = unpack("N", substr($content, 0, 4));
			$xml_len = $len_list[1];
			$xml_content = substr($content, 4, $xml_len);
			$from_corpid = substr($content, $xml_len + 4);
		} catch (Exception $e) {
			print $e;
			return array(ErrorCode::$DecryptAESError, null);
		}
		if ($from_corpid != $corpid)
			return array(ErrorCode::$ValidateSuiteKeyError, null);
		return array(0, $xml_content);

	}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465

6:测试回调url示例

public function actionReceiveCallBack(){
        //接收值 sign 时间戳   
        $signature=$_GET['signature'];    
        $nonce=$_GET['nonce'];         
        $timeStamp=$_GET['timestamp'];
        $suiteKey=Yii::$app->params['corpid'];//必填,企业ID
        $token="dingtalk";    //必须和在注册是一样
        //接收传过来的需要解密的值
        $postdata = file_get_contents("php://input");
        $postList = json_decode($postdata,true);
        $encrypt = $postList['encrypt'];
        //注册时加密的key
        $aesKey=\Yii::$app->params['aes_key'];    
        $aes_key_encode=base64_encode($aesKey);
        $aes_key=substr($aes_key_encode,0,-1);
        $decryptMsg="";
        $crypt = new DingCallbackCrypt($token,$aes_key,$suiteKey);
        $encryData = $crypt->DecryptMsg($signature,$timeStamp,$nonce,$encrypt,$decryptMsg);  //解密
        if($encryData['errcode']!==0){
         
        }else{
            if($newData->EventType=="bpms_task_change"||$newData->EventType=="bpms_instance_change"){
                    $dingtalk=new DingtalkController();
                    $dingtalk->UpdateProcess($newData->processInstanceId);
            }
            $msg="success";
            $encryptMsg="";
            $data = $crypt->EncryptMsg($msg,$timeStamp,$nonce,$encryptMsg);   //加密
            if($data['errcode']!=0){
               
            }else{
                return $data['data'];   //返回加密成功的json数据 
            }    
        }
    }1234567891011121314151617181920212223242526272829303132333435

我这里是直接将注册的事件写死了 ,需要注册其他的更改一下就可以。

  1. 钉钉业务事件回调 - 回调管理API

  2. 回调事件消息体加密

标签:加密,注册,接入,text,base64,module,key,mcrypt,php
来源: https://blog.csdn.net/fd746/article/details/113832375

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有