PHP相当于C#GetBytesFromUTF8

时间:2011-06-16 15:24:28

标签: c# php

我正在尝试创建一个php函数,这将允许我访问一个dotnet单点登录系统,我挂断了找到一个相当于GetBytesFromUTF8的PHP,我已经尝试过ord和mb_string无济于事。 php的任何想法等同于C#GetBytesFromUTF8?

//Function to Create the SSO function SSO($key,$uid){ $lenth=32; $aZ09 = array_merge(range('A', 'Z'), range('a', 'z'),range(0, 9)); $randphrase ='';

        for($c=0;$c < $lenth;$c++) {
           $randphrase .= $aZ09[mt_rand(0,count($aZ09)-1)];
        } 
        //Append key onto phrase end
        $randkey=$randphrase.$key;
        //Number of Bytes is string  (THIS IS THE PROBLEM, ITS JUST ADDING THE STRING LENGTH)
        $bytevalue=mb_strlen($randkey, 'latin1');
        // SHA512 Hash
        //$toencode= utf8_encode($bytevalue);
        $output = hash("sha512", $bytevalue);
        //base 64 encode the hash
        $sso = base64_encode($output);
        $length = mb_strlen($sso);
        $characters = 2;
        $start = $length - $characters;
        $last2 = substr($sso , $start ,$characters); 
        //$startitup = APIClient::Create('http://my.staging.dosespot.com/LoginSingleSignOn.aspx','SingleSignOnCode=$ssocode');
        // Yes, Strip the extra ==
        if($last2 == "=="){$ssocode = substr($sso,0,-2);}
        // No, just pass the value to the next step
        else{$ssocode=$sso;}

        //Use first 22 charecters of random.
        $shortphrase=substr($randphrase,0,22);
        //Append uid & key onto shortened phrase end
        $uidv=$uid.$shortphrase.$key;
        //Number of Bytes is string
        $idbytevalue=mb_strlen($uidv, 'latin1');
        //$idbytevalue= strBytes(utf8_encode($uidv));
        // SHA512 Hash
        $idencode= utf8_encode($idbytevalue);
        $idoutput = hash("sha512", $idencode);
        // Base64 Encode of hash
        $idssoe = base64_encode($idoutput);
        //Determine if we need to strip the zeros
        $idlength = mb_strlen($idssoe);
        $idcharacters = 2;
        $idstart = $idlength - $idcharacters;
        $idlast2 = substr($idssoe , $idstart ,$idcharacters); 
        if($idlast2 == "=="){$ssouidv = substr($idssoe,0,-2);}
        // No, just pass the value to the next step
        else{$ssouidv=$idssoe;}
        return array($ssocode, $ssouidv);
        }

我试图复制这个c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DoseSpot.EncryptionLibrary
{
    public class EncodingUtility
    {
        public enum encodingOptions : int
        {
            ASCII = 0,
            UTF7,
            UTF8,
            UTF32,
            Unicode,
            Base64String
        }
    public static string GetString(byte[] data, encodingOptions eo)
    {
        switch (eo)
        {
            case encodingOptions.ASCII:
                return ToASCII(data);
            case encodingOptions.Unicode:
                return ToUnicode(data);
            case encodingOptions.Base64String:
                return ToBase64String(data);
            case encodingOptions.UTF7:
                return ToUTF7(data);
            case encodingOptions.UTF32:
                return ToUTF32(data);
            case encodingOptions.UTF8:
            default:
                return ToUTF8(data);
        }
    }
    public static byte[] GetBytes(string message, encodingOptions eo)
    {
        switch (eo)
        {
            case encodingOptions.ASCII:
                return FromASCII(message);
            case encodingOptions.Unicode:
                return FromUnicode(message);
            case encodingOptions.Base64String:
                return FromBase64String(message);
            case encodingOptions.UTF7:
                return FromUTF7(message);
            case encodingOptions.UTF32:
                return FromUTF32(message);
            case encodingOptions.UTF8:
            default:
                return FromUTF8(message);
        }
    }
    protected static string ToBase64String(byte[] data)
    {
        return Convert.ToBase64String(data);
    }
    protected static string ToUnicode(byte[] data)
    {
        return unicode.GetString(data);
    }
    protected static string ToASCII(byte[] data)
    {
        return ascii.GetString(data);
    }
    protected static string ToUTF7(byte[] data)
    {
        return utf7.GetString(data);
    }
    protected static string ToUTF8(byte[] data)
    {
        return utf8.GetString(data);
    }
    protected static string ToUTF32(byte[] data)
    {
        return utf32.GetString(data);
    }
    protected static byte[] FromBase64String(string originalString)
    {
        return Convert.FromBase64String(originalString);
    }
    protected static byte[] FromUnicode(string originalString)
    {
        return unicode.GetBytes(originalString);
    }
    protected static byte[] FromASCII(string originalString)
    {
        return ascii.GetBytes(originalString);
    }
    protected static byte[] FromUTF7(string originalString)
    {
        return utf7.GetBytes(originalString);
    }
    protected static byte[] FromUTF8(string originalString)
    {
        return utf8.GetBytes(originalString);
    }
    protected static byte[] FromUTF32(string originalString)
    {
        return utf32.GetBytes(originalString);
    }
    public static Encoding getEncoding(encodingOptions eo)
    {
        switch (eo)
        {
            case encodingOptions.ASCII:
                return ascii;
            case encodingOptions.UTF7:
                return utf7;
            case encodingOptions.UTF8:
                return utf8;
            case encodingOptions.UTF32:
                return utf32;
            case encodingOptions.Unicode:
            default:
                return unicode;
        }
    }
    private static ASCIIEncoding ascii = new ASCIIEncoding();
       private static UTF8Encoding utf8 = new UTF8Encoding();
       private static UTF7Encoding utf7 = new UTF7Encoding();
       private static UTF32Encoding utf32 = new UTF32Encoding();
        private static UnicodeEncoding unicode = new UnicodeEncoding();
    }

}

    public static class EncryptionCommon
{
    public static int KeyLength = 32;
    public static int PhraseLength = 32;
    public static string CreatePhrase()
    {
        return Randomizer.RandomNumberOfLettersAll(PhraseLength);
    }
    public static string CreateKey()
    {
        return Randomizer.RandomNumberOfLetters(KeyLength);
    }
    public static string Encrypt(string Phrase, string MyKey)
    {
        byte[] data = EncodingUtility.GetBytes(Phrase + MyKey, EncodingUtility.encodingOptions.UTF8);
        byte[] result = new SHA512Managed().ComputeHash(data);
        string tempString = EncodingUtility.GetString(result, EncodingUtility.encodingOptions.Base64String);
        if (tempString.Substring(tempString.Length - 2).ToString().Equals("=="))
            tempString = tempString.Substring(0, tempString.Length - 2);
        return tempString;
    }
    public static string EncryptUserId(string Phrase, int UserId, string MyKey)
    {
        string UserPhrase = UserId.ToString();
        if (Phrase.Length > 22)
            UserPhrase += Phrase.Substring(0, 22);
        else
            UserPhrase += Phrase;
        return Encrypt(UserPhrase, MyKey);
    }
    public static bool VerifyKey(string key, string combinedPhraseAndEncryptedString)
    {
        Dictionary<string, string> myDict = SplitStringIntoPhraseAndHash(combinedPhraseAndEncryptedString);
        string phrase = myDict["phrase"];
        string providedEncryptedPhrase = myDict["encryptedString"];
        string testEncryptedPhrase = Encrypt(phrase, key);
        if (providedEncryptedPhrase.Equals(testEncryptedPhrase))
            return true;
        else
            return false;
    }

