2012-08-27 1 views
1

나는 블로우 피시 알고리즘으로 파일을 암호화하고 있지만, 그것에 대해 뭔가를 모르는 것 같습니다. Encipher()을 시도 할 때마다 '잘못된 길이'라는 예외가 throw됩니다. 8로 mod를 얻었을 때 길이가 0이어야한다는 것을 알았고 encipher를 시작하기 위해 8 x 8 블록의 스트림이 있어야 함을 의미한다고 생각합니다. 어떻게해야합니까? 복어의블로우 피쉬 길이가 잘못됨

, 인사이 방법 :

public void Encipher(byte[] data, int length) 
{ 
    uint xl, xr; 
    if ((length % 8) != 0) <-- Exception Line 
     throw new Exception("Invalid Length"); 
    for (int i = 0; i < length; i += 8) 
    { 
     // Encode the data in 8 byte blocks. 
     xl = (uint)((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]); 
     xr = (uint)((data[i + 4] << 24) | (data[i + 5] << 16) | (data[i + 6] << 8) | data[i + 7]); 
     Encipher(ref xl, ref xr); 
     // Now Replace the data. 
     data[i] = (byte)(xl >> 24); 
     data[i + 1] = (byte)(xl >> 16); 
     data[i + 2] = (byte)(xl >> 8); 
     data[i + 3] = (byte)(xl); 
     data[i + 4] = (byte)(xr >> 24); 
     data[i + 5] = (byte)(xr >> 16); 
     data[i + 6] = (byte)(xr >> 8); 
     data[i + 7] = (byte)(xr); 
    } 
} 

내 암호화 방법 :

private void EncryptFile(string szFilePath, string szInfoFile, string szKey, string szEncryptedFile = "") 
     { 
      // Blowfish 
      Blowfish alg = new Blowfish(Encoding.Unicode.GetBytes(szKey)); 

      // Open file 
      System.IO.FileStream originalStream = System.IO.File.OpenRead(szFilePath); 

      // Store original file length 
      long originalLength = originalStream.Length; 
      System.IO.File.WriteAllText(szInfoFile, originalLength.ToString()); 

      Byte[] buffer = new byte[originalStream.Length + (originalStream.Length % 8)]; 
      originalStream.Read(buffer, 0, buffer.Length); 
      originalStream.Close(); 

      // Encrypt 
      alg.Encipher(buffer, buffer.Length); 

      string szEncFile; 

      if (szEncryptedFile != string.Empty) szEncFile = szEncryptedFile; else szEncFile = szFilePath; 

      System.IO.FileStream stream = new System.IO.FileStream(szEncFile, System.IO.FileMode.Create); 
      stream.Write(buffer, 0, buffer.Length); 
      stream.Close(); 
     } 

감사합니다. 무엇을 당신이하려는 것은 8로 나누어 다음 값으로 최대 값 라운드 인 경우

+5

문제의 적절한 진단을 원한다면이 시나리오의 원인이되는 코드를 보여주십시오. –

+0

자신의 Encipher 메소드를 구현하는 대신 http://www.bouncycastle.org/csharp/를 사용하는 것이 좋습니다. –

+0

나는 Blowfish와 함께 돌아 다닙니다. 제안에 대해 감사드립니다. – MahanGM

답변

3

, 당신은 대신이 작업을 수행해야합니다

Byte[] buffer = new byte[originalStream.Length + (8-(originalStream.Length % 8))]; 
+0

고마워. 효과가 있습니다. – MahanGM

2

Peter Ritchieanswered에게 그것을. 그러나 아래에 고려해야 할 몇 가지 관용구가 있습니다. 처리 중에 예외 조건의 경우 자원 처분을 보장하기 위해 IDisposable 구현 클래스 (예 : FileStream)를 using 블럭에 배치합니다. 또 하나는 if..then을 한 줄에 넣었습니다. 정말 이상해. 나는 그것을 사용하는 용도에 맞는 삼항 연산자로 바꿨습니다. 행운을 빌어 요.

private void EncryptFile(string szFilePath, string szInfoFile, string szKey, string szEncryptedFile = "") 
    { 
     // Blowfish 
     Blowfish alg = new Blowfish(Encoding.Unicode.GetBytes(szKey)); 

     // Open file 
     using (System.IO.FileStream originalStream = System.IO.File.OpenRead(szFilePath)) 
     { 
      // Store original file length 
      long originalLength = originalStream.Length; 
      System.IO.File.WriteAllText(szInfoFile, originalLength.ToString()); 

      Byte[] buffer = new byte[originalStream.Length + (originalStream.Length % 8)]; 
      originalStream.Read(buffer, 0, buffer.Length); 
     } 

     // Encrypt 
     alg.Encipher(buffer, buffer.Length); 

     string szEncFile; 

     szEncFile = string.IsNullOrEmpty(szEncryptedFile) ? szFilePath : szEncryptedFile; 

     using (System.IO.FileStream stream = new System.IO.FileStream(szEncFile, System.IO.FileMode.Create)) 
     { 
      stream.Write(buffer, 0, buffer.Length); 
     } 
    } 
+0

감사합니다. 'FileStream'과'if..then' 블록은 심각한 테스트가 아니 었습니다. – MahanGM