public int DoFinal(byte[] output, int outOff) { int blockSize = cipher.GetBlockSize(); if (padding == null) { while (bufOff < blockSize) { buf[bufOff++] = 0; } } else { if (bufOff == blockSize) { cipher.ProcessBlock(buf, 0, mac, 0); bufOff = 0; } padding.AddPadding(buf, bufOff); } cipher.ProcessBlock(buf, 0, mac, 0); DesEngine desEngine = new DesEngine(); desEngine.Init(forEncryption: false, lastKey2); desEngine.ProcessBlock(mac, 0, mac, 0); desEngine.Init(forEncryption: true, lastKey3); desEngine.ProcessBlock(mac, 0, mac, 0); Array.Copy(mac, 0, output, outOff, macSize); Reset(); return(macSize); }
public int DoFinal(byte[] output, int outOff) { int blockSize = this.cipher.GetBlockSize(); if (this.padding == null) { while (this.bufOff < blockSize) { this.buf[this.bufOff++] = 0; } } else { if (this.bufOff == blockSize) { this.cipher.ProcessBlock(this.buf, 0, this.mac, 0); this.bufOff = 0; } this.padding.AddPadding(this.buf, this.bufOff); } this.cipher.ProcessBlock(this.buf, 0, this.mac, 0); DesEngine desEngine = new DesEngine(); desEngine.Init(false, this.lastKey2); desEngine.ProcessBlock(this.mac, 0, this.mac, 0); desEngine.Init(true, this.lastKey3); desEngine.ProcessBlock(this.mac, 0, this.mac, 0); Array.Copy(this.mac, 0, output, outOff, this.macSize); this.Reset(); return(this.macSize); }
public void EncryptFile(string inputFile, string outputFile, byte[] key, string mode) { FileStream instream; FileStream outstream; try { double percent = 0.0; long current = 0; long sizeFile = 0; var des = new DesEngine(); des.Init(true, new KeyParameter(key.ToArray())); IMode chosenMode = ChooseMode(mode, key, des); outstream = File.Open(outputFile, FileMode.Create); int chunkSize = 1024 * 1024; using (instream = File.OpenRead(inputFile)) { sizeFile = instream.Length; ProgressChanged?.Invoke(0.0); int byteRead = 0; byte[] input = new byte[chunkSize]; byte[] output = new byte[chunkSize]; while (true) { byteRead = instream.Read(input, 0, chunkSize); if (byteRead == chunkSize) { chosenMode.Process(input, output); outstream.Write(output, 0, chunkSize); } else { byte[] temp = new byte[byteRead]; Array.Copy(input, 0, temp, 0, byteRead); input = temp; input = AddPadding(input.ToArray()); output = new byte[input.Length]; chosenMode.Process(input, output); outstream.Write(output, 0, output.Length); break; } current += byteRead; percent = (double)current / sizeFile; percent *= 100; ProgressChanged?.Invoke(percent); } } } catch (Exception) { throw new Exception("Ошибка!"); } outstream?.Close(); instream?.Close(); }
public int DoFinal( byte[] output, int outOff) { int blockSize = cipher.GetBlockSize(); if (padding == null) { // pad with zeroes while (bufOff < blockSize) { buf[bufOff++] = 0; } } else { if (bufOff == blockSize) { cipher.ProcessBlock(buf, 0, mac, 0); bufOff = 0; } padding.AddPadding(buf, bufOff); } cipher.ProcessBlock(buf, 0, mac, 0); // Added to code from base class DesEngine deseng = new DesEngine(); deseng.Init(false, this.lastKey2); deseng.ProcessBlock(mac, 0, mac, 0); deseng.Init(true, this.lastKey3); deseng.ProcessBlock(mac, 0, mac, 0); // **** Array.Copy(mac, 0, output, outOff, macSize); Reset(); return(macSize); }
static LoginPacket() { var key = new byte[] { 0x54, 0x45, 0x53, 0x54, 0x00, 0x00, 0x00, 0x00 }; Decrypter = new DesEngine(); Decrypter.Init(false, new DesParameters(key)); //Encrypter = new DesEngine(); //Encrypter.Init(true, new DesParameters(key)); }
public override byte[] Encrypt(byte[] text, byte[] key, string mode) { text = AddPadding(text); var des = new DesEngine(); des.Init(true, new KeyParameter(key.ToArray())); IMode chosenMode = ChooseMode(mode, key, des); byte[] result = new byte[text.Length]; chosenMode.Process(text, result); return(result); }
public override byte[] Decrypt(byte[] ciphertext, byte[] key, string mode) { byte[] result = new byte[ciphertext.Length]; var des = new DesEngine(); des.Init(false, new KeyParameter(key.ToArray())); //CFB ecb = new CFB(des, key.ToArray()); IMode chosenMode = ChooseMode(mode, key, des); chosenMode.Process(ciphertext, result); return(RemovePadding(result)); }
public DesTransform(bool encryption, byte[] key) { engine = new DesEngine(); engine.Init(encryption, new KeyParameter(key)); }