ver- oder entschlüsselt einen stream via symetrischer verschlüsselung
/*
* die verschlüsselung läuft über einen stream transformer
* verschlüsselung: inputstream -plain-> cryptotransformer -encrypted-> outputstream
* entschlüsselung: inputstream -encrypted-> cryptotransformer -plain-> outputstream
* erzeugen des IV/Keys = Rijndael rj = Rijndael.Create();
*/
public Stream SymetricDeEncryption(bool encrypt, Stream input, Stream output, byte[] key, byte[] iv)
{
int buffSize = 1024;
Rijndael rj = Rijndael.Create();
rj.IV = iv;
rj.Key = key;
ICryptoTransform streamTransformer = null;
if (encrypt)
{
streamTransformer = rj.CreateEncryptor(key, iv);
}
else
{
streamTransformer = rj.CreateDecryptor(key, iv);
}
using (CryptoStream cs = new CryptoStream(input, streamTransformer, CryptoStreamMode.Read))
{
int bytesRead = 0;
byte[] buff = new byte[buffSize];
do
{
bytesRead = cs.Read(buff, 0, buffSize);
output.Write(buff, 0, bytesRead);
}
while (bytesRead > 0);
}
return output;
}
Kommentare zum Snippet