Beispiel #1
0
        static void Main(string[] args)
        {
            // our text to protect
            var text = "Hello!";

            // get bytes from text
            var bytes = Encoding.UTF8.GetBytes(text);

            // optional entropy
            var entropy = new byte[] { 100, 25, 31, 213 };

            // protect (encrypt)
            var protectedBytes = CrossProtect.Protect(bytes, entropy,
                                                      DataProtectionScope.CurrentUser);

            // unprotect (decrypt)
            var unprotected = CrossProtect.Unprotect(protectedBytes, entropy,
                                                     DataProtectionScope.CurrentUser);

            // convert bytes back to text
            var result = Encoding.UTF8.GetString(unprotected);

            // print result
            Console.WriteLine(result);
            Console.ReadKey();
        }
        public T Unprotect()
        {
            var data = CrossProtect.Unprotect(_encryptedData, _entropy, DataProtectionScope.CurrentUser);

            CheckReProtect(data);
            return(CreateUnprotected(data));
        }
        private void RunTrip(byte[] entropy, DataProtectionScope scope)
        {
            var encrypted   = CrossProtect.Protect(_sampleBytes, entropy, scope);
            var unencrypted = CrossProtect.Unprotect(encrypted, entropy, scope);
            var result      = Encoding.UTF8.GetString(unencrypted);

            Assert.Equal(SampleText, result);
        }
 private void Protect(byte[] data)
 {
     _entropy       = _random.GenerateRandomBytes(_random.NextInt(EntropyMaxLength - EntropyMinLength) + EntropyMinLength);
     _encryptedData = CrossProtect.Protect(data, _entropy, DataProtectionScope.CurrentUser);
     _timestamp     = _timestamper.UtcNow;
 }