private MCTResult <AlgoArrayResponse> Encrypt(IModeBlockCipherParameters param) { var responses = new List <AlgoArrayResponse>(); var i = 0; var j = 0; try { for (i = 0; i < 100; i++) { var iIterationResponse = new AlgoArrayResponse { IV = param.Iv, Key = param.Key, PlainText = param.Payload }; BitString jCipherText = null; BitString previousCipherText = null; BitString copyPreviousCipherText = null; var ivCopiedBytes = iIterationResponse.IV.ToBytes(); param.Iv = new BitString(ivCopiedBytes); for (j = 0; j < 1000; j++) { var jResult = _algo.ProcessPayload(param); jCipherText = jResult.Result; if (j == 0) { previousCipherText = iIterationResponse.IV; } param.Payload = previousCipherText; copyPreviousCipherText = previousCipherText; previousCipherText = jCipherText; } iIterationResponse.CipherText = jCipherText; responses.Add(iIterationResponse); param.Key = _keyMaker.MixKeys(param.Key, previousCipherText, copyPreviousCipherText); param.Iv = previousCipherText; } } catch (Exception ex) { ThisLogger.Debug($"i count {i}, j count {j}"); ThisLogger.Error(ex); return(new MCTResult <AlgoArrayResponse>(ex.Message)); } return(new MCTResult <AlgoArrayResponse>(responses)); }
private void SetupNextOuterLoopValues(ref BitString iv, ref BitString key, ref BitString input, int j, List <BitString> previousOutputs) { if (j == _INNER_ITERATIONS_PER_OUTPUT - 1) { switch (Shift) { case 1: { var len = j - key.BitLength + 1; var keyConcatenation = ConcatenateOutputsStartingAtIndex(previousOutputs, len); key = key.XOR(keyConcatenation).GetDeepCopy(); iv = ConcatenateOutputsStartingAtIndex(previousOutputs, j - (_blockSizeBits / Shift) + 1).GetDeepCopy(); input = previousOutputs[j - _blockSizeBits / Shift].GetDeepCopy(); break; } case 8: { var index = j - (key.BitLength / 8) + 1; var keyConcatenation = ConcatenateOutputsStartingAtIndex(previousOutputs, index); key = key.XOR(keyConcatenation).GetDeepCopy(); iv = ConcatenateOutputsStartingAtIndex(previousOutputs, j - (128 / 8) + 1).GetDeepCopy(); input = previousOutputs[j - 16].GetDeepCopy(); break; } case 128: { int previousOutputsLastIndex = previousOutputs.Count - 1; var currentOutput = previousOutputs[previousOutputsLastIndex]; var previousOutput = previousOutputs[previousOutputsLastIndex - 1]; key = _keyMaker.MixKeys( key, currentOutput, previousOutput ); iv = currentOutput; input = previousOutput; break; } default: throw new ArgumentException(nameof(Shift)); } } }