/// <summary> /// Transforms an input block. /// </summary> /// <param name="transformField">Either the <see cref="encryptor"/> or <see cref="decryptor"/> field.</param> /// <param name="transformCreator">The function to create a new transformer.</param> /// <param name="data">The input data.</param> /// <param name="iv">The initialization vector.</param> /// <returns>The result of the transform.</returns> private byte[] CipherOperation(ref Platform.ICryptoTransform transformField, Func <SymmetricCryptographicKey, byte[], Platform.ICryptoTransform> transformCreator, byte[] data, byte[] iv) { Requires.NotNull(transformCreator, nameof(transformCreator)); Requires.NotNull(data, nameof(data)); if (this.Padding == SymmetricAlgorithmPadding.None && data.Length == 0) { return(data); } if (iv != null || !this.CanStreamAcrossTopLevelCipherOperations || transformField == null) { transformField?.Dispose(); transformField = transformCreator(this, iv); } if (this.CanStreamAcrossTopLevelCipherOperations) { byte[] outputBlock = new byte[data.Length]; int bytesOutput = transformField.TransformBlock(data, 0, data.Length, outputBlock, 0); Array.Resize(ref outputBlock, bytesOutput); return(outputBlock); } else { return(transformField.TransformFinalBlock(data, 0, data.Length)); } }
private byte[] CipherBlock(System.Security.Cryptography.ICryptoTransform cryptor, byte[] input) { WriteLog("--- Cipher Block:"); WriteLog("InputBlockSize: " + cryptor.InputBlockSize.ToString()); WriteLog("OutputBlockSize: " + cryptor.OutputBlockSize.ToString()); int finalSize = input.Length % cryptor.InputBlockSize; int blockCount = (input.Length - finalSize) / cryptor.InputBlockSize; if (finalSize > 0) { blockCount += 1; } WriteLog("InputLength: " + input.Length.ToString()); WriteLog("BlockCount: " + blockCount.ToString()); WriteLog("FinalSize: " + finalSize.ToString()); int len = input.Length; System.IO.MemoryStream stream = new System.IO.MemoryStream(); for (int b = 0; b < blockCount; b++) { byte[] inputBuffer = new byte[cryptor.InputBlockSize]; byte[] outputBuffer = new byte[cryptor.OutputBlockSize]; // Calculate length of current block: int blockLength = b < blockCount - 1 ? cryptor.InputBlockSize : (len - 1) % cryptor.InputBlockSize + 1; int iPos = b * cryptor.InputBlockSize; // Copy data bytes to input buffers. System.Buffer.BlockCopy(input, iPos, inputBuffer, 0, blockLength); if (b < blockCount - 1) { cryptor.TransformBlock(input, iPos, cryptor.InputBlockSize, outputBuffer, 0); } else { WriteLog("Transform final block[" + blockLength.ToString() + "]..."); outputBuffer = cryptor.TransformFinalBlock(input, iPos, blockLength); } WriteEnc(inputBuffer, outputBuffer); stream.Write(outputBuffer, 0, outputBuffer.Length); } return(stream.ToArray()); }
public void Modern_DESTest_Kryptos() { byte[] key = new byte[8]; ulong baseKey = (ulong)'S' | (ulong)'O' << 8 | (ulong)'T' << 16 | (ulong)'P' << 24 | (ulong)'Y' << 32 | (ulong)'R' << 40 | (ulong)'K' << 48; //ulong baseKey = (ulong)'K' | (ulong)'R' << 8 | (ulong)'Y' << 16 | (ulong)'P' << 24 | (ulong)'T' << 32 | (ulong)'O' << 40 | (ulong)'S' << 48; ulong expandedKey = baseKey & 0x7F; expandedKey |= ((baseKey >> 7) & 0x7F) << 8; expandedKey |= ((baseKey >> 14) & 0x7F) << 16; expandedKey |= ((baseKey >> 21) & 0x7F) << 24; expandedKey |= ((baseKey >> 28) & 0x7F) << 32; expandedKey |= ((baseKey >> 35) & 0x7F) << 40; expandedKey |= ((baseKey >> 42) & 0x7F) << 48; expandedKey |= ((baseKey >> 49) & 0x7F) << 56; System.Security.Cryptography.DES des = System.Security.Cryptography.DES.Create(); des.Key = BitConverter.GetBytes(expandedKey); des.Mode = System.Security.Cryptography.CipherMode.ECB; System.Security.Cryptography.ICryptoTransform xform = des.CreateDecryptor(); byte[] data = new byte[8]; data[0] = (byte)'O'; data[1] = (byte)'K'; data[2] = (byte)'R'; data[3] = (byte)'U'; data[4] = (byte)'O'; data[5] = (byte)'X'; data[6] = (byte)'O'; byte[] output = new byte[8]; xform.TransformBlock(data, 0, 8, output, 0); for (int i = 0; i < 8; i++) { output[i] = (byte)(output[i] % 26); } }