GetCharCount() private method

private GetCharCount ( byte bytes, int count, bool flush ) : int
bytes byte
count int
flush bool
return int
    public static Highscore Load(Stream fs, System.Text.Decoder decoder)
    {
        const int length = 4;

        byte[] data = new byte[length];
        char[] chars;
        string name;

        fs.Read(data, 0, length);
        var bytesCount = System.BitConverter.ToInt32(data, 0);

        if (bytesCount > 0 && bytesCount < 100000)
        {
            data = new byte[bytesCount];
            fs.Read(data, 0, bytesCount);
            chars = new char[decoder.GetCharCount(data, 0, bytesCount)];
            decoder.GetChars(data, 0, bytesCount, chars, 0, true);
            name = new string(chars);
        }
        else
        {
            name = "highscore";
        }
        //
        bytesCount = 9;
        data       = new byte[bytesCount];
        fs.Read(data, 0, bytesCount);
        return(new Highscore(System.BitConverter.ToInt32(data, 0), name, System.BitConverter.ToUInt32(data, 4), (GameEndingType)data[8]));
    }
Example #2
0
    public static string base64Decode(string sData)
    {
        try
        {
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();

            System.Text.Decoder utf8Decode = encoder.GetDecoder();

            byte[] todecode_byte = Convert.FromBase64String(sData);

            int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

            char[] decoded_char = new char[charCount];

            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);

            string result = new String(decoded_char);

            return(result);
        }
        catch (Exception ex)
        {
            throw new Exception("Error in base64Decode" + ex.Message);
        }
    }
Example #3
0
    public void Load(System.IO.FileStream fs)
    {
        int LENGTH = 47;
        var data   = new byte[LENGTH];

        fs.Read(data, 0, LENGTH);
        ID = System.BitConverter.ToInt32(data, 0);

        atHome       = data[4] == 1;
        membersCount = data[5];
        if (data[6] == 2)
        {
            chanceMod = null;
        }
        else
        {
            chanceMod = data[6] == 1;
        }
        persistence     = data[7];
        survivalSkills  = data[8];
        secretKnowledge = data[9];
        perception      = data[10];
        intelligence    = data[11];
        techSkills      = data[12];
        freePoints      = data[13];
        level           = data[14];
        RecalculatePath();

        stamina      = System.BitConverter.ToSingle(data, 15);
        confidence   = System.BitConverter.ToSingle(data, 19);
        unity        = System.BitConverter.ToSingle(data, 23);
        loyalty      = System.BitConverter.ToSingle(data, 27);
        adaptability = System.BitConverter.ToSingle(data, 31);
        experience   = System.BitConverter.ToInt32(data, 35);

        missionsParticipated = System.BitConverter.ToUInt16(data, 39);
        missionsSuccessed    = System.BitConverter.ToUInt16(data, 41);

        int bytesCount = System.BitConverter.ToInt32(data, 43); //выдаст количество байтов, не длину строки

        if (bytesCount < 0 | bytesCount > 1000000)
        {
            Debug.Log("crew load error - name bytes count incorrect");
            GameMaster.LoadingFail();
            return;
        }
        if (bytesCount > 0)
        {
            data = new byte[bytesCount];
            fs.Read(data, 0, bytesCount);
            System.Text.Decoder d = System.Text.Encoding.Default.GetDecoder();
            var chars             = new char[d.GetCharCount(data, 0, bytesCount)];
            d.GetChars(data, 0, bytesCount, chars, 0, true);
            name = new string(chars);
        }
    }
    public static Highscore[] GetHighscores()
    {
        string path = Application.persistentDataPath + "/Highscores.lwhs";

        if (File.Exists(path))
        {
            FileStream fs    = File.Open(path, FileMode.Open);
            int        count = fs.ReadByte();
            if (count == 0)
            {
                fs.Close();
                return(null);
            }
            else
            {
                if (count > MAX_HIGHSCORES_COUNT)
                {
                    count = MAX_HIGHSCORES_COUNT;
                }
                var    hsa  = new Highscore[count];
                string name = "highscore";
                var    data = new byte[4];
                System.Text.Decoder decoder = System.Text.Encoding.Default.GetDecoder();
                char[] chars;
                int    bytesCount = 0;
                for (int i = 0; i < count; i++)
                {
                    fs.Read(data, 0, 4);
                    bytesCount = System.BitConverter.ToInt32(data, 0);
                    if (bytesCount > 0 && bytesCount < 100000)
                    {
                        data = new byte[bytesCount];
                        fs.Read(data, 0, bytesCount);
                        chars = new char[decoder.GetCharCount(data, 0, bytesCount)];
                        decoder.GetChars(data, 0, bytesCount, chars, 0, true);
                        name = new string(chars);
                    }
                    else
                    {
                        name = "highscore";
                    }
                    //
                    bytesCount = 9;
                    data       = new byte[bytesCount];
                    fs.Read(data, 0, bytesCount);
                    hsa[i] = new Highscore(name, System.BitConverter.ToUInt64(data, 0), (GameEndingType)data[bytesCount - 1]);
                }
                fs.Close();
                return(hsa);
            }
        }
        else
        {
            return(null);
        }
    }
