dougong2306
dougong2306
2018-02-08 08:50
浏览 143

使用KEY和IV在PHP中进行MD5和Triple Des加密

I have given a task to integrate our company system to external system via API. When I read about the API, it stated that the data should be encrypted using MD5 and Triple Des Encryption, so I made some research about how to do it.

// Here's the credentials to be used for encryption.

$key = 'dEvu4MHkqz7mRgeqmB1mQEXi';
$iv  = "avz9bUNx";

The data should be formatted as JSON String before encryption.

$params = array(
        'Number' => '+11177109886' // example
    );

$text = json_encode($params);

After that I made some codes to encrypt it.

function apiEncode($text, $key, $iv)
{
    // to append string with trailing characters as for PKCS7 padding scheme
    $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
    $padding = $block - (strlen($text) % $block);
    $text .= str_repeat(chr($padding), $padding);

    $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
    return base64_encode($crypttext);
}

The error I got is.

Warning: mcrypt_encrypt(): Received initialization vector of size 0, but size 32 is required for this encryption mode in /Applications/XAMPP/xamppfiles/htdocs/dapsapi/rdremit.php on line 20

图片转代码服务由CSDN问答提供 功能建议

我已经完成了通过API将公司系统集成到外部系统的任务。 当我读到API时,它说应该使用MD5和Triple Des Encryption加密数据,所以我做了一些关于如何做的研究。

  //这里是 用于加密的凭证。
 
 $ key ='dEvu4MHkqz7mRgeqmB1mQEXi'; 
 $ iv =“avz9bUNx”; 
   
 
 

数据应格式化 作为加密前的JSON字符串。

  $ params = array(
'Number'=>'+ 11177109886'// example 
); 
 
 $ text  = json_encode($ params); 
   
 
 

之后我做了一些代码来加密它。

  function apiEncode($ text,$ key,$ iv)
 {
 //以追加带有尾随字符的字符串,如PKCS7填充方案
 $ block = mcrypt_get_block_size  (MCRYPT_RIJNDAEL_256,MCRYPT_MODE_CBC); 
 $ padding = $ block  - (strlen($ text)%$ block); 
 $ text。= str_repeat(chr($ padding),$ padding); 
 
 $ crypttext =  mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$ key,$ text,MCRYPT_MODE_CBC,$ iv); 
返回base64_encode($ crypttext); 
} 
   
 
 

我得到的错误是 。

警告:mcrypt_encrypt():在 / Applications / XAMPP / xamppfiles中,此加密模式需要接收大小为0的初始化向量, 但大小为32 第20行/htdocs/dapsapi/rdremit.php

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

相关推荐