Vous êtes sur la page 1sur 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();

Vous aimerez peut-être aussi