如何将c#函数转换为php

时间:2011-02-11 12:30:38

标签: c# php mysql

如何将C#函数转换为PHP函数? C#代码如下:

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 个答案:

答案 0 :(得分:4)

我知道回答这个问题为时已晚。但我认为这对尝试将C#代码转换为PHP的人有所帮助。我最近有类似的要求

<?php

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

$password = mb_convert_encoding($password,'UTF-16LE');
echo base64_encode(sha1(base64_decode($salt).$password,true));

答案 1 :(得分:3)

我会说这是这样的:

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

答案 2 :(得分:1)

您可以使用PHP SHA1功能进行加密。