Windows Phone 7: Decrypting many files from isolated storage - c#

I'm trying do to a program which stores many (5-15) .txt files in phone's isolated storage memory. I noticed how easy it is to read those files with programs like Windows Phone Power Tools so I decided to to encrypt them. I'm using this link as a tutorial:
http://msdn.microsoft.com/en-us/library/windows/apps/hh487164(v=vs.105).aspx
Encryption works fine, as I'm obviously saving one file at a time. However, I'm having problems while trying to decrypt them. How should i edit my code so I can decrypt many .txt files? Below are my codes that I'm using at the moment:
private void IsoRead()
{
System.IO.IsolatedStorage.IsolatedStorageFile local =
System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
string[] filenames = local.GetFileNames("./DataFolder/*.txt*");
foreach (var fname in filenames)
{
//retrieve byte
byte[] ProtectedByte = this.DecryptByte();
//decrypt with Unprotect method
byte[] FromByte = ProtectedData.Unprotect(ProtectedByte, null);
//convert from byte to string
fText = Encoding.UTF8.GetString(FromByte, 0, FromByte.Length);
this.Items.Add(new itemView() { LineOne = fname, LineTwo = fText });
}
}
And the other one:
private byte[] DecryptByte()
{
// Access the file in the application's isolated storage.
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream readstream = new IsolatedStorageFileStream
("DataFolder\\"/*Here's where I'm having problems with*/, System.IO.FileMode.Open, FileAccess.Read, file);
// Read the written data from the file.
Stream reader = new StreamReader(readstream).BaseStream;
byte[] dataArray = new byte[reader.Length];
reader.Read(dataArray, 0, dataArray.Length);
return dataArray;
}
So basically the program has a listview page that get's the files from isolated storage. If one is touched, it goes to a new page that shows what's written in it.
Bonus question: Can I encrypt folders in WP7/WP8?
Edit: added one code line into IsoRead.

Xaml:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<Button Content="Write Random File" Click="WriteFile" VerticalAlignment="Top" />
<Button Content="Read files File" Click="ReadFiles" VerticalAlignment="Top" />
</StackPanel>
</Grid>
Code:
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private const string FilePath = "{0}.txt";
private readonly List<ItemView> items = new List<ItemView>();
private void WriteFile(object sender, RoutedEventArgs e)
{
var fileName = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
// Convert text to bytes.
byte[] data = Encoding.UTF8.GetBytes(fileName);
// Encrypt byutes.
byte[] protectedBytes = ProtectedData.Protect(data, null);
// Store the encrypted bytes in iso storage.
this.WriteToFile(protectedBytes, fileName);
}
private void ReadFiles(object sender, RoutedEventArgs e)
{
items.Clear();
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
var files = isoStore.GetFileNames("*.txt");
foreach (var file in files)
{
// Retrieve the protected bytes from isolated storage.
byte[] protectedBytes = this.ReadBytesFromFile(file);
// Decrypt the protected bytes by using the Unprotect method.
byte[] bytes = ProtectedData.Unprotect(protectedBytes, null);
// Convert the data from byte to string and display it in the text box.
items.Add(new ItemView { LineOne = file, LineTwo = Encoding.UTF8.GetString(bytes, 0, bytes.Length) });
}
}
//Show all the data...
MessageBox.Show(string.Join(",", items.Select(i => i.LineTwo)));
}
private void WriteToFile(byte[] bytes, string fileName)
{
// Create a file in the application's isolated storage.
using (var file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var writestream = new IsolatedStorageFileStream(string.Format(FilePath, fileName), System.IO.FileMode.Create, System.IO.FileAccess.Write, file))
{
// Write data to the file.
using (var writer = new StreamWriter(writestream).BaseStream)
{
writer.Write(bytes, 0, bytes.Length);
}
}
}
}
private byte[] ReadBytesFromFile(string filePath)
{
// Access the file in the application's isolated storage.
using (var file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var readstream = new IsolatedStorageFileStream(filePath, System.IO.FileMode.Open, FileAccess.Read, file))
{
// Read the data in the file.
using (var reader = new StreamReader(readstream).BaseStream)
{
var ProtectedPinByte = new byte[reader.Length];
reader.Read(ProtectedPinByte, 0, ProtectedPinByte.Length);
return ProtectedPinByte;
}
}
}
}
}
public class ItemView
{
public string LineOne { get; set; }
public string LineTwo { get; set; }
}

