欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP和.NET关于3DES加密兼容问题

程序员文章站 2022-06-09 18:19:35
...

下面是我的C#加解密代码: classDES { //密钥 privateconststringsKey="nuhyrn83Y9LNeygnpq7AMN0aeRQN7kuv"; //矢量,矢量可以为空 privateconststringsIV="qciO6X+aPLw="; ///summary ///加密 ////summary ///paramname="Value"明文/param ///returns密文ba

下面是我的C#加解密代码:

class DES

{

//密钥

private const string sKey = "nuhyrn83Y9LNeygnpq7AMN0aeRQN7kuv";

//矢量,矢量可以为空

private const string sIV = "qciO6X+aPLw=";

///

/// 加密

///

/// 明文

/// 密文 base64转码

public static string EncryptString(string Value)

{

SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();

ICryptoTransform ct;

MemoryStream ms;

CryptoStream cs;

byte[] byt;

mCSP.Key = Convert.FromBase64String(sKey);

mCSP.IV = Convert.FromBase64String(sIV);

Console.WriteLine("Key:" + mCSP.Key.ToString() + ",IV:" + mCSP.IV.ToString());

//指定加密的运算模式

mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;

//获取或设置加密算法的填充模式

mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);

byt = Encoding.UTF8.GetBytes(Value);

ms = new MemoryStream();

cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);

cs.Write(byt, 0, byt.Length);

cs.FlushFinalBlock();

cs.Close();

return Convert.ToBase64String(ms.ToArray());

}

///

/// 解密

///

/// base64转码密文

/// 明文

public static string DecryptString(string Value)

{

SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();

ICryptoTransform ct;

MemoryStream ms;

CryptoStream cs;

byte[] byt;

mCSP.Key = Convert.FromBase64String(sKey);

mCSP.IV = Convert.FromBase64String(sIV);

mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;

mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);

byt = Convert.FromBase64String(Value);

ms = new MemoryStream();

cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);

cs.Write(byt, 0, byt.Length);

cs.FlushFinalBlock();

cs.Close();

return Encoding.UTF8.GetString(ms.ToArray());

}

}

下面是我的PHP加解密代码:

class DesCrypt {

/**

* 使用3DES加密源数据

* @param string $oriSource 源数据

* @param string $key 密钥

* @param string $defaultIV 加解密向量

* @return string $result 密文

*/

public function encryptByTDES($oriSource, $key, $defaultIV) {

$key = base64_decode ( $key ); //结果为24位

$iv = base64_decode ( $defaultIV ); //结果为8位

$oriSource = $this->addPKCS7Padding ( $oriSource );

$td = mcrypt_module_open ( MCRYPT_3DES, '', MCRYPT_MODE_CBC, '' );

mcrypt_generic_init ( $td, $key, $iv );

$result = mcrypt_generic ( $td, $oriSource );

mcrypt_generic_deinit ( $td );

mcrypt_module_close ( $td );

$result = base64_encode ( $result );

return $result;

}

/**

* 使用3DES解密密文

* @param string $encryptedData 密文

* @param string $key 密钥

* @param string $defaultIV 加解密向量

* @return string $result 解密后的原文

*/

public function decryptByTDES($encryptedData, $key, $defaultIV) {

$key = base64_decode ( $key ); //结果为24位

$iv = base64_decode ( $defaultIV ); //结果为8位

$encryptedData = base64_decode ( $encryptedData );

$td = mcrypt_module_open ( MCRYPT_3DES, '', MCRYPT_MODE_CBC, '' );

mcrypt_generic_init ( $td, $key, $iv );

$result = mdecrypt_generic ( $td, $encryptedData );

mcrypt_generic_deinit ( $td );

mcrypt_module_close ( $td );

$result = $this->stripPKSC7Padding ( $result );

return $result;

}

/**

* 为字符串添加PKCS7 Padding

* @param string $source 源字符串

*/

private function addPKCS7Padding($source) {

$block = mcrypt_get_block_size ( 'tripledes', 'cbc' );

$pad = $block - (strlen ( $source ) % $block);

if ($pad

$char = chr ( $pad );

$source .= str_repeat ( $char, $pad );

}

return $source;

}

/**

* 去除字符串末尾的PKCS7 Padding

* @param string $source 带有padding字符的字符串

*/

public function stripPKSC7Padding($source) {

$block = mcrypt_get_block_size ( 'tripledes', 'cbc' );

$char = substr ( $source, - 1, 1 );

$num = ord ( $char );

if ($num > 8) {

return $source;

}

$len = strlen ( $source );

for($i = $len - 1; $i >= $len - $num; $i --) {

if (ord ( substr ( $source, $i, 1 ) ) != $num) {

return $source;

}

}

$source = substr ( $source, 0, - $num );

return $source;

}

}

这两部分代码的加密结果不同,无法兼容,请问是什么问题啊?