When I execute following code:
const int blockSize = 64;
const int keySize = 256;
string inputText;
byte[] inputBytes;
using (StreamReader reader = new StreamReader(inputFile))
{
inputText = reader.ReadToEnd();
inputBytes = StringToBytes(inputText);
}
byte[] outputBytes = new byte[inputBytes.Length];
CipherKeyGenerator keygen = new CipherKeyGenerator();
SecureRandom rand = new SecureRandom();
KeyGenerationParameters keygenParams = new KeyGenerationParameters(rand, keySize);
keygen.Init(keygenParams);
byte[] key = keygen.GenerateKey();
BufferedBlockCipher cipher = null;
ICipherParameters cipherParams = null;
byte[] iv = new byte[blockSize];
rand.NextBytes(iv);
cipherParams = new ParametersWithIV(
new ParametersWithSBox(
new KeyParameter(key),
Gost28147Engine.GetSBox("E-A")),
iv);
cipher = new BufferedBlockCipher(
new CfbBlockCipher(new Gost28147Engine(), subblockLength));
cipher.Init(true, cipherParams);
int bytesLength = cipher.ProcessBytes(inputBytes, 0, inputBytes.Length, outputBytes, 0);
cipher.DoFinal(outputBytes, bytesLength);
…I get following exception:
System.ArgumentOutOfRangeException was unhandled
Source=mscorlib
ParamName=dstIndex
StackTrace:
w System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
w System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
w Org.BouncyCastle.Crypto.Modes.CfbBlockCipher.Init(Boolean forEncryption, ICipherParameters parameters)
w Org.BouncyCastle.Crypto.BufferedBlockCipher.Init(Boolean forEncryption, ICipherParameters parameters)
...
Already resolved problem. In
new CfbBlockCipher(new Gost28147Engine(), subblockLength))size of block should be passed as number of bits, not bytes. IV should be the same size as size of block passed to constructor ofCfbBlockCipherso code have look like that:byte[] iv = new byte[subblockLength]A look into
CfbBlockCipher.Init, exception is easy to track from here: