Exemplo n.º 1
0
        public string Decrypt(Model.EncryptedMessage message, AccountWithPublicKey account, string secretPhrase)
        {
            var localMessageService = new LocalMessageService();
            var decrypted           = localMessageService.DecryptTextFrom(account.PublicKey, message.Message, message.Nonce, true, secretPhrase);

            return(decrypted);
        }
Exemplo n.º 2
0
 private static string GetNoteToSelfMessage(Transaction transaction)
 {
     if (transaction.EncryptToSelfMessage != null && transaction.SenderRs == _walletRepository.NxtAccountWithPublicKey.AccountRs)
     {
         var messageService = new LocalMessageService();
         var message        = transaction.EncryptToSelfMessage;
         var decryptedText  = messageService.DecryptTextFrom(transaction.SenderPublicKey, message.Data, message.Nonce, message.IsCompressed, _walletRepository.SecretPhrase);
         return(decryptedText);
     }
     return(null);
 }
Exemplo n.º 3
0
        public Model.EncryptedMessage Encrypt(string data, AccountWithPublicKey account, string secretPhrase)
        {
            var localMessageService = new LocalMessageService();
            var nonce     = localMessageService.CreateNonce();
            var encrypted = localMessageService.EncryptTextTo(account.PublicKey, data, nonce, true, secretPhrase);

            return(new Model.EncryptedMessage
            {
                Message = encrypted.ToString(),
                Nonce = nonce.ToString()
            });
        }
Exemplo n.º 4
0
 private CreateTransactionParameters.AlreadyEncryptedMessageToSelf GetEncryptedNoteToSelfMessage()
 {
     if (!string.IsNullOrEmpty(NoteToSelfMessage))
     {
         var localMessageService = new LocalMessageService();
         var nonce           = localMessageService.CreateNonce();
         var encryptedToSelf = localMessageService.EncryptTextTo(_walletRepository.NxtAccountWithPublicKey.PublicKey,
                                                                 NoteToSelfMessage, nonce, true, _walletRepository.SecretPhrase);
         var encryptedMessageToSelf = new CreateTransactionParameters.AlreadyEncryptedMessageToSelf(encryptedToSelf, nonce, true, true);
         return(encryptedMessageToSelf);
     }
     return(null);
 }
Exemplo n.º 5
0
 private CreateTransactionParameters.AlreadyEncryptedMessage GetEncryptedMessage()
 {
     if ((IsMessageEncryptionEnabled && EncryptMessage.Value) && !string.IsNullOrEmpty(Message))
     {
         var localMessageService = new LocalMessageService();
         var nonce     = localMessageService.CreateNonce();
         var encrypted = localMessageService.EncryptTextTo(_recipientPublicKey, Message, nonce,
                                                           true, _walletRepository.SecretPhrase);
         var encryptedMessage = new CreateTransactionParameters.AlreadyEncryptedMessage(encrypted, nonce, true, true, true);
         return(encryptedMessage);
     }
     return(null);
 }
Exemplo n.º 6
0
        private async Task UpdateEncryptedMessage(List <LedgerEntry> ledgerEntries)
        {
            foreach (var ledgerEntry in ledgerEntries.Where(e => e?.Transaction?.EncryptedMessage != null && e.UserIsSender))
            {
                try
                {
                    var messageService      = new LocalMessageService();
                    var encryptedMessage    = ledgerEntry.Transaction.EncryptedMessage;
                    var recipienctPublicKey = await GetAccountPublicKeyAsync(ledgerEntry.AccountTo);

                    var sharedKey   = messageService.GetSharedKey(recipienctPublicKey, encryptedMessage.Nonce, _walletRepository.SecretPhrase);
                    var unencrypted = messageService.DecryptText(encryptedMessage.Data, encryptedMessage.Nonce, encryptedMessage.IsCompressed, sharedKey);
                    ledgerEntry.EncryptedMessage = unencrypted;
                    ledgerEntry.UpdateOverviewMessage();
                }
                catch (Exception e)
                {
                    ledgerEntry.EncryptedMessage = "Unable to decrypt message";
                }
            }
        }
Exemplo n.º 7
0
        public static void Main()
        {
            var        localMessageService     = new LocalMessageService();
            var        localTransactionService = new LocalTransactionService();
            var        messageService          = new MessageService();
            var        transactionService      = new TransactionService();
            const bool useCompression          = true;

            // Encrypt message to send locally
            const string message   = "Sending a permanent message";
            var          nonce     = localMessageService.CreateNonce();
            var          encrypted = localMessageService.EncryptTextTo(RecipientPublicKey, message, nonce, useCompression, SecretPhrase);

            // Encrypt message to self locally
            const string messageToSelf   = "Note to self: sending a permanent message";
            var          nonceToSelf     = localMessageService.CreateNonce();
            var          encryptedToSelf = localMessageService.EncryptTextTo(SenderPublicKey, messageToSelf, nonceToSelf, useCompression, SecretPhrase);

            // Prepare the transaction with your public key
            var parameters = new CreateTransactionByPublicKey(1440, Amount.Zero, SenderPublicKey)
            {
                EncryptedMessage       = new CreateTransactionParameters.AlreadyEncryptedMessage(encrypted, nonce, true, useCompression),
                EncryptedMessageToSelf = new CreateTransactionParameters.AlreadyEncryptedMessageToSelf(encryptedToSelf, nonceToSelf, true, useCompression)
            };
            var unsigned = messageService.SendMessage(parameters, Recipient).Result;

            // Verify the unsigned transaction bytes from the node (only needed if you cannot trust the node)
            localTransactionService.VerifySendMessageTransactionBytes(unsigned, parameters, Recipient);

            // Sign and broadcast
            var signed        = localTransactionService.SignTransaction(unsigned, SecretPhrase);
            var result        = transactionService.BroadcastTransaction(new TransactionParameter(signed.ToString())).Result;
            var transactionId = result.TransactionId;

            Console.WriteLine($"Sent transaction: {transactionId}");

            Console.ReadLine();
        }