Related

Download multiple files .NET CORE Web API as ArchiveZip return root directory

I make a private class to get the bytes of the file and then return it into a zip file. Basically, I just want to pick specific files inside my folder and then zip it then download it. Here's my class:
private FileResult DownloadMultipleFiles(List<byte[]> byteArrayList)
{
var zipName = $"archive-EvidenceFiles-{DateTime.Now.ToString("yyyy_MM_dd-HH_mm_ss")}.zip";
using (MemoryStream ms = new MemoryStream())
{
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
foreach (var file in byteArrayList)
{
string fPath = Encoding.ASCII.GetString(file);
var entry = archive.CreateEntry(fPath, CompressionLevel.Fastest);
using (var zipStream = entry.Open())
{
zipStream.Write(file, 0, file.Length);
}
}
}
return File(ms.ToArray(), "application/zip", zipName);
}
}
And then, here's my controller:
[HttpGet("GetBundleFiles/{rhaId}")]
public async Task<IActionResult> GetBundleFiles(string rhaId)
{
List<byte[]> filesPath = new List<byte[]>();
var results = await _rhaFileEvidence.GetByRhaID(rhaId);
var files = results.ToList();
if (files.Count == 0)
return Ok(new { status = "null", message = "Empty data" });
files.ForEach(file =>
{
var fPath = file.FilePath;
byte[] bytes = Encoding.ASCII.GetBytes(fPath);
filesPath.Add(bytes);
});
return DownloadMultipleFiles(filesPath);
}
The controller works well, I can download the zip but when I open it, I can't get the files instead I get the root directory of the project I saved, like D:. I think I make mistake when making the memory stream or something, is there any suggestion how can I fix this? (Paste some solution code in the answer please)
you are getting the directory root because when you use archive.CreateEntry you are passing the file full path in parameter, you should be using only the file name
var entry = archive.CreateEntry(System.IO.Path.GetFileName(fPath), CompressionLevel.Fastest);
a second issue is that you actually saving the file path to your files not the content of the original file. you can update your DownloadMultipleFiles like this
private FileResult DownloadMultipleFiles(List<byte[]> byteArrayList)
{
var zipName = $"archive-EvidenceFiles-{DateTime.Now.ToString("yyyy_MM_dd-HH_mm_ss")}.zip";
using (MemoryStream ms = new MemoryStream())
{
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
foreach (var file in byteArrayList)
{
string fPath = Encoding.ASCII.GetString(file);
var entry = archive.CreateEntry(System.IO.Path.GetFileName(fPath), CompressionLevel.Fastest);
using (var zipStream = entry.Open())
{
var bytes = System.IO.File.ReadAllBytes(fPath);
zipStream.Write(bytes, 0, bytes.Length);
}
}
}
return File(ms.ToArray(), "application/zip", zipName);
}
}

How to download image and save it in local storage using Xamarin-Forms.?