Example #5
0
    public string base64Decode(string sData)
    {
        System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
        System.Text.Decoder      utf8Decode = encoder.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(sData);
        int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

        char[] decoded_char = new char[charCount];
        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        string result = new String(decoded_char); return(result);
    }
Example #6
0
    public static string Decrypt(this string str)
    {
        System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
        System.Text.Decoder      utf8Decode = encoder.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(str);
        int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

        char[] decoded_char = new char[charCount];
        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        return(new string(decoded_char));
    }
Example #7
0
    public string DecryptCode(string code)
    {
        code = code.Substring(0, code.Length - 1);
        System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
        System.Text.Decoder      utf8Decode = encoder.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(code);
        int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

        char[] decoded_char = new char[charCount];
        utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        code = new String(decoded_char);
        return(code);
    }
        //and Decoding:

        public string DecoAndGetServerName(string Servername)
        {
            System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
            System.Text.Decoder      strDecoder = encoder.GetDecoder();
            byte[] to_DecodeByte = Convert.FromBase64String(Servername);
            int    charCount     = strDecoder.GetCharCount(to_DecodeByte, 0, to_DecodeByte.Length);

            char[] decoded_char = new char[charCount];
            strDecoder.GetChars(to_DecodeByte, 0, to_DecodeByte.Length, decoded_char, 0);
            string Name = new string(decoded_char);

            return(Name);
        }
Example #9
0
        public string Decryptdata(string encryptpwd)
        {
            string       decryptpwd = string.Empty;
            UTF8Encoding encodepwd  = new UTF8Encoding();

            System.Text.Decoder utf8Decode = encodepwd.GetDecoder();
            byte[] todecode_byte           = Convert.FromBase64String(encryptpwd);
            int    charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            decryptpwd = new String(decoded_char);
            return(decryptpwd);
        }
Example #10
0
        public string GetString()
        {
            StringBuilder builder = new StringBuilder();

            System.Text.Decoder decoder = Encoding.UTF8.GetDecoder();
            for (int i = 0; i < this._segments.Count; i++)
            {
                bool   flush     = i == (this._segments.Count - 1);
                byte[] bytes     = this._segments[i];
                char[] chars     = new char[decoder.GetCharCount(bytes, 0, bytes.Length, flush)];
                int    charCount = decoder.GetChars(bytes, 0, bytes.Length, chars, 0, flush);
                builder.Append(chars, 0, charCount);
            }
            return(builder.ToString());
        }
        public static string Decrypt(string password)
        {
            //----Decription For Password Logic Start
            System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
            System.Text.Decoder      utf8Decode = encoder.GetDecoder();
            byte[] todecode_byte = Convert.FromBase64String(password);
            int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            string pass = new String(decoded_char);

            //----End Decription For Password Logic
            return(pass);
        }
        protected static string GetDataAsString(byte[] data, int ignoreLastBytes)
        {
            if (data.Length < ignoreLastBytes)
            {
                return("");
            }

            System.Text.Decoder utf8Decoder = System.Text.Encoding.UTF8.GetDecoder();
            int charCount = utf8Decoder.GetCharCount(data, 0, (data.Length - ignoreLastBytes));

            char[] recievedChars = new char[charCount];
            utf8Decoder.GetChars(data, 0, data.Length - ignoreLastBytes, recievedChars, 0);
            String recievedString = new String(recievedChars);

            return(recievedString);
        }
