0% found this document useful (0 votes)
49 views

1.convert An Audio/video File in Array of Bytes. 2.use A Cryptographic Algorithm To Encrypt/decrypt

This document discusses how to encrypt and decrypt media files by first converting them to an array of bytes that can then be encrypted and decrypted by cryptographic algorithms. It provides code to read any file as an array of bytes in C# and then describes how to take that byte array, convert it back to a media file, and play it.

Uploaded by

Radha Raman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

1.convert An Audio/video File in Array of Bytes. 2.use A Cryptographic Algorithm To Encrypt/decrypt

This document discusses how to encrypt and decrypt media files by first converting them to an array of bytes that can then be encrypted and decrypted by cryptographic algorithms. It provides code to read any file as an array of bytes in C# and then describes how to take that byte array, convert it back to a media file, and play it.

Uploaded by

Radha Raman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Encryption algorithms do not care about particular content of the data

to be encrypted or decrypted. They all work with arrays of bytes,


nothing else, so any file can be processed in the same way. The
problem is: by encryption, you might mean few very different things.

So the steps are:-
1.Convert an audio/video file in array of bytes.
2.Use a cryptographic algorithm to encrypt/decrypt.

How to convert a media file in array of bytes:-
How do I read a file as an array of bytes?
.NET provides a lot of great tools for reading and writing files, lots of stream classes. If
you're like me, I kind of find dealing with streams a pain. If I want to read a file I should
only have to say "ReadFile" and the content should be there the way I want.
As a result, I tend to write lots of helper methods that hide the stream manipulations so I
don't have to remember them. Here's one that will read any file as an array of bytes. I've
found this useful with both binary and text files when I didn't have to analyze the data
inside--such as a simple file transfer.
public static byte[] GetBytesFromFile(string fullFilePath)
{
// this method is limited to 2^32 byte files (4.2 GB)

FileStream fs = null;
try
{
fs = File.OpenRead(fullFilePath);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
return bytes;
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
}

}
How to read a audio file from array of bytes.
1.Get the byte array.
2.Read it in memory stream.
3.Pass it into object of System.Media.SoundPlayer .
4.Play it.
byte[] bytes = GetbyteArray();

// Convert the byte array to wav file



using (Stream s = new MemoryStream(bytes))

{

// http://msdn.microsoft.com/en-us/library/ms143770%28v=VS.100%29.aspx

System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer(s);

myPlayer.Play();

}

System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer(myfile);

myPlayer.Stream = new MemoryStream();

myPlayer.Play();

You might also like