private void HandleEncryptionResponse(EncryptionResponsePacket packet) { if (Authorized) { return; } if (AuthorizationStatus.HasFlag(AuthorizationStatus.EncryprionEnabled)) { var pkcs = new PKCS1Signer(Module.Security.RSAKeyPair); var decryptedToken = pkcs.DeSignData(packet.VerificationToken); for (var i = 0; i < VerificationToken.Length; i++) { if (decryptedToken[i] != VerificationToken[i]) { SendPacket(new AuthorizationDisconnectPacket { Reason = "Unable to authenticate." }); return; } } Array.Clear(VerificationToken, 0, VerificationToken.Length); var sharedKey = pkcs.DeSignData(packet.SharedSecret); Stream = new ProtobufTransmission <SCONPacket>(Socket, new BouncyCastleAesStream(Socket, sharedKey)); } else { SendPacket(new AuthorizationDisconnectPacket { Reason = "Encryption not enabled!" }); } }
private void HandleAuthorizationRequest(AuthorizationRequestPacket packet) { if (Authorized) { return; } SendPacket(new AuthorizationResponsePacket { AuthorizationStatus = AuthorizationStatus }); if (AuthorizationStatus.HasFlag(AuthorizationStatus.EncryprionEnabled)) { var publicKey = Module.Security.RSAKeyPair.PublicKeyToByteArray(); VerificationToken = new byte[4]; var drg = new DigestRandomGenerator(new Sha512Digest()); drg.NextBytes(VerificationToken); SendPacket(new EncryptionRequestPacket { PublicKey = publicKey, VerificationToken = VerificationToken }); } }