0

How do I convert a C# function to a PHP function? The C# code is below:

internal string EncodePassword(string password, string salt)
{
       // Get the Unicode bytes of the plain text password.
       byte[] bytes = System.Text.Encoding.Unicode.GetBytes(password);

       // The salt is a Base64 encoded string, convert back to a byte array.
       byte[] src = Convert.FromBase64String(salt);

       // Concat both byte buffers.
       byte[] dst = new byte[src.Length + bytes.Length];
       byte[] inArray = null;
       System.Buffer.BlockCopy(src, 0, dst, 0, src.Length);
       System.Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);

       // Compute the SHA1-hash from the concatenated buffer.
       System.Security.Cryptography.SHA1 algorithm = System.Security.Cryptography.SHA1Managed.Create();
       inArray = algorithm.ComputeHash(dst);

       // Return the result as a Base64-string.
       return Convert.ToBase64String(inArray);
}
3
  • Can you insert the code in a code block ? Commented Feb 11, 2011 at 12:33
  • 3
    I think your question is not How to convert? Your question is can you please convert it to php :)) Commented Feb 11, 2011 at 12:34
  • first you should know the password hashing functions of php Commented Feb 11, 2011 at 12:34

3 Answers 3

4

I know its too late to answer this question. But i think it will be helpful for someone who is trying to convert C# code to PHP. I had a similar kind of requirement recently

<?php

$password ='test123';
$salt = '5Br2nXb56EpliGXmfHdWWw==';

$password = mb_convert_encoding($password,'UTF-16LE');
echo base64_encode(sha1(base64_decode($salt).$password,true));
Sign up to request clarification or add additional context in comments.

Comments

3

I'd say it's something like this:

function encodePassword($password, $salt){
  return base64_encode(sha1($password . base64_decode($salt), true));
}

4 Comments

Possibly sha1(…, true). Otherwise I agree, it seems like that's all it's doing.
Yes, should be true as the second sha1()-parameter... (changed the answer accordingly).
Just for explanation: the C# SHA1 will be returned as raw bytes while PHP's SHA1 returns a hex string by default (true changes this behavior)
i have a salt value "5Br2nXb56EpliGXmfHdWWw==" and password "test123" and i have c# genrated password witch is using above salt and password the password is:- "YmwXST4800UkXas17GBEXex+mYQ=" can you do this in php thanks in advance :))
1

You can use the PHP SHA1 function to do the encryption.

1 Comment

YES I CAN BUT NOT GET THE RESULT ALSO PLEASE SEE THIS:- i have a salt value "5Br2nXb56EpliGXmfHdWWw==" and password "test123" and i have c# genrated password witch is using above salt and password the password is:- "YmwXST4800UkXas17GBEXex+mYQ=" can you do this in php thanks in advance :))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.