Example #13
0
    public static void LoadStaticData(System.IO.Stream fs)
    {
        if (artifactsList == null)
        {
            artifactsList = new List <Artifact>();
        }
        else
        {
            artifactsList.Clear();
        }
        var data = new byte[4];

        fs.Read(data, 0, 4);
        int artsCount = System.BitConverter.ToInt32(data, 0);

        int LENGTH = 23;

        while (artsCount > 0)
        {
            data = new byte[LENGTH];
            fs.Read(data, 0, LENGTH);
            var a = new Artifact(
                System.BitConverter.ToSingle(data, 0), // stability
                System.BitConverter.ToSingle(data, 4), //saturation
                System.BitConverter.ToSingle(data, 8), //frequency
                (Path)data[12]
                );
            a.ID         = System.BitConverter.ToInt32(data, 13);
            a.researched = data[17] == 1;
            a.status     = (ArtifactStatus)data[18];
            int bytesCount = System.BitConverter.ToInt32(data, 19); //выдаст количество байтов, не длину строки
            if (bytesCount > 0)
            {
                data = new byte[bytesCount];
                fs.Read(data, 0, bytesCount);
                System.Text.Decoder d = System.Text.Encoding.Default.GetDecoder();
                var chars             = new char[d.GetCharCount(data, 0, bytesCount)];
                d.GetChars(data, 0, bytesCount, chars, 0, true);
                a.name = new string(chars);
            }
            artsCount++;
        }

        fs.Read(data, 0, 4);
        nextID = System.BitConverter.ToInt32(data, 0);
    }
Example #14
0
        public string Decrypt(string cipherString)
        {
            //byte[] keyArray;
            //byte[] toEncryptArray = Convert.FromBase64String(cipherString);

            ////System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
            ////Get your key from config file to open the lock!
            //string key = "dinoosys@!@#";

            //if (useHashing)
            //{
            //    MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            //    keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            //    hashmd5.Clear();
            //}
            //else
            //    keyArray = UTF8Encoding.UTF8.GetBytes(key);

            //TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            //tdes.Key = keyArray;
            //tdes.Mode = CipherMode.ECB;
            //tdes.Padding = PaddingMode.PKCS7;

            //ICryptoTransform cTransform = tdes.CreateDecryptor();
            //byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            //tdes.Clear();
            //return UTF8Encoding.UTF8.GetString(resultArray);
            string ResultString = string.Empty;

            try
            {
                System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
                System.Text.Decoder      utf8Decode = encoder.GetDecoder();
                byte[] todecode_byte = Convert.FromBase64String(cipherString);
                int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
                char[] decoded_char  = new char[charCount];
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                ResultString = new String(decoded_char);
            }
            catch (Exception ex)
            {
                throw new Exception("invalid result.");
            }
            return(ResultString);
        }
Example #15
0
 /// <summary>
 /// Request 값 xml 객체로 추출
 /// </summary>
 /// <returns>XmlDocument</returns>
 private XmlDocument pParseRequestBytes()
 {
     try
     {
         XmlDocument         oXMLData = new XmlDocument();
         System.Text.Decoder oDecoder = System.Text.Encoding.UTF8.GetDecoder();
         System.Byte[]       aBytes   = Request.BinaryRead(Request.TotalBytes);
         long          iCount         = oDecoder.GetCharCount(aBytes, 0, aBytes.Length);
         System.Char[] aChars         = new char[iCount];
         oDecoder.GetChars(aBytes, 0, aBytes.Length, aChars, 0);
         oXMLData.Load(new System.IO.StringReader(new String(aChars)));
         return(oXMLData);
     }
     catch (Exception e)
     {
         throw new Exception(null, e);
     }
 }
