public NiceCryptoWriteStream(Stream dest, INiceCryptoTransform transform) { _dest = dest; _transform = transform; _intermediateBuffer = new byte[transform.InputBlockSize]; _outputBuffer = new byte[transform.OutputBlockSize]; }
public NiceCryptoReadStream(Stream source, INiceCryptoTransform transform) { _source = source; _transform = transform; _inputBuffer = new byte[_bufBlocksCapacity * transform.InputBlockSize]; _outputBuffer = new byte[_bufBlocksCapacity * transform.OutputBlockSize]; }
/// <summary> /// wrap for argumets /// </summary> /// <exception cref="OperationCanceledException">Task with this exception</exception> private static Task MakeTransformTask(INiceCryptoTransform transform, byte[] inBuf, int inOffset, byte[] outBuf, int outOffset, int blocksCount, CancellationToken token, Action <double> progressCallback = null) { return(Task.Run(() => { transform.NiceTransform(inBuf, inOffset, outBuf, outOffset, blocksCount, token, progressCallback); })); }
public BaseEncryptTransform(INiceCryptoTransform transform) { if (transform.InputBlockSize != transform.OutputBlockSize) { throw new CryptographicException("InputBlockSize != OutputBlockSize."); } _baseTransform = transform; }
/// <exception cref="ArgumentException">Wrong length of IV</exception> public CFBDecryptTransform(INiceCryptoTransform transform, byte[] IV) : base(transform) { if (IV.Length != InputBlockSize) { throw new ArgumentException("Wrong length of IV."); } _initVector = new byte[InputBlockSize]; Array.Copy(IV, _initVector, InputBlockSize); }
public static void NiceTransform(this INiceCryptoTransform transform, byte[] inputBuffer, int inputOffset, byte[] outputBuffer, int outputOffset, int blocksCount, Action <double> progressCallback = null) { for (int i = 0; i < blocksCount; i++) { transform.NiceTransform(inputBuffer, inputOffset + i * transform.InputBlockSize, outputBuffer, outputOffset + i * transform.OutputBlockSize, 1); progressCallback?.Invoke((double)(i + 1) / blocksCount); } }
public BaseDecryptTransform(INiceCryptoTransform transform) { if (transform.InputBlockSize != transform.OutputBlockSize) { throw new CryptographicException("InputBlockSize != OutputBlockSize."); } _baseTransform = transform; _prevText = new byte[2 * InputBlockSize]; }
/// <exception cref="ArgumentException">Wrong length of IV</exception> public static ICryptoTransform Get(INiceCryptoTransform transform, byte[] IV, CryptoDirection direction) { if (direction == CryptoDirection.Encrypt) { return(new CFBEncryptTransform(transform, IV)); } else { return(new CFBDecryptTransform(transform, IV)); } }
/// <exception cref="ArgumentException">data is empty.</exception> public static async Task <byte[]> TransformAsync(byte[] data, INiceCryptoTransform transform, int threadsCount = 4, Action <double> progressCallback = null) { if (data.Length == 0) { throw new ArgumentException("Length of text if empty."); } int blocksCount = data.Length / transform.InputBlockSize; int lastBlockSize = data.Length % transform.InputBlockSize; if (lastBlockSize == 0) { blocksCount--; lastBlockSize = transform.InputBlockSize; } byte[] result = new byte[blocksCount * transform.OutputBlockSize]; int blocksPerThread = blocksCount / threadsCount; Task[] transformTasks = new Task[threadsCount]; double[] progresses = new double[threadsCount]; for (int i = 0; i < threadsCount; i++) { int currentBlocksCount = i == threadsCount - 1 ? blocksPerThread + blocksCount % threadsCount : blocksPerThread; int i_ = i; transformTasks[i] = MakeTransformTask(transform, data, i * blocksPerThread * transform.InputBlockSize, result, i * blocksPerThread * transform.OutputBlockSize, currentBlocksCount, (progress) => { progresses[i_] = progress; progressCallback?.Invoke(MathEx.Sum(progresses) / threadsCount); }); } Task <byte[]> finalTask = Task.Run(() => { byte[] buf = new byte[transform.InputBlockSize]; Array.Copy(data, blocksCount * transform.InputBlockSize, buf, 0, lastBlockSize); return(transform.NiceFinalTransform(buf, 0, lastBlockSize)); }); await Task.WhenAll(transformTasks); byte[] final = await finalTask; Array.Resize(ref result, result.Length + final.Length); Array.Copy(final, 0, result, blocksCount * transform.OutputBlockSize, final.Length); return(result); }
public void Init() { Random random = new Random(126); byte[] tmKey = new byte[8]; random.NextBytes(tmKey); ulong key56 = BitConverter.ToUInt64(tmKey); _encryptor = DES_.GetNice(key56, CryptoDirection.Encrypt); _decryptor = DES_.GetNice(key56, CryptoDirection.Decrypt); _texts = new byte[3][]; _texts[0] = new byte[800_003];