I want to download an image and store it in specific folder in local storage.
I am using this to download image:
var imageData = await AzureStorage.GetFileAsync(ContainerType.Image, uploadedFilename);
var img = ImageSource.FromStream(() => new MemoryStream(imageData));
Create a FileService interface
in your Shared Code, create a new Interface, for instance, called IFileService.cs
public interface IFileService
{
void SavePicture(string name, Stream data, string location="temp");
}
Implementation Android
In your android project, create a new class called "Fileservice.cs".
Make sure it derives from your interface created before and decorate it with the dependency information:
[assembly: Dependency(typeof(FileService))]
namespace MyApp.Droid
{
public class FileService : IFileService
{
public void SavePicture(string name, Stream data, string location = "temp")
{
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
documentsPath = Path.Combine(documentsPath, "Orders", location);
Directory.CreateDirectory(documentsPath);
string filePath = Path.Combine(documentsPath, name);
byte[] bArray = new byte[data.Length];
using (FileStream fs = new FileStream(filePath , FileMode.OpenOrCreate))
{
using (data)
{
data.Read(bArray, 0, (int)data.Length);
}
int length = bArray.Length;
fs.Write(bArray, 0, length);
}
}
}
}
Implementation iOS
The implementation for iOS is basically the same:
[assembly: Dependency(typeof(FileService))]
namespace MyApp.iOS
{
public class FileService: IFileService
{
public void SavePicture(string name, Stream data, string location = "temp")
{
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
documentsPath = Path.Combine(documentsPath, "Orders", location);
Directory.CreateDirectory(documentsPath);
string filePath = Path.Combine(documentsPath, name);
byte[] bArray = new byte[data.Length];
using (FileStream fs = new FileStream(filePath , FileMode.OpenOrCreate))
{
using (data)
{
data.Read(bArray, 0, (int)data.Length);
}
int length = bArray.Length;
fs.Write(bArray, 0, length);
}
}
}
}
In order to save your file, in your shared code, you call
DependencyService.Get<IFileService>().SavePicture("ImageName.jpg", imageData, "imagesFolder");
and should be good to go.
public void DownloadImage(string URL)
{
var webClient = new WebClient();
webClient.DownloadDataCompleted += (s, e) =>
{
byte[] bytes = new byte[e.Result.Length];
bytes=e.Result; // get the downloaded data
string documentsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).AbsolutePath;
var partedURL = URL.Split('/');
string localFilename = partedURL[partedURL.Length-1];
string localPath = System.IO.Path.Combine(documentsPath, localFilename);
File.WriteAllBytes(localPath, bytes); // writes to local storage
Application.Current.MainPage.IsBusy = false;
Application.Current.MainPage.DisplayAlert("Download", "Download Finished", "OK");
MediaScannerConnection.ScanFile(Forms.Context,new string[] { localPath }, null, null);
};
var url = new Uri(URL);
webClient.DownloadDataAsync(url);
}
Here you have to use dependency service from xamarin forms PCL to call this method from android project.This will store your image to public folder. Will edit this if i get time to make a demo with iOS also.
I really liked Karan's approach.
I've made a sort of combination of them both and I wanted to share them here. Worked pretty well actually.
public String DownloadImage(Uri URL)
{
WebClient webClient = new WebClient();
string folderPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Images", "temp");
string fileName = URL.ToString().Split('/').Last();
string filePath = System.IO.Path.Combine(folderPath, fileName);
webClient.DownloadDataCompleted += (s, e) =>
{
Directory.CreateDirectory(folderPath);
File.WriteAllBytes(filePath, e.Result);
};
webClient.DownloadDataAsync(URL);
return filePath;
}

Downloading files as a zip result in corrupted zips in ASP.NET MVC

