How to encrypt & decrypt multiple files to one file? - c#

As title,
I want to encrypt 10 files into one file and the extension file can be customized. Once encrypted, I will automatically decrypt it to create 10 files as originally. Does anyone have any ideas?

Just as any other stream where you can't discern messages, you need a delimiter of some sorts. Prepending the length of stuff is a common way to do so.
So make up a file format specification, for example:
Start the file with an uint32 which specifies the number of files in the archive
Then, per file:
Write an uint32 specifying the file name length in bytes in the encoding you want to use (I'd go for UTF-8)
Write the file name's bytes
Write an uint32 specifying the file data length
Write the file data
Then when reading the file, read the uints and extract the next bytes.
But you generally don't want to invent your own file format.

Possbile solution
Encrypt all the files
Store the encrypted data (byte[]) in a List<byte[]> or store it separatly in temp-files (maybe file.oldext.newext.tmp)
Create a new XML file, write all the data to it (create a list within the XML file and store the data in the list elements; One element per file)
Save the XML file to the disk (newfilename.newext)

Related

How to view a picture from a byte array that contains a zip-compressed file?

I'm working on a program in C# that can view the content of encrypted ZIP files without having to extract them. So keep in mind, I really don't want to write to any directory file or the encrypted zip. With encrypted I mean there is a byte shifting encryption on the file headers.
I have a loop that decrypts the file headers. So then I have the File Header and Data (compressed). Now let's say there is only 1 file in the ZIP (Image). How can I display this image in a picturebox? I tried to fill a byte array with the decrypted bytes and then view 1 file from it with the DotNetZip library. But when I write/read the ZIP from byte array it says it's broken (I gues I messed up the file format?). Is there any way I can view the compressed file in a picturebox?

C# Save byte array as xml file

I'm receiving a .zip file from a server.
The .zip file is sent 64Base encoded and it contains an XML file.
After I decode the data to binary using Convert.FromBase64String, can I convert the byte array to XML?
I don't want to deal with unzipping.
I tried the following code: (that resulted in Gibberish that doesn't make any sense and doesn't look like XML at all)
XmlDocument doc = new XmlDocument();
string xml = Encoding.UTF8.GetString(buffer);
doc.LoadXml(xml);
Any ideas?
You say you don't want to unzip, but do you actually mean that you don't want to unzip to disc? Most zip libraries either allow you to unzip a file to a byte array directly or to a stream where you could pass it a MemoryStream.
There's no getting around having to uncompress. Unless you have control over the server side, then you could change the format to an uncompressed file (like a tar file). Then you wouldn't have to uncompress.
You say:
I'm receiving a .zip file from a server.
And:
I don't want to deal with unzipping.
Well. You have to. If the data is in a zip archive, you need to extract it first. You can't just ignore the fact.
There are plenty of zip libraries - sharpziplib is free and easy enough to use.

Saving different file formats

In my application I receive different files in base64string.
After receiving those base64Strings my application needs to convert them
into their original formats.
These files could be pdf,txt,jpeg image,bmp image,gif image or png image formats.
How do I know what format this file is in order to convert them to their
respective formats. Is there any way the base64string gives this info.
Any help will be appreciated.'
The base64 data only contains the file data itself, no metadata about it (including file name / extension). You could potentially try to parse the first few bytes of the decoded base64 data to try to find out the file type, but an easier approach would be for the service to add this information in some HTTP header (such as Content-Disposition).
I think you only need to convert it to binary format from base64string and save on disk. You only need to get the correct file extension or complete file name so that user can use associated program to open it.
The only reliable means to get the file type is through metadata associated with the file. If this is not available in your case, a workaround is to read the first few bytes of the file. Many common formats require that files of that format begin with a sequence of bytes, known as "magic numbers".
This Wikipedia article provides maic numbers for PDF, JPG, PNG, and GIF formats. BMP files typically begin with the constant 0x42 0x4D (*). Since text files contain only content, it would need to be a default option (i.e, if the first few bytes aren't recognized as a known magic number, assume it is a text file.)
The Base-64 encoding is simply the binary representation of the file. Converting back to a byte sequence and assessing the first few bytes should be sufficient to suggest a file is of a certain type. Note that this is an imperfect workaround; for instance, a text file that happens to start with a magic number (e.g., "BM") may be miscategorized as another type of file.

Create .wav file using C#

I have nessary chunk header bytes for a wave file stored in a text file. What I'd like to do is create a new .wav that will vary in data length and write into it 50ms/10kHz signals(which is stored in separate file). How can I accomplish with .NET/C#?
-Mickey
Use the System.IO.BinaryReader and System.IO.BinaryWriter streams to read data from your signals files, write binary datatypes to a file, as per the wav file specification.

How to remove zip compression from an XML file?

I have an XML file format .zfo that is compressed using zip algorithm. I need to remove this compression from the file, so that it is in usable XML form. Here is the file.
How can I remove this compression, or decompress this XML file?
It's not like you might imagine i.e: .zip file containing an xml file. Instead the byte[] that's written to the file is zip compressed.
Thanks in advance.
That file isn't zip compressed at all. It appears to be some xml that's embedded in a certificate, issued by the Czech Post Office. The actual message looks to be encoded in some kind of base64 variant.
Call your post office.
Check out DotNetZip (http://www.codeplex.com/DotNetZip)--it probably does what you need (e.g., DeflateStream).
A zip file contains meta-data (file and directory structure) as well as the actually compressed data. It sounds like your file only has the compressed data. DotNetZip should be able to handle both.

Categories

Resources