Ejemplo n.º 1
0
        public byte[] GenerateBinaryKey()
        {
            const string originalLookupIndex = "SecureMemo";

            using var secureRnd = new SecureRandomGenerator();
            MemoryStream ms = new MemoryStream();

            byte[] buffer = secureRnd.GetRandomData(secureRnd.GetRandomInt(29, 221));
            ms.Write(buffer, 0, buffer.Length);

            buffer = GeneralConverters.ConvertStringToByteArray(Encoding.ASCII, originalLookupIndex);
            ms.Write(buffer, 0, buffer.Length);

            buffer = secureRnd.GetRandomData(secureRnd.GetRandomInt(29, 221));
            ms.Write(buffer, 0, buffer.Length);
            buffer = ms.ToArray();

            // ReSharper disable once SuggestVarOrType_Elsewhere

            for (int i = 0; i < secureRnd.GetRandomInt(83, 101); i++)
            {
                // Tick count being tracked by .Net using 32 bit integers for some strange reason when the WIN_API CALL uses ulong and thus not overflowing in 24 days and 20 hours.
                int tickCount = Environment.TickCount;

                // Just inject a small amount of noise for each iteration
                int pos = tickCount % buffer.Length;
                buffer[pos] ^= (byte)(tickCount % byte.MaxValue);
                buffer       = SHA512.GetSHA512HashAsByteArray(buffer);
            }

            buffer = SHA256.GetSHA256HashAsByteArray(buffer);
            return(buffer);
        }
Ejemplo n.º 2
0
        public string GenerateKey()
        {
            const string originalLookupIndex = "SecureMemo";
            string       key;

            using var secureRnd = new SecureRandomGenerator();
            string tmp = secureRnd.GetPasswordString(secureRnd.GetRandomInt(29, 221)) + originalLookupIndex + secureRnd.GetPasswordString(secureRnd.GetRandomInt(29, 221));

            // ReSharper disable once SuggestVarOrType_Elsewhere
            byte[] buffer = GeneralConverters.ConvertStringToByteArray(Encoding.ASCII, tmp);
            for (int i = 0; i < secureRnd.GetRandomInt(83, 101); i++)
            {
                // Tick count being tracked by .Net using 32 bit integers for some strange reason when the WIN_API CALL uses ulong and thus not overflowing in 24 days and 20 hours.
                int tickCount = Environment.TickCount;

                // Just inject a small amount of noise for each iteration
                int pos = tickCount % buffer.Length;
                buffer[pos] ^= (byte)(tickCount % byte.MaxValue);
                buffer       = SHA512.GetSHA512HashAsByteArray(buffer);
            }

            buffer = SHA256.GetSHA256HashAsByteArray(buffer);
            key    = Convert.ToBase64String(buffer, 0, buffer.Length, Base64FormattingOptions.None).Trim("=".ToCharArray());

            return(key);
        }
Ejemplo n.º 3
0
        public byte[] Encode(string password)
        {
            var secureRandom       = new SecureRandomGenerator();
            var msBlock            = new MemoryStream();
            var msContent          = new MemoryStream();
            int leftPaddingLength  = secureRandom.GetRandomInt(64, 512);
            int rightPaddingLength = secureRandom.GetRandomInt(64, 512);

            byte[] sharedSecretBytes = GeneralConverters.StringToByteArray(SharedSecret);

            byte[] buffer = BitConverter.GetBytes(leftPaddingLength);
            msBlock.Write(buffer, 0, buffer.Length);

            buffer = BitConverter.GetBytes(rightPaddingLength);
            msBlock.Write(buffer, 0, buffer.Length);

            buffer = BitConverter.GetBytes(leftPaddingLength + rightPaddingLength + sharedSecretBytes.Length);
            msBlock.Write(buffer, 0, buffer.Length);

            msBlock.Write(secureRandom.GetRandomData(leftPaddingLength), 0, leftPaddingLength);
            msBlock.Write(sharedSecretBytes, 0, sharedSecretBytes.Length);
            msBlock.Write(secureRandom.GetRandomData(rightPaddingLength), 0, rightPaddingLength);

            byte[] encodeBytes = msBlock.ToArray();

            encodeBytes = EncryptionManager.EncryptData(encodeBytes, password);
            byte[] hashBytes = SHA512.Create().ComputeHash(encodeBytes, 0, encodeBytes.Length);

            buffer = BitConverter.GetBytes(encodeBytes.Length);
            msContent.Write(buffer, 0, buffer.Length);

            msBlock.WriteTo(msContent);

            buffer = BitConverter.GetBytes(hashBytes.Length);
            msContent.Write(buffer, 0, buffer.Length);
            msContent.Write(hashBytes, 0, hashBytes.Length);

            return(msContent.ToArray());
        }