Esempio n. 1
0
        /// <summary>
        /// Returns the plain text of the "secured" string value.
        /// </summary>
        /// <param name="value">The "secured" value to unscramble.</param>
        /// <returns>The plain text of the "secured" string value.</returns>
        public static string Unsecure(string value)
        {
            // get the bytes from a Base64 encoded string..
            byte[] scrambled = Convert.FromBase64String(value);

            // create a list of CharIntPair class instances to read the API key into..
            List <CharIntPair> pairs = new List <CharIntPair>();

            using (MemoryStream ms = new MemoryStream(scrambled)) // IDisposable, so using..
            {
                using (BinaryReader br = new BinaryReader(ms))    // IDisposable, so using..
                {
                    int len = br.ReadInt32();                     // read the length of randomized noise from the stream..
                    br.ReadBytes(len);                            // read the randomized noise from the stream..
                    len = br.ReadInt32();                         // read the amount of CharIntPair values stored in the stream..
                    for (int i = 0; i < len; i++)                 // go through the CharIntPair values in the stream..
                    {
                        // add the read pair to the list..
                        pairs.Add(new CharIntPair()
                        {
                            Position = br.ReadInt32(), Char = br.ReadChar()
                        });
                    }
                    // unscramble and return the "unsecured" string value..
                    return(CharIntPair.Unscramble(pairs));

                    // PS. the randomized end noise is not to be concerned about..
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// "Secures" the given API key.
        /// </summary>
        /// <param name="apiKey">The API key to "secure".</param>
        /// <param name="randomNoiseMin">The minimum value of the random noise (random characters) to be added to the return value.</param>
        /// <param name="randomNoiseMax">The maximum value of the random noise (random characters) to be added to the return value.</param>
        /// <returns></returns>
        public static string Secure(string apiKey, int randomNoiseMin = 30, int randomNoiseMax = 90)
        {
            // random characters to the beginning oh the string..
            byte[] randomNoise1 = Encoding.GetBytes(RandomString(random.Next(randomNoiseMin, randomNoiseMax)));

            // random characters to the end of the string..
            byte[] randomNoise2 = Encoding.GetBytes(RandomString(random.Next(randomNoiseMin, randomNoiseMax)));

            // the API key to scramble..
            byte[] apiKeyBytes = Encoding.GetBytes(apiKey);

            // scramble the API key..
            List <CharIntPair> scrambled = CharIntPair.Scramble(apiKey).ToList();

            byte[] scramble;                                   // the return value..
            using (MemoryStream ms = new MemoryStream())       // IDisposable, so using..
            {
                using (BinaryWriter bw = new BinaryWriter(ms)) // IDisposable, so using..
                {
                    bw.Write(randomNoise1.Length);             // write the randomized start string's length..
                    bw.Write(randomNoise1);                    // write the randomized start string..

                    // write the amount of scrambled API key characters and their positions..
                    bw.Write(scrambled.Count);

                    // write the scrambled API key characters and their positions..
                    foreach (CharIntPair pair in scrambled)
                    {
                        bw.Write(pair.Position);
                        bw.Write(pair.Char);
                    }

                    bw.Write(randomNoise2.Length); // write the randomized end string's length..
                    bw.Write(randomNoise2);        // write the randomized end string..
                }
                scramble = ms.ToArray();           // set the return value..
            }
            // return the value as Base64 encoded string..
            return(Convert.ToBase64String(scramble));
        }