private void ProcessSelectedPart(int BufferSize, long Begin, long End, Stream fsRead, Stream fsWrite, CryptMethod3Key Algorythm, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { byte[] Buffer = new byte[BufferSize]; byte[] CurBuffer = new byte[BufferSize]; byte[] temp; IAsyncResult ReadResult; IAsyncResult WriteResult; fsRead.Seek(Begin, SeekOrigin.Begin); ReadResult = fsRead.BeginRead(Buffer, 0, BufferSize, null, null); fsRead.EndRead(ReadResult); long i; for (i = Begin + BufferSize; i < End; i += BufferSize) { fsRead.Seek(i, SeekOrigin.Begin); ReadResult = fsRead.BeginRead(CurBuffer, 0, BufferSize, null, null); Buffer = CryptBuffer(Buffer, Algorythm, KeyList1, KeyList2, KeyList3, ref IV, FlipKey); fsWrite.Seek(i - BufferSize, SeekOrigin.Begin); WriteResult = fsWrite.BeginWrite(Buffer, 0, BufferSize, null, null); fsRead.EndRead(ReadResult); fsWrite.EndWrite(WriteResult); temp = Buffer; Buffer = CurBuffer; CurBuffer = temp; } fsWrite.Seek(i - BufferSize, SeekOrigin.Begin); WriteResult = fsWrite.BeginWrite(Buffer, 0, BufferSize, null, null); fsWrite.EndWrite(WriteResult); }
private void EnCryptLastPart(int BufferSize, long Begin, Stream fsRead, Stream fsWrite, CryptMethod3Key Algorythm, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { ProcessSelectedPart(BufferSize, Begin, fsRead.Length - fsRead.Length % BufferSize, fsRead, fsWrite, Algorythm, KeyList1, KeyList2, KeyList3, IV, FlipKey); if ((fsRead.Length % BufferSize) != 0) { byte[] Buffer = new byte[Const.BlockSizeForFile]; fsRead.Seek(fsRead.Length - fsRead.Length % BufferSize, SeekOrigin.Begin); fsRead.Read(Buffer, 0, Const.BlockSizeForFile); Buffer = CryptBuffer(Buffer, Algorythm, KeyList1, KeyList2, KeyList3, ref IV, FlipKey); fsWrite.Seek(fsRead.Length - fsRead.Length % BufferSize, SeekOrigin.Begin); fsWrite.Write(Buffer, 0, Const.BlockSizeForFile); byte[] part = new byte[1] { (byte)(fsRead.Length % BufferSize) }; fsWrite.Seek(fsWrite.Length, SeekOrigin.Begin); fsWrite.Write(part, 0, 1); } }
private void DeCryptLastPart(int BufferSize, long Begin, Stream fsRead, Stream fsWrite, CryptMethod3Key Algorythm, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { if ((fsRead.Length % BufferSize) != 0) { ProcessSelectedPart(BufferSize, Begin, fsRead.Length - fsRead.Length % BufferSize - BufferSize, fsRead, fsWrite, Algorythm, KeyList1, KeyList2, KeyList3, IV, FlipKey); byte[] byt = new byte[1]; fsRead.Seek(-1, SeekOrigin.End); fsRead.Read(byt, 0, 1); byte[] lastPart = new byte[Const.BlockSizeForFile]; fsRead.Seek(-1 - Const.BlockSizeForFile, SeekOrigin.End); fsRead.Read(lastPart, 0, Const.BlockSizeForFile); lastPart = CryptBuffer(lastPart, Algorythm, KeyList1, KeyList2, KeyList3, ref IV, FlipKey); try { fsWrite.Write(lastPart, 0, byt[0]); } catch { throw new Exception("Selected wrong file!"); } } else { ProcessSelectedPart(BufferSize, Begin, fsRead.Length, fsRead, fsWrite, Algorythm, KeyList1, KeyList2, KeyList3, IV, FlipKey); } }
private byte[] CryptBuffer(byte[] buffer, CryptMethod3Key Algorythm, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ref ulong IV, bool FlipKey) { ulong bufLong = 0; for (long i = 0; i < buffer.Length; i += Const.BlockSizeForFile) { ulong temp = 0; for (byte j = 0; j < Const.BlockSizeForFile; j++) { temp |= (ulong)buffer[i + j]; bufLong = temp; temp <<= Const.BlockSizeForFile; } bufLong = Algorythm(bufLong, KeyList1, KeyList2, KeyList3, IV, FlipKey); for (short j = Const.SBoxTablesShift; j >= 0; j--) { buffer[i + j] = (byte)(bufLong & Const.Mask8Bit); bufLong >>= Const.BlockSizeForFile; } } return buffer; }
public void EnCryptFile(Stream fsRead, Stream fsWrite, CryptMethod3Key Algorythm, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { if (fsRead.Length > Const.FileBufferSize) { ProcessSelectedPart(Const.FileBufferSize, 0, fsRead.Length - fsRead.Length % Const.FileBufferSize, fsRead, fsWrite, Algorythm, KeyList1, KeyList2, KeyList3, IV, FlipKey); } EnCryptLastPart(Const.BlockSizeForFile, fsRead.Length - fsRead.Length % Const.FileBufferSize, fsRead, fsWrite, Algorythm, KeyList1, KeyList2, KeyList3, IV, FlipKey); }
public DESCryptor() { _3des = new _3DES(); //DES cm1k = delegate(ulong value, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { return _3des.Crypt(value, KeyList1, FlipKey); }; cm1kCBCENcrupt = delegate(ulong value, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { IV = _3des.Crypt(value ^ IV, KeyList1, FlipKey); return IV; }; cm1kCBCDEcrupt = delegate(ulong value, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { IV = _3des.Crypt(value, KeyList1, FlipKey) ^ IV; return IV; }; //DESEEE3 DESEEE3 = delegate(ulong value, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { return _3des.Crypt(value, KeyList1, KeyList2, KeyList3, FlipKey, FlipKey, FlipKey); }; DESEEE3CBCEncrypt = delegate(ulong value, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { return _3des.Crypt(value ^ IV, KeyList1, KeyList2, KeyList3, FlipKey, FlipKey, FlipKey); }; DESEEE3CBCDecrypt = delegate(ulong value, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { return _3des.Crypt(value, KeyList1, KeyList2, KeyList3, FlipKey, FlipKey, FlipKey) ^ IV; }; //DESEDE3 DESEDE3 = delegate(ulong value, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { return _3des.Crypt(value, KeyList1, KeyList2, KeyList3, FlipKey, !FlipKey, FlipKey); }; DESEDE3CBCEncrypt = delegate(ulong value, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { return _3des.Crypt(value ^ IV, KeyList1, KeyList2, KeyList3, FlipKey, !FlipKey, FlipKey); }; DESEDE3CBCDecrypt = delegate(ulong value, ulong[] KeyList1, ulong[] KeyList2, ulong[] KeyList3, ulong IV, bool FlipKey) { return _3des.Crypt(value, KeyList1, KeyList2, KeyList3, FlipKey, !FlipKey, FlipKey) ^ IV; }; }