コード例 #1
0
        /// <summary>
        /// Encrypt a text message and send it to the given recipient.
        /// </summary>
        /// <param name="threemaId">target Threema ID</param>
        /// <param name="text">the text to send</param>
        /// <returns>generated message ID</returns>
        public string SendTextMessage(string threemaId, string text)
        {
            //fetch public key
            byte[] publicKey = this.apiConnector.LookupKey(threemaId);

            if (publicKey == null)
            {
                throw new InvalidKeyException("invalid threema id");
            }
            EncryptResult res = CryptTool.EncryptTextMessage(text, this.privateKey, publicKey);

            return(this.apiConnector.SendE2EMessage(threemaId, res.Nonce, res.Result));
        }
コード例 #2
0
        protected override void Execute()
        {
            byte[] privateKey = this.privateKeyField.GetValue();
            byte[] publicKey  = this.publicKeyField.GetValue();

            /* read text from stdin */
            //string text = ReadStream(System.Console.In).Trim();
            string text = DataUtils.Utf8Endcode(System.Console.ReadLine().Trim());

            EncryptResult res = CryptTool.EncryptTextMessage(text, privateKey, publicKey);

            System.Console.WriteLine(DataUtils.ByteArrayToHexString(res.Nonce));
            System.Console.WriteLine(DataUtils.ByteArrayToHexString(res.Result));
        }
コード例 #3
0
        public void TestEncrypt()
        {
            string text = "Dies ist eine Testnachricht. äöü";

            Key privateKey = Key.DecodeKey(Common.myPrivateKey);
            Key publicKey  = Key.DecodeKey(Common.otherPublicKey);

            EncryptResult res = CryptTool.EncryptTextMessage(text, privateKey.key, publicKey.key);

            Assert.IsNotNull(res);
            Assert.IsNotNull(res.Nonce);
            Assert.IsNotNull(res.Result);
            Assert.IsFalse(res.Nonce.SequenceEqual(new byte[res.Nonce.Length]));
            Assert.IsFalse(res.Result.SequenceEqual(new byte[res.Result.Length]));
        }
コード例 #4
0
        /// <summary>
        /// Wrapper to encrypt text <see cref="Threema.MsgApi.CryptTool.EncryptTextMessage"/>
        /// </summary>
        /// <param name="text">Text to encrypt</param>
        /// <param name="senderPrivateKey">Sender private key as hex-string</param>
        /// <param name="recipientPublicKey">Recipient public key as hex-string</param>
        /// <returns>Array with encrypted text, nonce and size</returns>
        public ArrayList EncryptTextMessage(string text, string senderPrivateKey, string recipientPublicKey)
        {
            byte[] privateKey = GetKey(senderPrivateKey, Key.KeyType.PRIVATE);
            byte[] publicKey  = GetKey(recipientPublicKey, Key.KeyType.PUBLIC);

            string textEncoded = DataUtils.Utf8Endcode(text);

            EncryptResult encryptResult = CryptTool.EncryptTextMessage(textEncoded, privateKey, publicKey);

            var result = new ArrayList();

            result.Add(DataUtils.ByteArrayToHexString(encryptResult.Result));
            result.Add(DataUtils.ByteArrayToHexString(encryptResult.Nonce));
            result.Add(encryptResult.Size.ToString());
            return(result);
        }