codice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace CriptografiaFile
{
class Criptografia
{
string nomeFileInput = null;
string nomeFileOutput = null;
byte[] datiFile = null;
//Creazione degli oggetti FIleStream
FileStream inputStream = null;
FileStream outputStream = null;
public Criptografia ( string fileinput)
{
this.nomeFileInput =fileinput ;
}
public Criptografia ( string fileoutput,string buf)
{
this.nomeFileOutput =fileoutput ;
this.datiFile = new byte[buf.Length];
this.datiFile = Encoding.ASCII.GetBytes(buf);
}
public Criptografia ( string fileinput,string fileoutput,string buf)
{
this.nomeFileInput =fileinput ;
this.nomeFileOutput =fileoutput ;
this.datiFile =new byte[buf.Length];
this.datiFile =Encoding.ASCII.GetBytes(buf);
}
public string leggifile()
{
/*
* NON SO COME CREARE RGBIV E RGBKEY
byte[] rgbiv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 15, 16};
byte[] rgbkey = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,3,18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
*/
inputStream =new FileStream (nomeFileInput,FileMode.Open ,FileAccess .Read );
datiFile = new byte[inputStream.Length];
// Creazione dell'oggetto SymmetricAlgorithm
SymmetricAlgorithm algoritmoTest = new RijndaelManaged();
// Creazione dell'oggetto ICryptoTransform
ICryptoTransform cifratore = algoritmoTest.CreateDecryptor (rgbkey, rgbiv);
// Creazione dell'oggetto CryptoStream
CryptoStream streamCifrato = new CryptoStream(inputStream,
cifratore, CryptoStreamMode.Read);
streamCifrato.Read(datiFile, 0, datiFile .Length);//LA LETTURA DEL FILE NON SO SE CORRETTA
//NON POSSO DIRLO CON CERTEZZA SENZA LE CHIAVI DEL METODO CREATEDECRYPTOR
streamCifrato.Close();
inputStream.Close();
return Encoding.ASCII.GetString(datiFile );//CONVERSIONE ARRAY BYTE A STRING CORRETTA ???
}
public void scrivifile()
{
/*
* NON SO COME CREARE RGBIV E RGBKEY
byte[] rgbiv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 15, 16 ,17,18,19,20,21,22,23,24,25};
byte[] rgbkey = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 3, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
*/
outputStream=new FileStream (nomeFileOutput,FileMode.OpenOrCreate,FileAccess .Write);
// Creazione dell'oggetto SymmetricAlgorithm
SymmetricAlgorithm algoritmoTest = new RijndaelManaged();
// Creazione dell'oggetto ICryptoTransform
ICryptoTransform cifratore = algoritmoTest.CreateEncryptor(rgbkey ,rgbiv);
// Creazione dell'oggetto CryptoStream
CryptoStream streamCifrato = new CryptoStream(outputStream,
cifratore, CryptoStreamMode.Write);
// Scritura dei dati cifrati nell'oggetto CryptoStream
streamCifrato.Write(datiFile, 0, datiFile.Length);
streamCifrato.Close();
outputStream.Close();
}
}
}