2012-03-21 67 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 2012-03-21 14:19:45

回答

1

当然是可能的,但我强烈建议你使用某种类型的连接流,或者通过缓冲(第一AES,然后毒蛇执行三个关,然后TwoFish或任何其他订单)。不要像现在这样对每个密码执行块大小加密,因为块大小和其他参数可能有所不同。

当然,使用三种单独的加密算法的有用性值得商榷。

+0

我正在使用块大小加密来显示处理大文件时的加密/解密进度。我编辑了我的代码以添加缺失的行。 – 2012-03-22 08:31:18

相关问题