Пример #1
0
        // ----- KeyStore Interactions -----
        public string CreateKey(string alias, string message)
        {
            if (keyStyle == "asymmetric")
            {
                var helper = new AsymmetricKeyHelper(alias); helper.CreateKey();

                // If encrypted data is converted from byte[] to string, then back to byte[]
                // it does not come back the same, will not decrypt
                var encryptedData = helper.EncryptData(message);
                return(helper.DecryptData(encryptedData));
            }
            else
            {
                var helper = new SymmetricKeyHelper(alias); helper.CreateKey();

                // If encrypted data is converted from byte[] to string, then back to byte[]
                // it does not come back the same, will not decrypt
                var encryptedData = helper.EncryptDataToBytes(message);
                return(helper.DecryptData(encryptedData));
            }
        }
Пример #2
0
        // Show how to use Symm and Asymm Helpers (no longer very similar)
        //  Symm keys are in app-level SecureStorage now, Asymm keys are in os-level keystore
        public void SetupKeyCreationTesting()
        {
            // Grab the buttons
            var saveButton   = FindViewById <Button>(Resource.Id.saveButton);
            var getButton    = FindViewById <Button>(Resource.Id.getButton);
            var deleteButton = FindViewById <Button>(Resource.Id.deleteButton);
            // Renamed, get all can feed into delete all if desired
            var deleteAllButton = FindViewById <Button>(Resource.Id.deleteAllButton);

            deleteAllButton.Text = "Get All";

            // !!!
            // Put private key in symm cipher => No output
            // Put symm key in asymm cipher => Exception
            var symmButton  = FindViewById <RadioButton>(Resource.Id.SymmRadioButton);
            var asymmButton = FindViewById <RadioButton>(Resource.Id.AsymmRadioButton);

            // Janky add listeners to buttons
            saveButton.Click += (o, e) =>
            {
                var key  = FindViewById <EditText>(Resource.Id.storedKeyText).Text;
                var data = FindViewById <EditText>(Resource.Id.storedMessageText).Text;
                Print(CreateKey(key, data));
            };
            getButton.Click += (o, e) =>
            {
                var key  = FindViewById <EditText>(Resource.Id.storedKeyText).Text;
                var data = FindViewById <EditText>(Resource.Id.storedMessageText).Text;

                if (keyStyle == "asymmetric")
                {
                    var helper = new AsymmetricKeyHelper(key);

                    var encryptedData = helper.EncryptData(data);
                    Print(helper.DecryptData(encryptedData));
                }
                else
                {
                    var helper = new SymmetricKeyHelper(key);

                    var encryptedData = helper.EncryptDataToBytes(data);
                    Print(helper.DecryptData(encryptedData));
                }
            };

            deleteButton.Click += (o, e) =>
            {
                var key = FindViewById <EditText>(Resource.Id.storedKeyText).Text;
                Print(DeleteKey(key).ToString());
            };
            deleteAllButton.Click += (o, e) =>
            {
                var keyAliases = GetAllKeys();
                var output     = "Key aliases: \n";
                foreach (var alias in keyAliases)
                {
                    output += alias + "\n";
                }
                Print(output);
            };

            symmButton.Click += (o, e) =>
            {
                keyStyle = "symmetric";
            };
            asymmButton.Click += (o, e) =>
            {
                keyStyle = "asymmetric";
            };

            // Grab the text inputs
            var nameInput    = FindViewById <EditText>(Resource.Id.storedKeyText);
            var messageInput = FindViewById <EditText>(Resource.Id.storedMessageText);

            // Set prompts
            nameInput.Hint    = "Key alias";
            messageInput.Hint = "Message to be encrypted";
        }