I have a server hosting a large XML file archive and an API retrieves requested files inside a zip. If I select around 11 or less files, the zip returns just fine. If I retrieve more, I receive the following error when attempting to open the zip:
"Windows cannot open the folder. The Compressed (zipped) Folder is
invalid."
Here are the data classes and methods to create the zip:
//Archive file containing filename and content as memory stream
public class ArchiveFile {
public string FileName;
public System.IO.MemoryStream FileContent;
}
//Method to retrieve archive files and zip them
public static System.IO.MemoryStream GetFilesAsZip (string[] arrFileNames) {
MemoryStream zipStream = null;
using (zipStream = new MemoryStream()) {
// Retrieve files using above method
ArchiveFile[] retrievedFiles = GetFilesFromArchive(arrFileNames);
// Initialize new ZipArchive on the return object's MemoryStream property
using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Update, leaveOpen: true)) {
// Write file entries into archive
foreach (ArchiveFile dataFile in retrievedFiles) {
if (dataFile.FileContent != null) {
// Create new ZipArchiveEntry with content
ZipArchiveEntry zipEntry = archive.CreateEntry(dataFile.FileName);
dataFile.FileContent.WriteTo(zipEntry.Open());
}//end if
} // end foreach
} //end using
} //end using
return zipStream;
}//end method
//API to return content to user as an MVC File Content Result
[HttpGet]
public ActionResult DownloadFiles (string [] fileNames) {
FileContentResult data = new FileContentResult(GetFiles(fileNames).GetBuffer(), “application/zip”) { FileDownloadName = “files.zip” };
return data;
} //end method
The corruption may have something to do with space allocation when writing to the memory stream. I noticed all my "successful" zips (11 or less files) were of size 259 KB but all "unsuccessful" zips (more than 11 files) were of size 517 KB, with some larger attempted zips of size 1034 KB. It strikes me as too much of a coincidence that these are all multiples of 258.5 KB, especially since a zip of 11 files results in a 259 KB zip but a zip of 12 files results in a 517 KB zip.
Any insight on what it could be?
ASP.Net Core API Controller returns corrupted excel file
return this in your controller
return new FileResult("demo.zip", Path.Combine(sWebRootFolder, sFileName), "application/zip");
add this code
public class FileResult : ActionResult
{
public FileResult(string fileDownloadName, string filePath, string contentType)
{
FileDownloadName = fileDownloadName;
FilePath = filePath;
ContentType = contentType;
}
public string ContentType { get; private set; }
public string FileDownloadName { get; private set; }
public string FilePath { get; private set; }
public async override Task ExecuteResultAsync(ActionContext context)
{
var response = context.HttpContext.Response;
response.ContentType = ContentType;
context.HttpContext.Response.Headers.Add("Content-Disposition", new[] { "attachment; filename=" + FileDownloadName });
using (var fileStream = new FileStream(FilePath, FileMode.Open))
{
await fileStream.CopyToAsync(context.HttpContext.Response.Body);
}
}
}
your code refactored
public byte[] GetFilesAsZip (string[] arrFileNames) {
byte[] buffer = null;
using (MemoryStream zipStream = new MemoryStream()) {
// Retrieve files using above method
ArchiveFile[] retrievedFiles = GetFilesFromArchive(arrFileNames);
// Initialize new ZipArchive on the return object's MemoryStream property
using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Update, leaveOpen: true)) {
// Write file entries into archive
foreach (ArchiveFile dataFile in retrievedFiles) {
if (dataFile.FileContent != null) {
// Create new ZipArchiveEntry with content
ZipArchiveEntry zipEntry = archive.CreateEntry(dataFile.FileName);
dataFile.FileContent.WriteTo(zipEntry.Open());
}//end if
} // end foreach
} //end using
buffer = zipStream.ToArray();
} //end using
return buffer;
}//end method
you should be able to change this to
FileContentResult data = new FileContentResult(GetFiles(fileNames), “application/zip”) { FileDownloadName = “files.zip” };
I've had success in the past doing return File(fileLocation, "application/zip", fileName); where fileLocation is the path to the folder and fileName is the name of the actual folder. You can do this right in your ActionResult.

Convert Zip File to byte[] and byte[] to zip file

