private static IMessage GetMessage(JObject msg) { try { var msgType = msg["type"].ToObject <int>(); var payload = msg["payload"].ToObject <string>().FromHex(); switch (MessageTypeExtension.GetRawValue(msgType)) { case MessageType.PLAIN_MESSAGE: if (payload.Length <= 0) { return(EmptyMessage.Create()); } return(PlainMessage.Create(Encoding.UTF8.GetString(payload))); case MessageType.SECURED_MESSAGE: return(SecureMessage.CreateFromEncodedPayload(payload)); default: return(EmptyMessage.Create()); } } catch (Exception) { return(EmptyMessage.Create()); } }
public static async Task getResponse() { try { Account sender = Account.CreateFromPrivateKey("5cd5a454be8045056bd3c5e67c20dea92a8eb4921351d850f0249983a804a903", NetworkType.Types.TEST_NET); KeyPair keyPair1 = sender.KeyPair; Account reciever = Account.CreateFromPrivateKey("5cd5a454be8045056bd3c5e67c20dea92a8eb4921351d850f0249983a804a903", NetworkType.Types.TEST_NET); KeyPair keyPair2 = reciever.KeyPair; var transaction = TransferTransaction.Create( NetworkType.Types.TEST_NET, Deadline.CreateHours(2), Address.CreateFromEncoded("TB2Z7J-R2K3BZ-2TRS6D-H5747F-Y3XKUM-BNWCI4-BPDJ"), new List <Mosaic> { Xem.CreateRelative(1) }, SecureMessage.Create("Well shit. W/ mosaic", sender.PrivateKey, reciever.PublicKey) ); SignedTransaction signedTransaction = transaction.SignWith(keyPair1); TransactionResponse response = await new TransactionHttp(host).Announce(signedTransaction); Console.WriteLine(response.Message); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } }
public override void Init(MyObjectBuilder_SessionComponent sessionComponent) { base.Init(sessionComponent); if (MyAPIGateway.Multiplayer.MultiplayerActive) { modid = ulong.Parse(this.ModContext.ModId.Substring(0, this.ModContext.ModId.Length - 4)); m_registered = true; MyAPIGateway.Multiplayer.RegisterMessageHandler(SECURECHANNEL_ID, receiveSecureMessage); MyAPIGateway.Multiplayer.RegisterMessageHandler(UNSECURECHANNEL_ID, receiveMessage); rgen = new Random(); if (!MyAPIGateway.Session.IsServer) { rgen.NextBytes(selfkey); var hello = new SecureMessage() { steamid = MyAPIGateway.Multiplayer.MyId, data = MyAPIGateway.Utilities.SerializeToBinary <MessageData>(new MessageData() { modid = this.modid, messageid = 0, message = selfkey }) }; MyAPIGateway.Multiplayer.SendMessageToServer(UNSECURECHANNEL_ID, MyAPIGateway.Utilities.SerializeToBinary <SecureMessage>(hello)); } } }
public void IncludeNewPlayer(Socket socket) { var sendRsa = Config.CreateRsa(); var export = sendRsa.ExportParameters(false); var secureMessage = new SecureMessage().WithRsaParameters(export); socket.Send(secureMessage); var reply = socket.ReceiveRSA <SecureReplyMessage>(sendRsa); var symmetric = new TripleDESCryptoServiceProvider { IV = reply.IV, Key = reply.Key }; var player = new Player { Nick = reply.Nick, Socket = socket, Crypt = symmetric }; WaitingPlayers.Enqueue(player); Console.WriteLine(player.Nick); ConsolidatePlayersIntoGames(); }
public static async Task <bool> SendFromNewAccount(String message, Account newAcc) { KeyPair keyPair = KeyPair.CreateFromPrivateKey(newAcc.PrivateKey); var transaction = TransferTransaction.Create( NetworkType.Types.TEST_NET, Deadline.CreateHours(2), Address.CreateFromEncoded(Config.Address), new List <Mosaic> { Mosaic.CreateFromIdentifier("nem:xem", 10) }, SecureMessage.Create(message, newAcc.PrivateKey, Config.PublicKey) ); TransactionHttp transactionHttp = new TransactionHttp("http://" + Config.Domain + ":7890"); SignedTransaction signedTransaction = transaction.SignWith(keyPair); Thread.Sleep(2000); await transactionHttp.Announce(signedTransaction); Thread.Sleep(2000); Console.WriteLine(signedTransaction.Hash); Console.WriteLine(Config.Address + " recve something from : " + newAcc.Address.Plain); return(true); }
private void receiveMessage(byte[] obj) { try { var message = MyAPIGateway.Utilities.SerializeFromBinary <SecureMessage>(obj); var data = MyAPIGateway.Utilities.SerializeFromBinary <MessageData>(message.data); if (data.modid != modid) { return; } if (data.messageid == 0) { if (MyAPIGateway.Session.IsServer) { //add key from client to dictionary. if (encryptlookuptable.ContainsKey(message.steamid)) { encryptlookuptable.Remove(message.steamid); } rgen.NextBytes(selfkey); byte[] mdata = MyAPIGateway.Utilities.SerializeToBinary <MessageData>(new MessageData() { modid = this.modid, messageid = 0, message = selfkey }); EncryptDecrypt(ref mdata, ref data.message); var hello = new SecureMessage() { steamid = 0, data = mdata }; //we could send this secure? MyAPIGateway.Multiplayer.SendMessageTo(SECURECHANNEL_ID, MyAPIGateway.Utilities.SerializeToBinary <SecureMessage>(hello), message.steamid); EncryptDecrypt(ref data.message, ref selfkey); encryptlookuptable.Add(message.steamid, data.message); } return; } Action <ulong, byte[]> handle; if (messageHandlers.TryGetValue(data.messageid, out handle)) { handle(message.steamid, data.message); } } catch { //do nothing } }
public void CanCreateSecureMessage() { var secureMessage = SecureMessage.Create("Hello", "5949fc564c90ac186cd4f9d2b8298b677bca300b9d8f926ca04e1739e4ed0cba", "2ecf1decef6818bd9c38985afd6efc1c981e64e9a1ecc1e7b6b25eb30454cce0"); var decoded = secureMessage.GetDecodedPayload( "5949fc564c90ac186cd4f9d2b8298b677bca300b9d8f926ca04e1739e4ed0cba", "2ecf1decef6818bd9c38985afd6efc1c981e64e9a1ecc1e7b6b25eb30454cce0"); Assert.AreEqual("Hello", decoded); }
private void HandleSecureMessage(SecureMessage msg) { if (msg.PublicKey.Length == 32) { this.recipentEPK = msg.PublicKey; } if (msg.Payload.Length > 0) { base.OnMessageAssembled(msg.Payload); } }
/// <summary> /// Created Message with information from database /// </summary> /// <param name="dbMsg"></param> /// <param name="aesKey"></param> internal Message(SecureMessage dbMsg, byte[] aesKey) { byte[] msgEncrypted = dbMsg.Message; byte[] msgIv = dbMsg.EncryptionIV; byte[] msgBytes = Crypt.Instance.DecryptAes(dbMsg.Message, aesKey, msgIv); string msgString = Encoding.UTF8.GetString(msgBytes); SenderId = dbMsg.SenderId; Content = msgString; Timestamp = dbMsg.Timestamp; ConversationId = dbMsg.ConId; }
public async Task GetTransactionWithEncryptedMessage() { const string HASH = "fc60dbe36b99769261b86dc562e7dd2189a440cf878511e2e310208335bc5e9d"; var tx = await new TransactionHttp(host).GetTransaction(HASH); Assert.AreEqual(PUBKEYMESS, tx.Signer.PublicKey); Assert.AreEqual(TransactionTypes.Types.Transfer, tx.TransactionType); var ttx = (TransferTransaction)tx; Assert.AreEqual(HASH, ttx.TransactionInfo.Hash); Assert.AreEqual(MessageType.Type.ENCRYPTED, ttx.Message.GetMessageType()); SecureMessage smsg = (SecureMessage)ttx.Message; //Assert.AreEqual("Decrypted message", smsg.GetDecodedPayload("PRIVATE KEY", PUBKEYMESS)); }
internal SecureMessage ToDatabase(int recipientId, byte[] aesKeyBytes) { byte[] contentBytes = Encoding.UTF8.GetBytes(Content); byte[] generatedIv = Crypt.Instance.GenerateIv("AES"); SecureMessage dbMsg = new SecureMessage() { SenderId = SenderId, Message = Crypt.Instance.EncryptAes(contentBytes, aesKeyBytes, generatedIv), ConId = ConversationId, Timestamp = Timestamp, EncryptionIV = generatedIv, }; return(dbMsg); }
public async Task Should_Announce_Transfer_Transaction_With_NetworkCurrencyMosaic_SecureMessage() { var account = Account.GenerateNewAccount(Fixture.NetworkType); Log.WriteLine($"Reciever private key {account.KeyPair.PrivateKeyString}, reciever public key {account.PublicAccount.PublicKey}"); var mosaic = NetworkCurrencyMosaic.CreateRelative(10); Log.WriteLine($"Sender private key {Fixture.SeedAccount.KeyPair.PrivateKeyString}, sender public key {Fixture.SeedAccount.PublicAccount.PublicKey}"); var message = SecureMessage.Create("Test secure message", Fixture.SeedAccount.KeyPair.PrivateKeyString, account.PublicAccount.PublicKey); var result = await Fixture.Transfer(Fixture.SeedAccount, account.Address, mosaic, message, Fixture.GenerationHash); Log.WriteLine($"Transaction confirmed {result.TransactionInfo.Hash}"); result.TransactionInfo.Hash.Should().NotBeNullOrWhiteSpace(); //result.TransactionType.Should().Be(EntityType.TRANSFER); ((TransferTransaction)result).Message.GetMessageType().Should().Be(MessageType.SECURED_MESSAGE.GetValueInByte()); }
public async Task AnnounceTransferTransactionWithMosaicWithSecureMessage() { var keyPair = new KeyPair(Config.PrivateKeyMain); var transaction = TransferTransaction.Create( NetworkType.Types.TEST_NET, Deadline.CreateHours(2), new Address("TAVPDJ-DR3R3X-4FJUKN-PY2IQB-NNRFV2-QR5NJZ-J3WR"), new List <MosaicAmount> { new MosaicAmount("nem:xem", 10) }, SecureMessage.Create("hello2", Config.PrivateKeyMain, "4cc7409929a72019240065c9e53aa339f87ba889238ff9fbe978251fdbb05d9f") ).SignWith(keyPair); var response = await new TransactionHttp(host).Announce(transaction); Assert.AreEqual("SUCCESS", response.Message); }
internal override void ChangeState(PipeState newState) { if (_state == PipeState.AwaitingAck && newState == PipeState.Connected) { //we need to do a key exchange before we can be ready to send anything. if (keyPair == null) { keyPair = Sodium.PublicKeyBox.GenerateKeyPair(); SecureMessage message = new SecureMessage(); message.PublicKey = keyPair.PublicKey; base.Send(MessagePackSerializer.Get <SecureMessage> ().PackSingleObject(message)); this._secureState = SecureDuplexState.AwaitingKeyResponse; } } else { base.ChangeState(newState); } }
public IMessage CreateMessage(ProximaxMessagePayloadModel messagePayload, string senderPrivateKey, string recipientPublicKeyRaw, string recipientAddress, bool useBlockchainSecureMessage) { CheckParameter(messagePayload != null, "messagePayload is required"); var jsonPayload = messagePayload.ToJson(); if (useBlockchainSecureMessage) { var recipientPublicKey = this.GetRecipientPublicKey(senderPrivateKey, recipientPublicKeyRaw, recipientAddress); return(SecureMessage.Create(jsonPayload, senderPrivateKey, recipientPublicKey)); } else { return(PlainMessage.Create(jsonPayload)); } }
internal override void OnMessageAssembled(byte[] message) { switch (_secureState) { case SecureDuplexState.Initializing: //should be a key this.recipentEPK = MessagePackSerializer.Get <SecureMessage> ().UnpackSingleObject(message).PublicKey; break; case SecureDuplexState.AwaitingKeyResponse: this.recipentEPK = MessagePackSerializer.Get <SecureMessage> ().UnpackSingleObject(message).PublicKey; break; case SecureDuplexState.PipeSecure: //should be data (but can be a key) SecureMessage smsg = MessagePackSerializer.Get <SecureMessage> ().UnpackSingleObject(message); this.HandleSecureMessage(smsg); break; } }
public async Task AnnounceTransferTransactionWithMultipleMosaicsWithSecureMessage() { var keyPair = new KeyPair(Config.PrivateKeyMain); var transaction = TransferTransaction.Create( NetworkType.Types.TEST_NET, Deadline.CreateHours(2), new Address("TALICE-ROONSJ-CPHC63-F52V6F-Y3SDMS-VAEUGH-MB7C"), new List <MosaicAmount>() { new MosaicAmount("nem:xem", 1000), //Mosaic.CreateFromIdentifier("nis1porttest:test", 10), TODO: fix multiple mosaic transfer }, SecureMessage.Create("hello2", Config.PrivateKeyMain, "5D8BEBBE80D7EA3B0088E59308D8671099781429B449A0BBCA6D950A709BA068") ).SignWith(keyPair); var response = await new TransactionHttp(host).Announce(transaction); Assert.AreEqual("SUCCESS", response.Message); }
private void recieve() { try { while (true) { SecureMessage m = cryptTCP.Read(); if (m.isSecure) { Console.WriteLine("Recieved Secure Message: " + m.Message); } else { Console.WriteLine("INSECURE MESSAGE. ERROR: " + m.Error); } } } catch (Exception) { dropConnection(); } }
public async Task AnnounceTransferTransactionWithMosaicWithSecureMessage() { var keyPair = KeyPair.CreateFromPrivateKey(Config.PrivateKeyMain); var transaction = TransferTransaction.Create( NetworkType.Types.MIJIN_TEST, Deadline.CreateHours(2), Address.CreateFromEncoded("SAAA57-DREOPY-KUFX4O-G7IQXK-ITMBWK-D6KXTV-BBQP"), new List <Mosaic> { Mosaic.CreateFromIdentifier("nem:xem", 10) }, SecureMessage.Create("hello2", Config.PrivateKeyMain, "5D8BEBBE80D7EA3B0088E59308D8671099781429B449A0BBCA6D950A709BA068") ).SignWith(keyPair); await new TransactionHttp("http://" + Config.Domain + ":3000").Announce(transaction); listener.TransactionStatus(Address.CreateFromPublicKey(keyPair.PublicKeyString, NetworkType.Types.MIJIN_TEST)) .Subscribe(e => Console.WriteLine(e.Status)); var status = await listener.ConfirmedTransactionsGiven(Address.CreateFromPublicKey(transaction.Signer, NetworkType.Types.MIJIN_TEST)).Take(1); Assert.AreEqual(keyPair.PublicKeyString, status.Signer.PublicKey); }
public void Should_Correcntly_Populate_Request() { WebPayIntegration wpi = new WebPayIntegration(new Configuration { AuthenticityToken = "7db11ea5d4a1af32421b564caaa946d1ead3daf0", Key = null, WebPayRootUrl = null }); var secureMessage = new SecureMessage { AcsUrl = "dsjdsj", AuthenticityToken = "aa", Id = "nn", Pareq = "cc" }; _3DSecureHandler _3dSecureHandler = new _3DSecureHandler(secureMessage, wpi); Mock <I3DSecureClient> _3dSecureClient = new Mock <I3DSecureClient>(); // _3dSecureClient.Setup(x => x.FinishTransaction(secureMessage). var response = _3dSecureHandler.FinishTransaction(_3dSecureClient.Object); Assert.AreEqual(_3dSecureHandler.smRequest.PaRes, secureMessage.Pareq); Assert.AreEqual(_3dSecureHandler.smRequest.MD, secureMessage.AuthenticityToken); }
public static async void CreateTransaction(Account receiverAcc, String message) { KeyPair keyPair = KeyPair.CreateFromPrivateKey(Config.PrivateKeyMain); var transaction = TransferTransaction.Create( NetworkType.Types.TEST_NET, Deadline.CreateHours(2), Address.CreateFromEncoded(receiverAcc.Address.Plain), new List <Mosaic> { Mosaic.CreateFromIdentifier("nem:xem", 2000000) }, SecureMessage.Create(message, Config.PrivateKeyMain, receiverAcc.PublicKey) ); TransactionHttp transactionHttp = new TransactionHttp("http://" + Config.Domain + ":7890"); SignedTransaction signedTransaction = transaction.SignWith(keyPair); Thread.Sleep(2000); await transactionHttp.Announce(signedTransaction); Console.WriteLine(signedTransaction.Hash); Console.WriteLine(receiverAcc.Address.Plain + " recve something from : " + Config.Address); Thread.Sleep(2000); }
public async Task AnnounceTransferTransactionWithMultipleMosaicsWithSecureMessage() { var keyPair = KeyPair.CreateFromPrivateKey(Config.PrivateKeyMain); var transaction = TransferTransaction.Create( NetworkType.Types.MIJIN_TEST, Deadline.CreateHours(2), Address.CreateFromEncoded("SAOV4Y5W627UXLIYS5O43SVU23DD6VNRCFP222P2"), new List <Mosaic>() { Mosaic.CreateFromIdentifier("nem:xem", 1000000000000), //Mosaic.CreateFromIdentifier("happy:test2", 10), }, SecureMessage.Create("hello2", Config.PrivateKeyMain, "5D8BEBBE80D7EA3B0088E59308D8671099781429B449A0BBCA6D950A709BA068") ).SignWith(keyPair); await new TransactionHttp("http://" + Config.Domain + ":3000").Announce(transaction); var status = await listener.ConfirmedTransactionsGiven(Address.CreateFromPublicKey(transaction.Signer, NetworkType.Types.MIJIN_TEST)).Take(1); Assert.AreEqual(keyPair.PublicKeyString, status.Signer.PublicKey); }
public static void SendMessage(Message msg, KeyStore ks) { byte[] convAesKey = null; int recipientId = -1; using (DataContext db = new DataContext()) { var conv = GetConversation(msg.SenderId, msg.RecipientId, db); msg.ConversationId = conv.ConId; // Non-blocking execution of setting unread Task.Run(() => SetUnread(conv.ConId, msg.RecipientId)); if (msg.SenderId == conv.UserAId) { convAesKey = Crypt.Instance.DecryptRsa(conv.KeyA, ks.rsaPrivate); recipientId = conv.UserBId; } else if (msg.SenderId == conv.UserBId) { convAesKey = Crypt.Instance.DecryptRsa(conv.KeyB, ks.rsaPrivate); recipientId = conv.UserAId; } SecureMessage dbMsg = msg.ToDatabase(recipientId, convAesKey); if (convAesKey == null || recipientId == -1) { throw new ChatException("Recipient invalid or message could not be encrypted"); } // Add to database db.SecureMessage.Add(dbMsg); db.SaveChanges(); } }
public static async Task <bool> CreateTransaction() { KeyPair keyPair = KeyPair.CreateFromPrivateKey(Config.PrivateKeyMain); var transaction = TransferTransaction.Create( NetworkType.Types.TEST_NET, Deadline.CreateHours(2), Address.CreateFromEncoded("TCPCAZ-7XJ2X4-SWR6AG-6BUEKS-6DQ7DL-Z2D6QB-5M2V"), new List <Mosaic> { Mosaic.CreateFromIdentifier("nem:xem", 10) }, SecureMessage.Create("hello", Config.PrivateKeyMain, "d72f89db8d0a3655168c9d2abe8de2910aaf4506a47d46e22ca48c7e0442e8ef") ); TransactionHttp transactionHttp = new TransactionHttp("http://" + Config.Domain + ":7890"); SignedTransaction signedTransaction = transaction.SignWith(keyPair); Thread.Sleep(2000); await transactionHttp.Announce(signedTransaction); Console.WriteLine(signedTransaction.Hash); Thread.Sleep(2000); return(true); }
public _3DSecureHandler(SecureMessage secureMessage, WebPayIntegration integation) { _secureMessage = secureMessage; _integation = integation; }