Пример #1
0
        private static EncryptResult EncryptMessage(ThreemaMessage threemaMessage, byte[] privateKey, byte[] publicKey)
        {
            // determine random amount of PKCS7 padding
            int padbytes = new Random().Next(254) + 1;

            byte[] messageBytes;
            try
            {
                messageBytes = threemaMessage.GetData();
            }
            catch
            {
                return(null);
            }

            // prepend type byte (0x02) to message data
            byte[] data = new byte[1 + messageBytes.Length + padbytes];
            data[0] = (byte)threemaMessage.GetTypeCode();

            messageBytes.CopyTo(data, 1);

            // append padding
            for (int i = 0; i < padbytes; i++)
            {
                data[i + 1 + messageBytes.Length] = (byte)padbytes;
            }

            return(Encrypt(data, privateKey, publicKey));
        }
        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);
            }
        }
Пример #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);
        }