public void EncryptAES(IWriteStream writeStream, string sharedSecret) { if (!IsEncrypt) { return; } using (var aesAlg = new RijndaelManaged()) { var key = new Rfc2898DeriveBytes(sharedSecret, _salt); aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8); var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); var byteFragment = writeStream.ToByteFragment(); using (var resultStream = new MemoryStream()) { using (var csEncrypt = new CryptoStream(resultStream, encryptor, CryptoStreamMode.Write)) { csEncrypt.Write(byteFragment.Buffer, byteFragment.Offset, byteFragment.Count); } writeStream.Reset(); writeStream.ShiftLeft(aesAlg.IV); var ivLength = (ushort)aesAlg.IV.Length; writeStream.ShiftLeft(ivLength); writeStream.ShiftRight((ushort)byteFragment.Count); var bytes = resultStream.ToArray(); writeStream.ShiftRight(bytes); } } }
private void AcceptConnected(IUser peer, IReadStream readStream, IWriteStream writeStream) { var publicKey = readStream.ShiftRight <string>(); var key = ID + "_" + SystemUtil.CreateRandomString(Ssfi.CryptoConfig.CryptonKeyLength); var encryptKey = Ssfi.Security.EncryptAesKey(key, publicKey); var securityComponent = SecurityComponent.GetObject(); securityComponent.AesKey = key; securityComponent.Encryptkey = encryptKey; peer.AddComponent <ISecurityComponent>(securityComponent); Logger.Debug($"添加{nameof(ISecurityComponent)}组件!"); writeStream.ShiftRight((ushort)encryptKey.Length); Logger.Debug("EncryptKey Length:" + encryptKey.Length); writeStream.ShiftRight(encryptKey); writeStream.ShiftRight(string.Join(";", RpcMethodIds)); var tmp = readStream.Clone(); _serverSocket.AddEvent(() => { try { _acceptAction.Invoke(peer, readStream); } catch (Exception e) { Logger.Error(e); } tmp.Dispose(); }); }
public Serializable(object _ = null) { WriteStream = PoolAllocator <IWriteStream> .GetObject(); _endianBinaryWriter = new EndianBinaryWriter(WriteStream); WriteStream.ShiftRight((byte)0); }
public void Write(TypeCode typeCode, IConvertible value) { if (value != null) { _writeStream.ShiftRight(true); _writeStream.ShiftRight((byte)typeCode); _writeStream.ShiftRight(typeCode, value); } else { _writeStream.ShiftRight(false); } }