Ejemplo n.º 1
0
        public async void DecryptEncryptedFileToNewFile()
        {
            //eg. decrypt encrypted file to a new file

            //XamEncDec.DecryptFileAsNewFile(<True=local directory,False=use custom path>,
            //<set string for custom path>, <folder name for output>, <encrypted filename>,
            //<password used for encryption>, <new filename for decrypted file>);
            var isFileDecrypted = await XamEncDec.DecryptFileAsNewFile(true, "", "folderName1", "MARBLES_ENCRYPTED.JPG", "myPassword", "MARBLES_DECRYPTED.JPG");
        }
        private async void DecryptFileToNewFile_Clicked(object sender, EventArgs e)
        {
            if (ent_filetofile_filename.Text != "" && ent_filetofile_filename.Text != null)
            {
                var file = await CrossFilePicker.Current.PickFile();

                try
                {
                    if (file != null)
                    {
                        await Navigation.PushModalAsync(popup);

                        await Task.Delay(5000);

                        StringBuilder stb        = new StringBuilder(file.FilePath);
                        int           postIndex  = IndexOf(stb, file.FileName, 0, true);
                        string        folderPath = stb.Remove(postIndex, file.FilePath.Length - postIndex).ToString();

                        //convert android content Uri into physical path, normally happens in Android emulator
                        folderPath = ConvertAndroidContentUriPath(folderPath);

                        bool isFileEncrypted = await XamEncDec.DecryptFileAsNewFile(false, folderPath, "", file.FileName, "myPassword", ent_filetofile_filename.Text);

                        if (isFileEncrypted)
                        {
                            await DisplayAlert("Decrypt", "File successfully decrypted", "OK");
                        }
                        else
                        {
                            await DisplayAlert("Error", "Decryption failed", "OK");
                        }
                        file.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    await DisplayAlert("Error", "Decryption failed, file is not a valid encrypted file", "OK");
                }

                await Navigation.PopModalAsync();
            }
            else
            {
                await DisplayAlert("Error", "Please set the filename", "OK");
            }
        }