2016-04-16 7 views
0

パスワードを与えられたIVでIVをシードしたくありませんでした。質問は、生成されたIVで暗号化する方法を作成した場合、生成されたIVを保存して、データベースに保存せずに復号化メソッドに入れることができるかどうかです。生成されたIVを暗号化から復号化の方法に戻す方法

private void EncryptFile(string inputFile,string outputFile, string pass_input){ 

     try 
     { 
      MessageBox.Show("FLAG 1"); 
      UTF8Encoding UE = new UTF8Encoding(); 
      //Represents a UTF-8 encoding of unicode characters 

      string password = pass_input; 
      // passed password through user input 

      byte[] key = UE.GetBytes(password); 
      //password parsed into bytes 

      string cryptFile = outputFile; 
      FileStream FScrypt = new FileStream(cryptFile, FileMode.Create); 
      //FScrypt is created where it creates a file to save output 

      RijndaelManaged RMcrypto = new RijndaelManaged(); 
      //RMcrypto is created where new instance of RijndaelManaged Class is initialized 

      RMcrypto.GenerateIV(); 
      CryptoStream cs = new CryptoStream(FScrypt, RMcrypto.CreateEncryptor(key,RMcrypto.IV), CryptoStreamMode.Write); 
      //CreateEncryptor from RMcrypto creates a symmetric Rijndael Encryptor object with specified key 

      FileStream fsIn = new FileStream(inputFile, FileMode.Open); 
      //fsIn Opens the input file 
      MessageBox.Show("FLAG 2"); 
      int data; 

      while ((data = fsIn.ReadByte()) != -1) 
       //will read input file opened by fsIn 
       cs.WriteByte((byte)data); 
      MessageBox.Show("FLAG 3"); 

      fsIn.Close(); 
      cs.Close(); 
      FScrypt.Close(); 
     catch(Exception ex) 
     { 
      MessageBox.Show("Failed"); 
     } 
} 

private void DecryptFile(string inputFile, string outputFile, string pass_input) 
    { 
     try 
     { 
      string password = pass_input; // Taking password input and storing in local string. 

      UTF8Encoding UE = new UTF8Encoding() //UnicodeEncoding Object UTF8 
      byte[] key = UE.GetBytes(password); // converting password to bytes 

      FileStream fsCrypt = new FileStream(inputFile, FileMode.Open); // Opens the file 

      RijndaelManaged RMCrypto = new RijndaelManaged(); //new RijndaelMaaged object 

      CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(key, **"RMCrypto.IV"**), CryptoStreamMode.Read); 

      FileStream fsOut = new FileStream(outputFile, FileMode.Create); 

      int data; 

      while ((data = cs.ReadByte()) != -1) 
       fsOut.WriteByte((byte)data); 

      fsOut.Close(); 
      cs.Close(); 
      fsCrypt.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Failed"); 
     } 
    } 
+0

FileStreamを使用する代わりに、MemoryStreamを使用します。 – jdweng

+0

あなたは正確に何を暗号化していますか?問題のデータベースとファイル暗号化とは何が関係していますか? – Plutonix

答えて

2

1つのよく使わ/認められた方法で暗号化されたデータにIVを付加することで、IVは秘密である必要はありません。

関連する問題