public bool MessageCallback([FromForm] ThreemaMessageCallback threemaMessageCallback)
        {
            try
            {
                try
                {
                    _log.LogDebug("ThreemaController.MessageCallback: ModelState {0}, ThreemaMessageCallback {1}", ModelState.IsValid, threemaMessageCallback.ToString());
                }
                catch (Exception ex)
                {
                    _log.LogError("ThreemaController.MessageCallback 1 " + ex.Message);
                }

                string nonce = threemaMessageCallback.Nonce;
                string box   = threemaMessageCallback.Box;

                string callbackPublicKey = GetPublicKey(threemaMessageCallback.From);

                string myPrivateKey = this._configuration["Threema:PrivateKey"];
                Key    privateKey   = Key.DecodeKey(myPrivateKey);
                Key    publicKey    = Key.DecodeKey(callbackPublicKey);

                ThreemaMessage message = CryptTool.DecryptMessage(
                    DataUtils.HexStringToByteArray(box),
                    privateKey.key,
                    publicKey.key,
                    DataUtils.HexStringToByteArray(nonce)
                    );

                switch (message.GetTypeCode())
                {
                case TextMessage.TYPE_CODE:
                    _log.LogInformation("ThreemaController.MessageCallback TextMessage from {0} to {1}: {2}", threemaMessageCallback.From, threemaMessageCallback.To, message.ToString());
                    break;

                //case DeliveryReceipt.TYPE_CODE:
                //class DeliveryReceipt statt public class DeliveryReceipt
                case 0x80:
                    _log.LogInformation("ThreemaController.MessageCallback DeliveryReceipt from {0} to {1}: {2}", threemaMessageCallback.From, threemaMessageCallback.To, message.ToString());
                    break;

                default:
                    _log.LogInformation("ThreemaController.MessageCallback ThreemaMessage? from {0} to {1}: {2}", threemaMessageCallback.From, threemaMessageCallback.To, message.ToString());
                    break;
                }

                return(true);
            }
            catch (Exception ex)
            {
                _log.LogError("ThreemaController.MessageCallback 2 " + ex.Message);
                return(false);
            }
        }
        protected override void Execute()
        {
            byte[] privateKey = this.privateKeyField.GetValue();
            byte[] publicKey  = this.publicKeyField.GetValue();
            byte[] nonce      = this.nonceField.GetValue();

            /* read box from stdin */
            //byte[] box = DataUtils.HexStringToByteArray(ReadStream(System.Console.In));
            byte[] box = DataUtils.HexStringToByteArray(DataUtils.Utf8Endcode(System.Console.ReadLine().Trim()));

            ThreemaMessage message = CryptTool.DecryptMessage(box, privateKey, publicKey, nonce);

            System.Console.WriteLine(message);
        }
예제 #3
0
        /// <summary>
        /// Wrapper to decrypt box <see cref="Threema.MsgApi.CryptTool.DecryptMessage"/>
        /// </summary>
        /// <param name="box">Encrypted box as hex-straing</param>
        /// <param name="recipientPrivateKey">Recipient private key as hex-string</param>
        /// <param name="senderPublicKey">Sender public key as hex-string</param>
        /// <param name="nonce">Nonce as hex-string</param>
        /// <returns>Array with type and decrypted message</returns>
        public ArrayList DecryptMessage(string box, string recipientPrivateKey, string senderPublicKey, string nonce)
        {
            byte[] privateKey = GetKey(recipientPrivateKey, Key.KeyType.PRIVATE);
            byte[] publicKey  = GetKey(senderPublicKey, Key.KeyType.PUBLIC);
            byte[] nonceBytes = DataUtils.HexStringToByteArray(nonce);
            byte[] boxBytes   = DataUtils.HexStringToByteArray(box);

            ThreemaMessage message = CryptTool.DecryptMessage(boxBytes, privateKey, publicKey, nonceBytes);

            var result = new ArrayList();

            result.Add(message.GetTypeCode().ToString());
            result.Add(message.ToString());
            return(result);
        }
