php .net 3des,PHP和C# 3DES加密

本文探讨了将PHP中的3DES加密函数转换为C#(ASP.NET)时遇到的问题,包括不同哈希处理、密钥编码、初始向量(IV)选择和填充模式。在C#代码中,使用了CBC模式和PKCS7填充,并提供了PHP和C#的加密函数对比。尽管尝试了各种调整,但加密结果仍然不一致。讨论了可能的解决策略,包括使用相同的加密模式、IV和密钥处理方式。
摘要由CSDN通过智能技术生成

Need to convert the following function from PHP to C# (asp.net)

function encrypt_3DES($message, $key){

// Se establece un IV por defecto

$bytes = array(0,0,0,0,0,0,0,0); //byte [] IV = {0, 0, 0, 0, 0, 0, 0, 0}

$iv = implode(array_map("chr", $bytes)); //PHP 4 >= 4.0.2

// Se cifra

$ciphertext = mcrypt_encrypt(MCRYPT_3DES, $key, $message, MCRYPT_MODE_CBC, $iv); //PHP 4 >= 4.0.2

return $ciphertext;

}

Where

$message is a string to encode and $key is the key

The $key is base 64 encoded and it is decoded before calling the function

$key = $this->decodeBase64($key);

$ciphertext = $this->encrypt_3DES($message, $key);

Following C# code I used:

key = Base64Decode(key);

ciphertext = encrypt_3DES(order, key,true);

where

private string Base64Decode(string base64EncodedData)

{

byte[] base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);

return Encoding.GetEncoding(28591).GetString(base64EncodedBytes);

// 28591 for php compatibility

}

and

private string encrypt_3DES(string message, string k,bool useHashing)

{

byte[] keyArray;

byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(message);

//If hashing use get hashcode regards to your key

if (useHashing)

{

MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();

keyArray = hashmd5.ComputeHash(Encoding.GetEncoding(28591).GetBytes(k));

//Always release the resources and flush data

// of the Cryptographic service provide. Best Practice

hashmd5.Clear();

}

else

keyArray = UTF8Encoding.GetEncoding(28591).GetBytes(k);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

//set the secret key for the tripleDES algorithm

tdes.Key = keyArray;

//mode of operation. there are other 4 modes.

//We choose ECB(Electronic code Book)

tdes.Mode = CipherMode.ECB;

//padding mode(if any extra byte added)

tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateEncryptor();

//transform the specified region of bytes array to resultArray

byte[] resultArray =

cTransform.TransformFinalBlock(toEncryptArray, 0,

toEncryptArray.Length);

//Release resources held by TripleDes Encryptor

tdes.Clear();

//Return the encrypted data into unreadable string format

return Convert.ToBase64String(resultArray, 0, resultArray.Length);

}

The results from PHP and C# are not the same.

Talk1:

There are some differences like the hashing, cipher-mode and cipher-padding in C#. Also, it seems your PHP code is incomplete considering how your C# code looks. Can you additionally provide some example message and key?

Talk2:

You should use CBC, an explicit IV and you should use a key consisting of bytes, not a string. If you use a string in the method parameter you're already lost.

Talk3:

B. Complete source code at the following link link

Talk4:

Bodewes this is an interface to a credit cart payment circuit. The key is provided as a string Base 64 encoded. The bank IT department does not provide a C# interface. So I'm trying to translate from PHP. Thanks for your suggestions.

Talk5:

Sorry, I meant that you should not use a string after base 64 decoding. After decoding you should use bytes. I.e. just base64EncodedBytes and byte[] k as parameter.

Solutions1

This is the function in C# would be the same as you've written in PHP

public static byte[] TripleDESEncrypt(string texto, byte[] key)

{

using (TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider())

{

byte[] iv_0 = { 0, 0, 0, 0, 0, 0, 0, 0 };

byte[] toEncryptArray = Encoding.ASCII.GetBytes(texto);

tdes.IV = iv_0;

//assign the secret key

tdes.Key = key;

tdes.Mode = CipherMode.CBC;

tdes.Padding = PaddingMode.Zeros;

ICryptoTransform cTransform = tdes.CreateEncryptor();

//transform the specified region of bytes array to resultArray

byte[] resultArray =

cTransform.TransformFinalBlock(toEncryptArray, 0,

toEncryptArray.Length);

//Clear to Best Practices

tdes.Clear();

return resultArray;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值