Ejemplo n.º 1
0
        private void SaveWallet(string path, string password)
        {
            var wallet = new JsonWallet
                         (
                EcdsaKeyPair.PrivateKey.ToHex(),
                HubPrivateKey.ToHex(),
                new Dictionary <ulong, string>(
                    _tpkeKeys.Select(p =>
                                     new System.Collections.Generic.KeyValuePair <ulong, string>(
                                         p.Key, p.Value.ToHex()
                                         )
                                     )
                    ),
                new Dictionary <ulong, string>(
                    _tsKeys.Select(p =>
                                   new System.Collections.Generic.KeyValuePair <ulong, string>(
                                       p.Key, p.Value.ToHex()
                                       )
                                   )
                    )
                         );
            var json             = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(wallet));
            var key              = Encoding.UTF8.GetBytes(password).KeccakBytes();
            var encryptedContent = Crypto.AesGcmEncrypt(key, json);

            File.WriteAllBytes(path, encryptedContent);
        }
Ejemplo n.º 2
0
        public void Test_AesGcmEncryptDecryptRoundTrip()
        {
            var key      = Crypto.GenerateRandomBytes(32);
            var baseText =
                "0xf86d808504a817c800832dc6c0948e7b7262e0fa4616566591d51f998f16a79fb547880de0b6b3a76400008025a0115105d96a43f41a5ea562bb3e591cbfa431a8cdae9c3030457adca2cb854f78a012fb41922c53c73473563003667ed8e783359c91d95b42301e1955d530b1ca33"
                .HexToBytes();
            const int n       = 1000;
            var       startTs = TimeUtils.CurrentTimeMillis();

            for (var it = 0; it < n; ++it)
            {
                var plaintext = baseText.Concat(it.ToBytes()).ToArray();
                var cipher    = Crypto.AesGcmEncrypt(key, plaintext);
                var decrypted = Crypto.AesGcmDecrypt(key, cipher);
                Assert.IsTrue(plaintext.SequenceEqual(decrypted));
            }

            var endTs = TimeUtils.CurrentTimeMillis();

            Console.WriteLine($"{n} encrypt/decrypt: {endTs - startTs}ms, avg = {(double) (endTs - startTs) / n}");
        }