public NSData Decrypt(NSData cipherData, NSData associatedData)
        {
            NSData result = _aead.Decrypt(cipherData, associatedData, out NSError error);

            if (error != null)
            {
                Debug.WriteLine(error.LocalizedFailureReason);
                return(null);
            }

            return(result);
        }
示例#2
0
        private void TestTink()
        {
            RegisterAead();

            // gen key
            NSError error;
            var     tpl = new TINKAeadKeyTemplate(TINKAeadKeyTemplates.Aes256Gcm, out error);

            if (error != null)
            {
                Console.WriteLine("Error: " + error);
                return;
            }
            var handle = new TINKKeysetHandle(tpl, out error);

            if (error != null)
            {
                Console.WriteLine("Error: " + error);
                return;
            }

            // store
            var keysetName = "co.tnn.tink.demo_key";

            if (!handle.WriteToKeychainWithName(keysetName, true, out error))
            {
                Console.WriteLine("Error: " + error);
                return;
            }

            // load
            TINKKeysetHandle handleStore = new TINKKeysetHandle(keysetName, out error);

            if (error != null)
            {
                Console.WriteLine("Error: " + error);
                return;
            }

            // AEAD
            ITINKAead aead = TINKAeadFactory.PrimitiveWithKeysetHandle(handleStore, out error);

            if (error != null)
            {
                Console.WriteLine("Error: " + error);
                return;
            }

            // encrypt
            NSData cipher = aead.Encrypt(NSData.FromString("hello world", NSStringEncoding.UTF8), NSData.FromString("k_value", NSStringEncoding.UTF8), out error);

            if (error != null)
            {
                Console.WriteLine("Error: " + error);
                return;
            }

            Console.WriteLine(cipher.GetBase64EncodedString(NSDataBase64EncodingOptions.None));

            // decrypt
            var plain = aead.Decrypt(cipher, NSData.FromString("k_value", NSStringEncoding.UTF8), out error);

            if (error != null)
            {
                Console.WriteLine("Error: " + error);
                return;
            }
            Console.WriteLine(plain.ToString(NSStringEncoding.UTF8));
        }