protected virtual void Dispose(bool disposing) { if (disposing) { _disposed = true; _readCipher?.Dispose(); _readCipher = null; CipherLib?.Dispose(); CipherLib = null; } }
private void ProcessStreamControl(EncryptedStreamControl sc) { switch (sc) { case EncryptedStreamControl.EncryptRSA: { if (!CipherLib.ContainsKey("rsa-private")) { throw new SecureChannelException("RSA certificate is required."); } ReadCipher = CipherLib["rsa-private"].Clone(); break; } case EncryptedStreamControl.EncryptXOR: { var passwLen = ReadUInt16(); if (passwLen != 0) { var passw = ReadData(passwLen); CipherLib["xor"] = new XorCipher(passw); } ReadStream.Cipher = CipherLib["xor"].Clone(); break; } case EncryptedStreamControl.EncryptAES: { var passwLen = ReadUInt16(); if (passwLen != 0) { var passw = ReadData(passwLen); CipherLib["aes"] = new Aes256Cipher(passw); } ReadCipher = CipherLib["aes"].Clone(); break; } default: throw new StreamControlException("Unknown StreamControl command"); } }
public void Encrypt(bool reset = false) { if (reset) { WriteStream.Cipher = new XorCipher(); WriteCipher = null; } if (CipherLib.ContainsKey("rsa-public")) { WriteUInt16((ushort)EncryptedStreamControl.EncryptRSA); WriteCipher = CipherLib["rsa-public"].Clone(); } byte[] salt = Random.Get(32); WriteUInt16((ushort)EncryptedStreamControl.EncryptAES); WriteUInt16((ushort)salt.Length); WriteData(salt); CipherLib["aes"] = new Aes256Cipher(salt); WriteCipher = CipherLib["aes"].Clone(); if (!CipherLib.ContainsKey("xor")) { byte[] passw = Random.Get(2048 / 8); WriteUInt16((ushort)EncryptedStreamControl.EncryptXOR); WriteUInt16((ushort)passw.Length); WriteData(passw); CipherLib["xor"] = new XorCipher(passw); } else { WriteUInt16((ushort)EncryptedStreamControl.EncryptXOR); WriteUInt16((ushort)0); } WriteStream.Cipher = CipherLib["xor"].Clone(); }