    public static Dictionary<string, string> SplitStringIntoPhraseAndHash(string stringToSplit)
    {
        Dictionary<string, string> myResult = new Dictionary<string, string>();
        if (stringToSplit != null && stringToSplit.Trim().Length >= PhraseLength)
        {
            string phraseFound = stringToSplit.Substring(0, PhraseLength);
            string encryptedString = stringToSplit.Substring(PhraseLength);
            myResult.Add("phrase", phraseFound);
            myResult.Add("encryptedString", encryptedString);
        }
        return myResult;
    }

    public static string CreatePhraseEncryptedCombinedString(string phrase, string key)
    {
        string toReturn = phrase;
        toReturn += Encrypt(phrase, key);
        return toReturn;
    }
}

我试图在PHP中复制这个C#进程无济于事。 如何创建正确的SINGLESIGNONCODE: 1.您已获得一把钥匙(UTF-8) 2.在UTF-8中创建一个长度为32个字符的随机短语 一个。 Create32CharPhrase 3.将密钥附加到短语    一个。 Create32CharPhrase + Key 4.从UTF-8字符串中获取字节值    一个。 GetBytesFromUTF8(Create32CharPhrase + Key) 5.使用SHA512散列刚收到的字节值 SHA512Hash(GetBytesFromUTF8(Create32CharPhrase + Key)) 6.从您创建的哈希中获取Base64String GetBase64String(SHA512Hash(GetBytesFromUTF8(Create32CharPhrase + Key))) 7.如果最后有两个=符号,则将其删除。 RemoveExtraEqualsSigns(GetBase64String(SHA512Hash(GetBytesFromUTF8(Create32CharPhrase +键))))

该功能的第二部分......

如何创建正确的单身人士身份: 1.从第1步中获取该短语的前22个字符 2.在UserId字符串中附加从第一步中获取的22个字符 3.(UserId)+(短语的前22个字符) 4.将密钥附加到2b中创建的字符串 (UserId)+(短语的前22个字符)+键 5.获取字符串的字节值 GetBytesFromUTF8((UserId)+(短语的前22个字符)+键) 6.使用SHA512散列刚收到的字节值 SHA512Hash(GetBytesFromUTF8((UserId)+(短语的前22个字符)+键)) 7.从您创建的哈希中获取Base64String 8. GetBase64String(SHA512Hash(GetBytesFromUTF8((UserId)+(短语的前22个字符)+ 键))) 9.如果最后有两个=符号,则将其删除。 RemoveExtraEqualsSigns(GetBase64String(SHA512Hash(GetBytesFromUTF8((UserId)+(first 22) 短语的字符)+键))))

1 个答案:

答案 0 :(得分:0)

  

取自edit to the original post

用于ASP服务的PHP SSO

x = np.array([2,3,1,0,4,3,5,4,3,2,3,4,5,10,15,120,102,10])