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;
}
我不认为你可以同时使用outBuffer作为输入和输出缓冲区。此外请记住,由于填充,结果将随每个应用密码一起增长。 – Robert 2012-03-21 14:19:45