コード例 #1
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);
        }
コード例 #2
0
        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);
            }
        }