Skip to content

Instantly share code, notes, and snippets.

Created November 11, 2011 02:55
============================================
修改前代码如下:
protected byte[] E (byte[] data)
{
List<byte> eData = new List<byte>();
int maxLength = ... ;
int blocksCount = (data.Length - 1) / maxLength;
byte[] block = new byte[maxLength];
for (int i = 0; i <= blocksCount; i++)
{
if (i == blocksCount)
{
int lastBlockLength = data.Length - maxLength * i;
if (maxLength != lastBlockLength)
{
block = new byte[lastBlockLength];
}
}
Buffer.BlockCopy(...);
byte[] eBlock = provider.E(block, true);
eData.AddRange(eBlock);
}
return eData.ToArray();
}
============================================
修改后的代码如下,
protected byte[] E (byte[] data)
{
int maxLength = ... ;
int blocksCount = data.Length / maxLength;
byte[] block = new byte[maxLength];
int hasAdded = 0;
for (int i = 0; i < blocksCount; i++)
{
Buffer.BlockCopy(...);
byte[] eBlock = provider.E(block, true);
eData.AddRange(encryptedBlock);
hasAdded += maxLength;
}
// If the last block length less than maxLength
if (hasAdded < data.Length)
{
int lastBlockLength = data.Length - hasAdded;
if (maxLength != lastBlockLength)
{
block = new byte[lastBlockLength];
}
Buffer.BlockCopy(...);
byte[] lastBlock = provider.E(block, true);
eData.AddRange(lastBlock);
}
return eData.ToArray();
}
============================================
protected byte[] D (byte[] encryptedData)
{
List<byte> dData = new List<byte>();
int keySize = provider.KeySize / 8;
int blocksCount = eData.Length / keySize;
byte[] block = new byte[keySize];
for (int i = 0; i < blocksCount; i++)
{
Buffer.BlockCopy(...);
byte[] dBlock = provider.D (block, true);
dData.AddRange(dBlock);
}
return dData.ToArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment