2012-03-21 4 views
0

BouncyCastle 오픈 소스 라이브러리를 사용하여 여러 암호화 알고리즘으로 데이터를 암호화 할 수 있습니까? 한 암호의 출력을 다른 암호의 입력으로 전달하기 위해 아래 코드를 작성했지만 항상 동일한 출력 바이트를 얻고 있습니다. 사전에Bouncycastle을 사용한 다중 암호화

감사합니다, 아마드 야신 물론

Boolean ProcessFile(
     PaddedBufferedBlockCipher aesCipher, 
     PaddedBufferedBlockCipher serpentCipher, 
     PaddedBufferedBlockCipher twofishCipher, 
     FileStream fin, 
     FileStream fout, 
     int addOnLength) 
    { 
     int inputSize = aesCipher.GetBlockSize(); 
     int outputSize = aesCipher.GetOutputSize(inputSize); 

     byte[] inputBuffer = new byte[inputSize]; 
     byte[] outBuffer = new byte[outputSize]; 

     long fileLenght = fin.Length - addOnLength; 
     long progressStep = fileLenght >= 100 ? fileLenght/100 : fileLenght; 

     long totalLength = 0; 
     int inLength = 0; 
     int outLength; 

     try 
     { 
      while ((inLength = fin.Read(inputBuffer, 0, inputSize)) != 0) 
      { 
       outLength = aesCipher.ProcessBytes(inputBuffer, 0, inLength, outBuffer, 0); 
       outLength = serpentCipher.ProcessBytes(outBuffer, 0, outLength, outBuffer, 0); 
       outLength = twofishCipher.ProcessBytes(outBuffer, 0, outLength, outBuffer, 0); 

       fout.Write(outBuffer, 0, outLength); 
       totalLength += inLength; 
       if (totalLength >= progressStep) 
       { 
        DoProgressChanged(); 
        totalLength = totalLength % progressStep; 
       } 
      } 
      outLength = aesCipher.DoFinal(outBuffer, 0); 
      fout.Write(outBuffer, 0, outLength); 
      fout.Close(); 
      fin.Close(); 
      return true; 
     } 
     catch (IOException e) 
     { 
      throw new IOException(e.Message); 
     } 
    } 


    public event EventHandler ProgressChanged; 
    private void DoProgressChanged() 
    { 
     if (this.ProgressChanged != null) 
      this.ProgressChanged(this, new EventArgs()); 
    } 



    enum Mode { Encription = 1, Decription = 0 } 
    enum Algorithm { AES = 1, Serpent = 2, Twofish = 3 } 
    PaddedBufferedBlockCipher GetCipher(Algorithm algorithm, Mode mode, byte[] key, byte[] iv) 
    { 
     IBlockCipher blockCipher; 
     ICipherParameters parameters = new ParametersWithIV(new KeyParameter(key), iv); 
     switch (algorithm) 
     { 
      case Algorithm.AES: 
       blockCipher = new AesFastEngine(); 
       break; 
      case Algorithm.Serpent: 
       blockCipher = new SerpentEngine(); 
       break; 
      case Algorithm.Twofish: 
       blockCipher = new TwofishEngine(); 
       break; 
      default: 
       blockCipher = new AesFastEngine(); 
       break; 
     } 
     IBlockCipher cbcblockCipher = new CbcBlockCipher(blockCipher); 
     PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(cbcblockCipher, new Pkcs7Padding()); 
     cipher.Init(mode == Mode.Encription, parameters); 
     return cipher; 
    } 
+0

outBuffer를 입력 및 출력 버퍼로 동시에 사용할 수 있다고 생각하지 않습니다. 또한 패딩 때문에 각 적용 암호에 따라 결과가 커질 수 있습니다. – Robert

답변

1

이 가능하지만, 난 강력하게 당신이 연결된 스트림의 어떤 종류를 사용하거나 버퍼 (먼저 AES, 뱀 위에 세 패스를 수행 제안 , TwoFish 또는 다른 순서). 블록 크기 및 기타 매개 변수가 다를 수 있으므로 지금처럼 모든 암호에 대해 블록 크기 암호화를 수행하지 마십시오.

물론 3 개의 별도 암호화 알고리즘을 사용하는 것이 유용 할 수 있는지에 대해서는 논쟁의 여지가 있습니다.

+0

대용량 파일 작업시 블록 크기 암호화를 사용하여 암호화/암호 해독 진행 상황을 보여줍니다. 누락 된 행을 추가하기 위해 코드를 편집했습니다. –