I try to convert zip file to byte[] and write it to a text file.
int BufferSize=65536;
private void button1_Click(object sender, EventArgs e)
{
DialogResult re = openFileDialog1.ShowDialog();
if (re == DialogResult.OK)
{
string fileName = openFileDialog1.FileName;
try
{
byte[] bytes = File.ReadAllBytes(fileName);
File.WriteAllBytes(#"F:\Info.txt", bytes);
}
catch (Exception) { }
}
}
Then I try to convert those byte to zip file. But I can't do it.
My code is here:
private void button2_Click(object sender, EventArgs e)
{
DialogResult re = openFileDialog1.ShowDialog();
if (re == DialogResult.OK)
{
string fileName = openFileDialog1.FileName;
try
{
byte[] bytes = File.ReadAllBytes(fileName);
using (var mstrim = new MemoryStream(bytes))
{
using (var inStream = new GZipStream(mstrim, CompressionMode.Compress))
{
using (var outStream = File.Create("Tax.Zip"))
{
var buffer = new byte[BufferSize];
int readBytes;
while ((readBytes = inStream.Read(buffer, 0, BufferSize)) != 0)
{
outStream.Write(buffer, 0, readBytes);
}
}
}
}
}
catch (Exception) { }
}
}
Error:File Mode not valid.
What File Mode is needed and how can I accomplish what I described?
Just try this.
byte[] data = File.ReadAllBytes("D:\\z.7z");
File.WriteAllBytes("D:\\t.txt", data); // Requires System.IO
byte[] newdata = File.ReadAllBytes("D:\\t.txt");
File.WriteAllBytes("D:\\a.7z", newdata); // Requires System.IO
Try this,
private void button1_Click(object sender, EventArgs e)
{
byte[] arr;
MemoryStream ms = new MemoryStream();
arr = File.ReadAllBytes("C:\\asik.zip");
File.WriteAllBytes(#"D:\\asik.txt", arr);
ms.Close();
FileStream stream = File.OpenRead(#"D:\\asik.txt");
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
MemoryStream ms1 = new MemoryStream(fileBytes);
CreateToMemoryStream(ms1, #"D:\\asik.zip");
ms1.Close();
}
public void CreateToMemoryStream(MemoryStream memStreamIn, string zipEntryName)
{
MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
ZipEntry newEntry = new ZipEntry(zipEntryName);
newEntry.DateTime = DateTime.Now;
zipStream.PutNextEntry(newEntry);
StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
zipStream.CloseEntry();
zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.
zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream.
//outputMemStream.Position = 0;
//return outputMemStream;
//// Alternative outputs:
//// ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory.
//byte[] byteArrayOut = outputMemStream.ToArray();
//// GetBuffer returns a raw buffer raw and so you need to account for the true length yourself.
//byte[] byteArrayOut2 = outputMemStream.GetBuffer();
//long len = outputMemStream.Length;
}
You're using GZipStream, which is used for GZip files, not (PK-)Zip files. This isn't going to work, obviously. Try the ZipFile class instead (though sadly, it doesn't work on streams, just files).
Apart from simply being a different file format, the big difference is that GZip is for compression only, while Zip is also an archive (that is, it can contain multiple files).
public class BytesVal {
public static void main(String[] args) throws IOException, MoreZipException {
// TODO Auto-generated method stub
File file = new File("F:\\ssd\\doc\\");
System.out.println("Byte inside the Zip file is" + BytesVal.getAllBytes(file));
}
public static byte[] getAllBytes(File folderName) throws IOException, MoreZipException {
String[] sourceFiles = null;
if (folderName.isDirectory()) {
sourceFiles = folderName.list();
if (sourceFiles.length > 1) {
throw new MoreZipException(sourceFiles.length);
}
}
byte[] bytes = null;
Path filePath = Paths.get("F:/ssd/doc/" + sourceFiles[0]);
bytes = Files.readAllBytes(filePath);
return bytes;
}
}

Read Xml file and save content into memory WP7

I have a xml with data, in this case images stored in the internet..i want to read the xml in windows phone and save it to the memory.. how can i do that? any tutorial?
Lets divide your task into two parts
1. Downloading XML file containing image path
2. Reading that XML file and binding image control to that dynamic path
Lets Proceeds with first case:
1. Downloading XML file containing image path
here Path=http://server_adrs/XML_FILE
iso_path=Path inside Isolated Storage where u want to save XML file.
public void GetXMLFile(string path)
{
WebClient wcXML = new WebClient();
wcXML.OpenReadAsync(new Uri(path));
wcXML.OpenReadCompleted += new OpenReadCompletedEventHandler(wc);
}
void wc(object sender, OpenReadCompletedEventArgs e)
{
var isolatedfile = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(iso_path, System.IO.FileMode.Create, isolatedfile))
{
byte[] buffer = new byte[e.Result.Length];
while (e.Result.Read(buffer, 0, buffer.Length) > 0)
{
stream.Write(buffer, 0, buffer.Length);
}
stream.Flush();
System.Threading.Thread.Sleep(0);
}
}
2. Reading XML file and binding image control to the dynamic path
here i am having an List which is showing an images, so i will a function to bind images to this list as per below.
public IList<Dictionary> GetListPerCategory_Icon(string category, string xmlFileName)
{
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (storage.FileExists(xmlFileName))
{
using (Stream stream = storage.OpenFile(xmlFileName, FileMode.Open, FileAccess.Read))
{
try
{
loadedData = XDocument.Load(stream);
var data = from query in loadedData.Descendants("category")
where query.Element("name").Value == category
select new Glossy_Test.Dictionary
{
Image=GetImage((string)query.Element("iconpress")),//This is a function which will return Bitmap image
};
categoryList = data.ToList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), (((PhoneApplicationFrame)Application.Current.RootVisual).Content).ToString(), MessageBoxButton.OK);
return categoryList = null;
}
}
}
}
return categoryList;
}
and here the definition for above function
public BitmapImage GetImage(string imagePath)
{
var image = new BitmapImage();
imagePath = "/Glossy" + imagePath;
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (storage.FileExists(imagePath))
{
using (Stream stream = storage.OpenFile(imagePath, FileMode.Open, FileAccess.Read))
{
image.SetSource(stream);
}
}
}
return image;
}
you could use WebClient to pull the xml from the server and then save it as an XDocument in your callback.

Categories