PHP Equivalent to C# GetBytesFromUTF8

本文关键字:GetBytesFromUTF8 to Equivalent PHP | 更新日期: 2023-09-27 17:59:31

我正在尝试创建一个php函数,该函数将允许我访问dotnet单点登录系统,但我一直在寻找一个相当于GetBytesFromUTF8的php,我尝试了ord和mb_string,但都没有成功。有没有关于等价于C#GetBytesFromUTF8的php的想法?

//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#过程,但没有成功。如何创建正确的单一签名代码:1.已为您提供了一个密钥(UTF-8)2.创建一个32个字符长的UTF-8随机短语a.创建32CharPhrase3.将关键字附加到短语中a.创建32CharPhrase+密钥4.从UTF-8字符串中获取字节值a.GetBytesFromUTF8(Create32CharPhrase+键)5.使用SHA512对您刚刚收到的字节值进行散列SHA512Hash(GetBytesFromUTF8(Create32CharPhrase+密钥))6.从您创建的哈希中获取Base64StringGetBase64String(SHA512Hash(GetBytesFromUTF8(Create32CharPhrase+Key))7.如果末尾有两个=符号,则将其移除。RemoveExtraEqualsSigns(GetBase64String(SHA512Hash)GetBytesFromUTF8(Create32CharPhrase+键))

函数的第二部分。。。

如何创建正确的单音提示:1.从步骤1中提取短语的前22个字符2.将从第一步中获取的22个字符附加到UserId字符串中3.(UserId)+(短语的前22个字符)4.将密钥附加到2b中创建的字符串(UserId)+(短语的前22个字符)+密钥5.获取字符串的Byte值GetBytesFromUTF8((UserId)+(短语的前22个字符)+键)6.使用SHA512对您刚刚收到的字节值进行散列SHA512Hash(GetBytesFromUTF8((UserId)+(短语的前22个字符)+密钥)7.从您创建的哈希中获取Base64String8.GetBase64String(SHA512Hash(GetBytesFromUTF8((UserId)+(短语的前22个字符)+键))9.如果末尾有两个=符号,则将其移除。RemoveExtraEqualsSigns(GetBase64String(SHA512Hash)GetBytesFromUTF8((UserId)+(前22短语的字符)+键)))

PHP Equivalent to C# GetBytesFromUTF8

从原始帖子的编辑中删除

ASP服务的PHP 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)];
        } 
        //echo "Key: ".$key."<br/>";
        //echo "Phrase: ".$randphrase."<br/>";
        //Append key onto phrase end
        $randkey=$randphrase.$key;
        // SHA512 Hash
        $toencode= utf8_encode($randkey);
        // Pass 3rd, optional parameter as TRUE to output raw binary data
        $output = hash("sha512", $toencode, true);
        //base 64 encode the hash binary data
        $sso = base64_encode($output);
        $length = mb_strlen($sso);
        $characters = 2;
        $start = $length - $characters;
        $last2 = substr($sso , $start ,$characters); 
        // Yes, Strip the extra ==
        if($last2 == "==")
                        {$ssocode = substr($sso,0,-2);}
        // No, just pass the value to the next step
        else{$ssocode=$sso;}
        // Prepend the random phrase to the encrypted code.
        $ssocode = $randphrase.$ssocode;
        //echo "SSO: ".$ssocode."<br/>";
        //Use first 22 charecters of random.
        $shortphrase=substr($randphrase,0,22);
        //Append uid & key onto shortened phrase end
        $uidv=$uid.$shortphrase.$key;
        // SHA512 Hash
        $idencode= utf8_encode($uidv);
        // Pass 3rd, optional parameter as TRUE to output raw binary data
        $idoutput = hash("sha512", $idencode, true);
        // Base64 Encode of hash binary data
        $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;}
        //echo "SSOID: ".$ssouidv;
        return array($ssocode, $ssouidv);
}