Ciao , in seguito a questa discussione

http://forum.html.it/forum/showthrea...readid=1513606

ho cercato in rete , esempi su come criptare un file di testo , la sicurezza e la criptografia
sono argomenti ampi e complessi , e visto , che non so quando e se , utilizzero un file
criptografato, non ritengo di dover imparare e ricordare tutte le classi necessarie .

Premetto , che è un argomento che non ho capito bene , e di cui non conosco la terminologia corretta , quindi spero mi scusiate , per domande stupide e parole senza senso.

A tale scopo, ho pensato di racchiudere il codice , per criptografia simmetrica , in una
classe , e creare e leggere un file criptato , tramite due metodi , scrivifile , leggifile , che non richiedono parametri , e dovrebbero rendere la criptografia di un file di testo della stessa difficoltà di aprire una lattina di birra


Il problema che riscontro , è la creazione della chiave rgbkey ,e del array di inizializzazione
rgbiv ,del metodo CreateDecryptor() , se non si immettono parametri nel metodo , li crea
casualmente , e effettivamente , si ha un file criptografato , ma non avendo una chiave
costante , non è possibile decriptare , rendendo inutile la cosa .

Ho provato , a creare questi due array , convertendo una stringa in array di byte , ho letto che l'array di inizializzazione iv ,per questo algoritmo deve essere di 16 bit , ma mi da errore "
Il vettore di inizializzazione (IV) specificato non corrisponde alla dimensione del blocco per questo algoritmo.




" ?
ho passato 3 ore a guardare esempi , e a leggere documentazione microsoft , non ho capito come creare questi dua array di byte , NON VOGLIO FARLI GENERARE automaticamente , perchè ho pensato , che devono essere due costanti , in modo che cripta e decripta allo stesso modo !

se riscontrate altri problemi nel codice , vorrei andare un po fuori regolamento , e discuterne in questo topic .

Classe

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();

}


            

            

            



    
    




    }
}