Need to convert PHP Code to C# [Resolved]

Posted by Ganesh961991 under C# on 9/12/2013 | Points: 10 | Views : 33247 | Status : [Member] | Replies : 5
Hi All

$signature = rawurlencode(
base64_encode(
hash_hmac("sha1", $concatenatedString, $privateKey, true)
)
);

This is my PHP code i need to convert this part to c#. Anybody suggest me. Is there any online conversion tool available Or any equivalent c# code is also is much appreciable

Regards
Ganesh

Ganesh


Responses

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
1) rawurlencode equivalent in the C# is
HttpUtility.UrlEncodeUnicode( string str)


2) base64_encode() equivalent is as follows:
string str = "Hello";

byte[] encodedbytes = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
string encoded = System.Convert.ToBase64String(encodedbytes);


reference: http://onlinebase64decoder.com/functions.php
For Encoding and decoding of url:
UTF-7 is very unlikely to be what you want. You really need to know what encoding PHP is using. It may be using the default encoding for your system. Fortunately it's a lot easier to decode than you're making it:


public static string base64Decode(string data)
{
byte[] binary = Convert.FromBaseString(data);
return Encoding.Default.GetString(binary);
}There's no need to explicitly mess around with Encoder :)

Another possibility is that PHP is using ISO Latin 1, which is code page 28591:

public static string base64Decode(string data)
{
byte[] binary = Convert.FromBaseString(data);
return Encoding.GetEncoding(28591).GetString(binary);
}The PHP manual unhelpfully just says: "Before PHP 6, a character is the same as a byte. That is, there are exactly 256 different characters possible." Shame it doesn't say what each byte actually means...


3) for hash_hmac() equivalent in C# refer the following link
http://stackoverflow.com/questions/14109381/what-is-the-equivalent-this-code-in-c-sharp
http://www.slavasoft.com/quickhash/samples/vb_sample_hmac_sha1_string.htm

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Ganesh961991, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Hi Ganesh,
You are welcome..
Post the solution which you tweeted from the above link... It will help others..

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Ganesh961991, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ganesh961991 on: 9/13/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Chandu ,
http://stackoverflow.com/questions/14109381/what-is-the-equivalent-this-code-in-c-sharp
from the above link is i have got the code and its working fine. Thanks


Regards
Ganesh






Ganesh

Ganesh961991, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ganesh961991 on: 9/13/2013 [Member] Starter | Points: 25

Up
0
Down
Here is the solution
string sign = CreateToken(your contenateString, your PrivateKey);


private string CreateToken(string message, string secret)

{
secret = secret ?? "";
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacsha1= new HMACSHA1(keyByte,true))

{
byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
return Convert.ToBase64String(hashmessage);
}
}

That's it. MARK IT AS ANSWER

Ganesh

Ganesh961991, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tomblos on: 8/29/2015 [Member] Starter | Points: 25

Up
0
Down
I found different code examples on how to convert to base64 here. http://imagetobase64.com/code-examples Also you can convert images to base64 online.

Ganesh961991, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response