Exemple #1
0
 public bool DeleteKey(string keyAlias)
 {
     if (keyStyle == "asymmetric")
     {
         var helper = new AsymmetricKeyHelper(keyAlias);
         return(helper.DeleteKey());
     }
     else
     {
         var helper = new SymmetricKeyHelper(keyAlias);
         return(helper.DeleteKey());
     }
 }
Exemple #2
0
        public string DEBUGTEST()
        {
            // Test the possibility of encrypting using a Public key in the SecureStorage
            var output = "DEBUGTEST\n";

            // Make key pair
            var asymmHelper = new AsymmetricKeyHelper("DEBUGTEST");

            asymmHelper.CreateKey();
            var serializedCertificate = asymmHelper.GetCertificate().GetEncoded();

            var certificateEncrypter = new CertificateEncrypter("DEBUGTEST", serializedCertificate);
            var encryptedData        = certificateEncrypter.EncryptData("This is a quick little test here/n");

            // Run the externally encrypted data through internal decrypter
            output += asymmHelper.DecryptData(encryptedData);

            return(output);
        }
Exemple #3
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));
            }
        }
Exemple #4
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.StoredItems);


            AsymmetricKeyHelper ask1 = new AsymmetricKeyHelper("key1");

            ask1.CreateKey();
            string sharableKey = ask1.GetSharablePublicKey();

            AsymmetricKeyHelper ask2 = new AsymmetricKeyHelper("key2");
            string encryptedData     = ask2.EncryptWithAnotherPublicKey("This is a sym key", sharableKey);

            string decryptedData = ask1.DecryptDataFromString(encryptedData);



            //byte[] encryptedText = firstKey.EncryptDataWithAnotherPublicKey(encodedKey1, "TestValueGoesHere");

            //string decryptedText = firstKey.DecryptData(encryptedText);


            //SymmetricKeyHelper skh = new SymmetricKeyHelper("firstKey");
            //skh.CreateKey();
            //var encryptedText = skh.EncryptDataToBytes("Testing123");
            //var keyString = skh.GetKeyString();


            //SymmetricKeyHelper skh2 = new SymmetricKeyHelper("newKey");
            //skh2.LoadKey(keyString);
            //var decryptedText = skh2.DecryptData(encryptedText);



            //SymmetricKeyHelper skh = new SymmetricKeyHelper("firstKey");
            //skh.CreateKey();
            //var data = skh.EncryptData("Yeet");
            //var t = skh.GetKey();

            //string keyString = Convert.ToBase64String(t.GetEncoded());
            //byte[] convertedKey = Convert.FromBase64String(keyString);

            //SymmetricKeyHelper skh2 = new SymmetricKeyHelper("newKey");
            //skh2.LoadKey(convertedKey);
            //var l = skh2.DecryptData(data);


            //string encryptedStr = Convert.ToBase64String(data);
            //byte[] test = Convert.FromBase64String(encryptedStr);

            //var k = skh.DecryptData(test);

            // these are mutually exclusive
            //SetupPasswordBasedTesting();
            //SetupKeyCreationTesting();
            //SetupStoredItemTesting();

            Print(DEBUGTEST());
        }
Exemple #5
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";
        }