public override void GenerateKey() { base.KeyValue = RNG.GetBytes(base.KeySizeValue >> 3); }
private byte[] EncryptFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) { int mod = inputCount % InputBlockSize; if (mod != 0 && _algo.Padding == PaddingMode.None) { throw new CryptographicException(); } if (inputCount == 0 && (_algo.Padding == PaddingMode.None || _algo.Padding == PaddingMode.Zeros)) { return(new byte[0]); } int retBlocks = inputCount / InputBlockSize; bool appendLastBlock = false; if (mod > 0 || (_algo.Padding != PaddingMode.None && _algo.Padding != PaddingMode.Zeros)) { retBlocks++; appendLastBlock = true; } byte padSize = (byte)(retBlocks * InputBlockSize - inputCount); int norSize = inputCount - mod; byte[] buf = new byte [retBlocks * OutputBlockSize]; TransformBlock(inputBuffer, inputOffset, norSize, buf, 0); if (appendLastBlock) { byte[] lastBlock = new byte [InputBlockSize]; switch (_algo.Padding) { case PaddingMode.ANSIX923: Buffer.BlockCopy(inputBuffer, inputOffset + norSize, lastBlock, 0, mod); lastBlock [lastBlock.Length - 1] = padSize; break; case PaddingMode.PKCS7: Buffer.BlockCopy(inputBuffer, inputOffset + norSize, lastBlock, 0, mod); for (int i = mod; i < lastBlock.Length; i++) { lastBlock[i] = padSize; } break; case PaddingMode.ISO10126: RNG.GetBytes(lastBlock); Buffer.BlockCopy(inputBuffer, inputOffset + norSize, lastBlock, 0, mod); lastBlock[lastBlock.Length - 1] = padSize; break; default: Buffer.BlockCopy(inputBuffer, inputOffset + norSize, lastBlock, 0, mod); break; } TransformBlock(lastBlock, 0, InputBlockSize, buf, (retBlocks - 1) * OutputBlockSize); Array.Clear(lastBlock, 0, lastBlock.Length); } return(buf); }
public override void GenerateIV() { base.IVValue = RNG.GetBytes(base.BlockSizeValue >> 3); }