Example #16
0
        public static string DecodeFromBase64(this string encodedData)
        {
            if (string.IsNullOrEmpty(encodedData))
            {
                return(null);
            }
            UTF8Encoding encoder = new UTF8Encoding();

            System.Text.Decoder utf8Decode = encoder.GetDecoder();
            byte[] todecode_byte           = Convert.FromBase64String(encodedData);
            int    charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            string result = new String(decoded_char);

            return(result);
        }
        // GetStringFromByteArray metodu byte dizisindeki belli uzunluktaki
        // bir parçayı alarak, string olarak döndürür.
        //
        // bytes:  Byte dizisi.
        // iStart: Byte dizisindeki başlangıç konumu.
        // iLen:   Çevrilecek  byte parçasının uzunluğu.
        // dec:    Çevirmede kullanılacak Decoder.
        public static string GetStringFromByteArray(Byte[] bytes, int iStart,
            int iLen, Decoder dec)
        {
            // Parametrelerin hata kontrolünü yap.
            if ((iStart + iLen <= bytes.Length) && (iStart >= 0) && (iLen > 0))
            {
                // Karakter dizisi nesnesi oluştur.
                Char[] chars;
                // StringBuilder nesnesi oluştur.
                StringBuilder sb = new StringBuilder();

                // GetCharCount metoduyla, byte dizisindeki kodlaması çözülecek
                // olan byte'lar için gerekli karakter sayısını hesapla.
                int iCharCount = dec.GetCharCount(bytes, iStart, iLen);
                // Karakter dizisini boyutlandır.
                chars = new Char[iCharCount];
                // GetChars metoduyla, belirtilen sayı kadar, byte dizisinden
                // alınan elemanların karakter dizisine çevrilmesi:
                dec.GetChars(bytes, iStart, iLen, chars, 0);

                // must skip null-termination of C++ string
                // Karakter dizisinin sonundaki null kontrolü için:
                for (int i = 0; i < iCharCount; ++i)
                {
                    // Konrol karakteri var mı?
                    if (Char.GetUnicodeCategory(chars[i]) != UnicodeCategory.Control)
                    {
                        // Karakter dizini doldur.
                        sb.Append(chars[i].ToString());
                    }
                    else
                    {
                        break;      // Konrol karakteri yakalandı.
                    }//if
                }//for

                return sb.ToString();
            }
            else
            {
                // Parametrelerde hata bulundu. Boş bir string çevir.
                return String.Empty;
            }
        }
Example #18
0
        /// <summary>
        /// Decodes a base64 string
        /// </summary>
        /// <param name="data">string that will be converted</param>
        /// <returns>true if the conversion succeeded or false if it didnt</returns>
        public static bool Base64StringDecode(string data)
        {
            try
            {
                System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
                System.Text.Decoder      utf8Decode = encoder.GetDecoder();

                byte[] todecode_byte = System.Convert.FromBase64String(data);
                int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
                char[] decoded_char  = new char[charCount];
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                data = new String(decoded_char);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Example #19
0
            public static string Decrypt(string cipherText, string passPhrasee = "ASD")
            {
                var rand = cipherText.Substring(0, 6);


                string passPhrase = passPhrasee;


                System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
                System.Text.Decoder      utf8Decode = encoder.GetDecoder();
                byte[] todecode_byte = Convert.FromBase64String(cipherText.Decode());
                int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

                char[] decoded_char = new char[charCount];
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                string result = new String(decoded_char);

                return(result);
            }
        public string decryptPassword(string sData)
        {
            System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
            System.Text.Decoder      utf8Decode = encoder.GetDecoder();
            int mod4 = sData.Length % 4;

            if (mod4 > 0)
            {
                sData += new string('=', 4 - mod4);
            }
            byte[] todecode_byte = Convert.FromBase64String(sData);
            int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            string result = new String(decoded_char);

            return(result);
        }
Example #21
0
        private static string base64Decode(string sData)
        {
            try
            {
                System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
                System.Text.Decoder      utf8Decode = encoder.GetDecoder();
                byte[] todecode_byte = Convert.FromBase64String(sData);
                int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
                char[] decoded_char  = new char[charCount];
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                string result = new String(decoded_char);

                return(result);
            }
            catch
            {
                throw new InvalidCastException("Error decoding DB password");
            }
        }
Example #22
0
 public static string Base64Decode(string data)
 {
     try
     {
         System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
         System.Text.Decoder      utf8Decode = encoder.GetDecoder();
         data = data.Replace("-", "+").Replace("_", "/");
         byte[] todecode_byte = Convert.FromBase64String(data);
         int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
         char[] decoded_char  = new char[charCount];
         utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
         string result = new String(decoded_char);
         return(result);
     }
     catch (Exception e)
     {
         return("");
     }
 }
        public static String Decode(String data)
        {
            try
            {
                System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
                System.Text.Decoder      utf8Decode = encoder.GetDecoder();

                Byte[] toDecodeByte = Convert.FromBase64String(data);
                Int32  charCount    = utf8Decode.GetCharCount(toDecodeByte, 0, toDecodeByte.Length);
                Char[] decodedChar  = new Char[charCount];
                utf8Decode.GetChars(toDecodeByte, 0, toDecodeByte.Length, decodedChar, 0);
                String result = new String(decodedChar);
                return(result);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #24
0
        public string base64Decode(string sData)
        {
            System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
            System.Text.Decoder      utf8Decode = encoder.GetDecoder();
            try
            {
                byte[] todecode_byte = Convert.FromBase64String(sData);

                int    charCount    = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
                char[] decoded_char = new char[charCount];
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                string result = new String(decoded_char);
                return(result);
            }
            catch (Exception err)
            {
                //Response.Redirect("~/error.aspx");
                return("");
            }
        }
 protected string decodepwd(string depas)
 {
     try
     {
         System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
         System.Text.Decoder      utf8Decode = encoder.GetDecoder();
         byte[] todecode_byte = Convert.FromBase64String(depas);
         int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
         char[] decoded_char  = new char[charCount];
         utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
         string result = new String(decoded_char);
         return(result);
     }
     catch (Exception ex)
     {
         CreateLogFile log = new CreateLogFile();
         log.ErrorLog(Server.MapPath("../Logs/Errorlog"), "Decode method of AlumniProfileHome page for " + Session["loginname"] + ":" + ex.Message);
         return("conversion error");
     }
 }
Example #26
0
        //public static string profileidEncrypt(string clearText)
        //{
        //    string EncryptionKey = "MAKV2SPBNI99212";
        //    byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
        //    using (Aes encryptor = Aes.Create())
        //    {
        //        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        //        encryptor.Key = pdb.GetBytes(32);
        //        encryptor.IV = pdb.GetBytes(16);

        //        using (MemoryStream ms = new MemoryStream())
        //        {
        //            using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
        //            {
        //                cs.Write(clearBytes, 0, clearBytes.Length);
        //                cs.Close();
        //            }
        //            clearText = Convert.ToBase64String(ms.ToArray());
        //        }
        //    }

        //    return clearText;
        //}

        public static string base64Decode(string data)
        {
            try
            {
                data = data.Replace("100", "=").Replace("200", "+").Replace("300", "#").Replace("400", "!").Replace("500", ";").Replace("600", "'").Replace("700", "/").Replace("800", "\\");
                System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
                System.Text.Decoder      utf8Decode = encoder.GetDecoder();

                byte[] todecode_byte = Convert.FromBase64String(data);
                int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
                char[] decoded_char  = new char[charCount];
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                string result = new String(decoded_char);
                return(result);
            }
            catch (Exception e)
            {
                throw new Exception("Error in base64Decode" + e.Message);
            }
        }
Example #27
0
 public static string ReadMinimum(this Stream stream, Decoder decoder)
 {
     var buffer = new byte[16];
     for (int i = 0; i < buffer.Length; ++i)
     {
         var read = stream.Read(buffer, i, 1);
         if (read == 0)
         {
             if (i != 0) throw new Exception("invalid encoded text?");
             return null;
         }
         var charCount = decoder.GetCharCount(buffer, 0, i + 1);
         if (charCount != 0)
         {
             var chars = new char[charCount];
             decoder.GetChars(buffer, 0, i + 1, chars, 0);
             return new string(chars);
         }
     }
     throw new Exception("impossible?");
 }
Example #28
0
        public string base64Decode(string data)
        {
            //siiani on töötanud
            try
            {
                System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
                System.Text.Decoder      utf8Decode = encoder.GetDecoder();

                byte[] todecode_byte = Convert.FromBase64String(data);
                int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
                char[] decoded_char  = new char[charCount];
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                string result = new String(decoded_char);
                return(result);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                throw new Exception("Error in base64Decode" + e.Message);
            }
        }
Example #29
0
        public string Decrypt(string asEncryptedString)
        {
            string result = null;

            try
            {
                System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
                System.Text.Decoder      utf8Decode = encoder.GetDecoder();
                byte[] todecode_byte = Convert.FromBase64String(asEncryptedString);
                int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
                char[] decoded_char  = new char[charCount];
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                result = new String(decoded_char);
            }
            catch (Exception Ex)
            {
                Logger.WriteError(Ex, "Decrypt", "User");
                throw Ex;
            }
            return(result);
        }
Example #30
0
        /// <summary>
        /// 解密。
        /// </summary>
        internal static string Decrypt(string pToDecrypt)
        {
            String Result = "";

            if (String.IsNullOrEmpty(pToDecrypt))
            {
                return(Result);
            }
            try {
                System.Text.UTF8Encoding encoder    = new System.Text.UTF8Encoding();
                System.Text.Decoder      utf8Decode = encoder.GetDecoder();
                byte[] todecode_byte = Convert.FromBase64String(pToDecrypt);
                int    charCount     = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
                char[] decoded_char  = new char[charCount];
                utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
                Result = new String(decoded_char);
                return(Result);
            }
            catch {
                //throw new Exception("Error in base64Decode" + e.Message);
                return("");
            }

            /*DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
             * byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
             * for (int x = 0; x < pToDecrypt.Length / 2; x++) {
             *      int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
             *      inputByteArray[x] = (byte)i;
             * }
             * provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
             * provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
             * MemoryStream ms = new MemoryStream();
             * CryptoStream cs = new CryptoStream(ms, provider.CreateDecryptor(), CryptoStreamMode.Write);
             * cs.Write(inputByteArray, 0, inputByteArray.Length);
             * cs.FlushFinalBlock();
             * Result= Encoding.Default.GetString(ms.ToArray());
             * cs.Close();
             * return Result;*/
        }
Example #31
0
        public static string base64Decode(string sData) //Decode
        {
            try
            {
                var encoder = new System.Text.UTF8Encoding();
                System.Text.Decoder utf8Decode = encoder.GetDecoder();
                byte[] todecodeByte            = Convert.FromBase64String(sData);
                int    charCount   = utf8Decode.GetCharCount(todecodeByte, 0, todecodeByte.Length);
                char[] decodedChar = new char[charCount];
                utf8Decode.GetChars(todecodeByte, 0, todecodeByte.Length, decodedChar, 0);
                string result = new String(decodedChar);
                return(result);
            }

            /*string EncryptionKey = "MAKV2SPBNI99212";
             * byte[] cipherBytes = Convert.FromBase64String(cipherText);
             * using (Aes encryptor = Aes.Create())
             * {
             *  Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
             *  encryptor.Key = pdb.GetBytes(32);
             *  encryptor.IV = pdb.GetBytes(16);
             *  using (MemoryStream ms = new MemoryStream())
             *  {
             *      using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
             *      {
             *          cs.Write(cipherBytes, 0, cipherBytes.Length);
             *          cs.Close();
             *      }
             *      cipherText = Encoding.Unicode.GetString(ms.ToArray());
             *  }
             * }
             * return cipherText;
             * //  }*/
            catch (Exception ex)
            {
                throw new Exception("Error in base64Decode" + ex.Message);
            }
        }
Example #32
0
        public string SetDecoding(string strText)
        {
            string strReturn = null;

            do
            {
                try {
                    System.Text.UTF8Encoding objEncoding = new System.Text.UTF8Encoding();
                    System.Text.Decoder      objDecoder  = objEncoding.GetDecoder();

                    byte[] bDecode    = Convert.FromBase64String(strText);
                    int    iCount     = objDecoder.GetCharCount(bDecode, 0, bDecode.Length);
                    char[] charDecode = new char[iCount];
                    objDecoder.GetChars(bDecode, 0, bDecode.Length, charDecode, 0);
                    strReturn = new string( charDecode );
                }
                catch (Exception ex) {
                    Trace.WriteLine(ex.Message);
                }
            } while(false);

            return(strReturn);
        }
Example #33
0
		private int GetCharsFromStream(Stream inputStream, int count, Decoder decoder, char[] chars)
		{
			// Now, read just the field value.
			PGUtil.CheckedStreamRead(inputStream, _inputBuffer, 0, count);
			int charCount = decoder.GetCharCount(_inputBuffer, 0, count);
			decoder.GetChars(_inputBuffer, 0, count, chars, 0);
			return charCount;
		}