예제 #4
0
        public void TestDecrypt()
        {
            string nonce = "0a1ec5b67b4d61a1ef91f55e8ce0471fee96ea5d8596dfd0";
            string box   = "45181c7aed95a1c100b1b559116c61b43ce15d04014a805288b7d14bf3a993393264fe554794ce7d6007233e8ef5a0f1ccdd704f34e7c7b77c72c239182caf1d061d6fff6ffbbfe8d3b8f3475c2fe352e563aa60290c666b2e627761e32155e62f048b52ef2f39c13ac229f393c67811749467396ecd09f42d32a4eb419117d0451056ac18fac957c52b0cca67568e2d97e5a3fd829a77f914a1ad403c5909fd510a313033422ea5db71eaf43d483238612a54cb1ecfe55259b1de5579e67c6505df7d674d34a737edf721ea69d15b567bc2195ec67e172f3cb8d6842ca88c29138cc33e9351dbc1e4973a82e1cf428c1c763bb8f3eb57770f914a";

            Key privateKey = Key.DecodeKey(Common.otherPrivateKey);
            Key publicKey  = Key.DecodeKey(Common.myPublicKey);

            ThreemaMessage message = CryptTool.DecryptMessage(
                DataUtils.HexStringToByteArray(box),
                privateKey.key,
                publicKey.key,
                DataUtils.HexStringToByteArray(nonce)
                );

            Assert.IsNotNull(message);
            Assert.IsInstanceOfType(message, typeof(TextMessage), "message is not a instance of text message");
            Assert.AreEqual(((TextMessage)message).Text, "Dies ist eine Testnachricht. äöü");
        }
예제 #5
0
        /// <summary>
        /// Decrypt a Message and download the blobs of the Message (e.g. image or file)
        /// </summary>
        /// <param name="threemaId">Threema ID of the sender</param>
        /// <param name="messageId">Message ID</param>
        /// <param name="box">Encrypted box data of the file/image message</param>
        /// <param name="nonce">Nonce that was used for message encryption</param>
        /// <param name="outputFolder">Output folder for storing decrypted images/files</param>
        /// <returns>Result of message reception</returns>
        public ReceiveMessageResult ReceiveMessage(string threemaId, string messageId, byte[] box, byte[] nonce, string outputFolder)
        {
            //fetch public key
            byte[] publicKey = this.apiConnector.LookupKey(threemaId);

            if (publicKey == null)
            {
                throw new InvalidKeyException("invalid threema id");
            }

            ThreemaMessage message = CryptTool.DecryptMessage(box, this.privateKey, publicKey, nonce);

            if (message == null)
            {
                return(null);
            }

            ReceiveMessageResult result = new ReceiveMessageResult(messageId, message);

            if (message.GetType() == typeof(ImageMessage))
            {
                //download image
                ImageMessage imageMessage = (ImageMessage)message;
                byte[]       fileData     = this.apiConnector.DownloadFile(imageMessage.BlobId);

                if (fileData == null)
                {
                    throw new MessageParseException();
                }

                byte[]   decryptedFileContent = CryptTool.Decrypt(fileData, privateKey, publicKey, imageMessage.Nonce);
                FileInfo imageFile            = new FileInfo(outputFolder + "/" + messageId + ".jpg");

                using (FileStream stream = File.OpenWrite(imageFile.FullName))
                {
                    stream.Write(decryptedFileContent, 0, decryptedFileContent.Length);
                    stream.Close();
                }

                result.Files.Add(imageFile);
            }
            else if (message.GetType() == typeof(FileMessage))
            {
                //download file
                FileMessage fileMessage = (FileMessage)message;
                byte[]      fileData    = this.apiConnector.DownloadFile(fileMessage.BlobId);

                byte[]   decryptedFileData = CryptTool.DecryptFileData(fileData, fileMessage.EncryptionKey);
                FileInfo file = new FileInfo(outputFolder + "/" + messageId + "-" + fileMessage.FileName);

                using (FileStream stream = File.OpenWrite(file.FullName))
                {
                    stream.Write(decryptedFileData, 0, decryptedFileData.Length);
                    stream.Close();
                }

                result.Files.Add(file);

                if (fileMessage.ThumbnailBlobId != null)
                {
                    byte[] thumbnailData = this.apiConnector.DownloadFile(fileMessage.ThumbnailBlobId);

                    byte[]   decryptedThumbnailData = CryptTool.DecryptFileThumbnailData(thumbnailData, fileMessage.EncryptionKey);
                    FileInfo thumbnailFile          = new FileInfo(outputFolder + "/" + messageId + "-thumbnail.jpg");
                    using (FileStream stream = File.OpenWrite(thumbnailFile.FullName))
                    {
                        stream.Write(decryptedThumbnailData, 0, decryptedThumbnailData.Length);
                        stream.Close();
                    }

                    result.Files.Add(thumbnailFile);
                }
            }

            return(result);
        }