我在Php中有一个Aes加密代码我希望C#中的那个克隆相同我怎么能这样做 [英] I Have A Aes Encryption Code In Php I Want Same Clone Of That In C# How Can I Do That

查看:80
本文介绍了我在Php中有一个Aes加密代码我希望C#中的那个克隆相同我怎么能这样做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *
这是PHP中的AES加密和解密代码(128位)* /

error_reporting(0);



函数加密($ plainText,$ key)

{

$ secretKey = hextobin(md5($ key));

$ initVector = pack(C *,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f);



/ *打开模块和创建IV(初始化向量)* /

$ openMode = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc','');

$ blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,'cbc');

$ plainPad = pkcs5_pad($ plainText,$ blockSize);



/ *初始化加密句柄* /

if(mcrypt_generic_init($ openMode,$ secretKey,$ initVector)!= -1)

{

/ *加密数据* /

$ encryptedText = mcrypt_generic($ openMode,$ plainPad);

mcrypt_generic_dein它($ openMode);



}



返回bin2hex($ encryptedText);

}



函数解密($ encryptedText,$ key)

{

$ secretKey = hextobin(md5($ key));

$ initVector = pack(C *,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b ,0x0c,0x0d,0x0e,0x0f);

$ encryptedText = hextobin($ encryptedText);



/ *打开模块,然后创建IV * /

$ openMode = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc','');





mcrypt_generic_init($ openMode,$ secretKey,$ initVector);

$ decryptedText = mdecrypt_generic($ openMode,$ encryptedText);



//从字符串末尾删除空值

$ decryptedText = rtrim($ decryptedText,\ 0);



//返回Decrypted string:some text here



mcrypt_generic_deinit( $ openMode);



返回$ decryptedText;



}

/ / ***********填充功能*********************



函数pkcs5_pad($ plainText,$ blockSize)

{

$ pad = $ blockSize - (strlen($ plainText)%$ blockSize);

返回$ plainText。 str_repeat(chr($ pad),$ pad);

}



// **********十六进制到二进制函数的php 4.0版本********



函数hextobin($ hexString)

{

$ length = strlen($ hexString);

$ binString =;

$ count = 0;

while($ count< $ length)

{

$ subString = substr($ hexString,$ count,2);

$ packedString = pack(H *,$ subString);

if($ count == 0)

{

$ binString = $ packedString;

}



其他

{

$ binString。= $ packedString;

}



$ count + = 2;

}

返回$ binString;

}

?>









i希望在c#中克隆此代码我有一个密钥和消息要加密...

任何帮助都会非常明显..

先谢谢..

/* This is an AES Encryption and Decryption Code (128 Bit) in PHP */
error_reporting(0);

function encrypt($plainText,$key)
{
$secretKey = hextobin(md5($key));
$initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);

/* Open module and Create IV (Intialization Vector) */
$openMode = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '','cbc', '');
$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, 'cbc');
$plainPad = pkcs5_pad($plainText, $blockSize);

/* Initialize encryption handle */
if (mcrypt_generic_init($openMode, $secretKey, $initVector) != -1)
{
/* Encrypt data */
$encryptedText = mcrypt_generic($openMode, $plainPad);
mcrypt_generic_deinit($openMode);

}

return bin2hex($encryptedText);
}

function decrypt($encryptedText,$key)
{
$secretKey = hextobin(md5($key));
$initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f);
$encryptedText=hextobin($encryptedText);

/* Open module, and create IV */
$openMode = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '','cbc', '');


mcrypt_generic_init($openMode, $secretKey, $initVector);
$decryptedText = mdecrypt_generic($openMode, $encryptedText);

// Drop nulls from end of string
$decryptedText = rtrim($decryptedText, "\0");

// Returns "Decrypted string: some text here"

mcrypt_generic_deinit($openMode);

return $decryptedText;

}
//*********** Padding Function *********************

function pkcs5_pad ($plainText, $blockSize)
{
$pad = $blockSize - (strlen($plainText) % $blockSize);
return $plainText . str_repeat(chr($pad), $pad);
}

//********** Hexadecimal to Binary function for php 4.0 version ********

function hextobin($hexString)
{
$length = strlen($hexString);
$binString="";
$count=0;
while($count<$length)
{
$subString =substr($hexString,$count,2);
$packedString = pack("H*",$subString);
if ($count==0)
{
$binString=$packedString;
}

else
{
$binString.=$packedString;
}

$count+=2;
}
return $binString;
}
?>




i want to clone this code in c# i have a key and message to encrypt...
any help will be really appreciable..
Thanks in Advance..

推荐答案

plainText,
plainText,


key)

{
key)
{


secretKey = hextobin(md5(
secretKey = hextobin(md5(


这篇关于我在Php中有一个Aes加密代码我希望C#中的那个克隆相同我怎么能这样做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