Exemplo n.º 1
0
        private static void Blake2Tests()
        {
            const string input = "ABCD";

            byte[] bytes = Encoding.UTF8.GetBytes(input);
            using ProtectedMemory protectedMemory = ProtectedMemory.Allocate(bytes.Length);
            protectedMemory.Write(bytes, 0);
            Blake2bProtectedCryptoProvider blake2 = new Blake2bProtectedCryptoProvider();

            Console.WriteLine(blake2.ComputeHash(protectedMemory));
        }
        public string Decrypt(ProtectedMemory key, string message)
        {
            Blake2bProtectedCryptoProvider blake2b = new Blake2bProtectedCryptoProvider();

            using ProtectedMemory paddedKey = blake2b.ComputeHashProtected(key, 64);
            ExtractHeader(message, paddedKey, out byte[] iv, out byte[] buffer);
            using (ProtectedMemory aesKey = DeriveAesKey(paddedKey))
            {
                AesState aesState = new AesState(aesKey, iv);
                aesState.AesCbcDecryptBuffer(ref buffer);
            }
            int contentLength = Pkcs7Padding.GetContentLength(buffer, 16);

            return(Encoding.UTF8.GetString(buffer, 0, contentLength));
        }
        private unsafe ProtectedMemory DeriveAesKey(ProtectedMemory key)
        {
            using ProtectedMemory protectedBuffer = ProtectedMemory.Allocate(64);
            key.CopyTo(0, protectedBuffer, 0, 64);
            using (ProtectedMemoryAccess bufferAccess = new ProtectedMemoryAccess(protectedBuffer))
            {
                ulong *buffer = (ulong *)bufferAccess.Handle;
                for (int i = 0; i < 64 / sizeof(ulong); i++)
                {
                    buffer[i] ^= 0x36;
                }
            }
            Blake2bProtectedCryptoProvider blake2b = new Blake2bProtectedCryptoProvider();

            return(blake2b.ComputeHashProtected(protectedBuffer, 32));
        }
        public ProtectedMemory DecryptProtected(ProtectedMemory key, string message)
        {
            Blake2bProtectedCryptoProvider blake2b = new Blake2bProtectedCryptoProvider();

            using ProtectedMemory paddedKey = blake2b.ComputeHashProtected(key, 64);
            ExtractHeader(message, paddedKey, out byte[] iv, out byte[] messageBytes);
            using ProtectedMemory protectedBuffer = ProtectedMemory.Allocate(messageBytes.Length);
            protectedBuffer.Write(messageBytes, 0, messageBytes.Length);
            using (ProtectedMemory aesKey = DeriveAesKey(paddedKey))
            {
                AesState aesState = new AesState(aesKey, iv);
                aesState.AesCbcDecryptBuffer(protectedBuffer);
            }
            int             contentLength = Pkcs7Padding.GetContentLength(protectedBuffer, 16);
            ProtectedMemory result        = ProtectedMemory.Allocate(contentLength);

            protectedBuffer.CopyTo(0, result, 0, contentLength);
            return(result);
        }
Exemplo n.º 5
0
        private static void Blake2PerfTest()
        {
            byte[] bytes = Encoding.UTF8.GetBytes("Lorem ipsum dolor sit amet, consectetur adipiscing elit.");
            using ProtectedMemory protectedMemory = ProtectedMemory.Allocate(bytes.Length);
            protectedMemory.Write(bytes, 0);
            Blake2bProtectedCryptoProvider blake2 = new Blake2bProtectedCryptoProvider();
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i < 1000000; i++)
            {
                _ = blake2.ComputeHash(protectedMemory);
            }
            stopwatch.Stop();
            Console.WriteLine("1000000 hashes done in " + stopwatch.Elapsed.ToString());
            double t = stopwatch.ElapsedMilliseconds / 1000000d;

            Console.WriteLine(" * " + t.ToString() + " ms per digest.");
            Console.WriteLine(" * " + (1000d / t).ToString() + " hashes per second.");
        }