public void Decrypt_AES_128_CTR()
        {
            var input      = new byte[] { 0x00, 0x00, 0x00, 0x2c, 0x1a, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x73, 0x68, 0x2d, 0x75, 0x73, 0x65, 0x72, 0x61, 0x75, 0x74, 0x68, 0xb0, 0x74, 0x21, 0x87, 0x16, 0xb9, 0x69, 0x48, 0x33, 0xce, 0xb3, 0xe7, 0xdc, 0x3f, 0x50, 0xdc, 0xcc, 0xd5, 0x27, 0xb7, 0xfe, 0x7a, 0x78, 0x22, 0xae, 0xc8 };
            var key        = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
            var iv         = new byte[] { 0xe6, 0x65, 0x36, 0x0d, 0xdd, 0xd7, 0x50, 0xc3, 0x48, 0xdb, 0x48, 0x07, 0xa1, 0x30, 0xd2, 0x38 };
            var output     = new byte[] { 0xca, 0xfb, 0x1c, 0x49, 0xbf, 0x82, 0x2a, 0xbb, 0x1c, 0x52, 0xc7, 0x86, 0x22, 0x8a, 0xe5, 0xa4, 0xf3, 0xda, 0x4e, 0x1c, 0x3a, 0x87, 0x41, 0x1c, 0xd2, 0x6e, 0x76, 0xdc, 0xc2, 0xe9, 0xc2, 0x0e, 0xf5, 0xc7, 0xbd, 0x12, 0x85, 0xfa, 0x0e, 0xda, 0xee, 0x50, 0xd7, 0xfd, 0x81, 0x34, 0x25, 0x6d };
            var testCipher = new AesCipher(key, new CtrCipherMode(iv), null);

            var actual = testCipher.Decrypt(output);

            Assert.IsTrue(input.IsEqualTo(actual));
        }
        public bool Decrypt(EncryptedMessage encryptedMessage, int senderId, out string messageText)
        {
            if (encryptedMessage == null)
            {
                throw new ArgumentException("Encrypted message cannot be null");
            }

            if (encryptedMessage.Body == null || encryptedMessage.DigitalSignature == null ||
                encryptedMessage.SymmetricKey == null || encryptedMessage.Iv == null)
            {
                throw new ArgumentException("Not all encrypted message fields are initialized");
            }

            IContactModel senderContact = _storageService.GetContacts().FirstOrDefault(c => c.Id == senderId);

            if (senderContact == null)
            {
                throw new ArgumentException("Contact with id of senderId does not exist");
            }

            string receiverKeyPair = _storageService.GetUser().KeyPair;
            string senderPublicKey = senderContact.PublicKey;

            try
            {
                // decrypt symmetric key with receivers private key
                RsaCipher rsa = new RsaCipher(receiverKeyPair);
                byte[]    encryptedSymmetricKeyBytes = FormatConverter.String64ToBytes(encryptedMessage.SymmetricKey);
                byte[]    decryptedSymmetricKeyBytes = rsa.Decrypt(encryptedSymmetricKeyBytes);

                // decrypt message text with jsut decrypted symmetric key
                byte[]    ivBytes = FormatConverter.String64ToBytes(encryptedMessage.Iv);
                AesCipher aes     = new AesCipher(decryptedSymmetricKeyBytes, ivBytes);
                byte[]    encryptedMessageBytes = FormatConverter.String64ToBytes(encryptedMessage.Body);
                byte[]    decryptedMessageBytes = aes.Decrypt(encryptedMessageBytes);

                // set message text out parameter
                messageText = FormatConverter.BytesToString(decryptedMessageBytes);

                // verify digital signature
                rsa = new RsaCipher(senderPublicKey);
                byte[] digitalSignatureBytes = FormatConverter.String64ToBytes(encryptedMessage.DigitalSignature);
                bool   signatureOk           = rsa.VerifyDigitalSignature(decryptedMessageBytes, digitalSignatureBytes);

                return(signatureOk);
            }
            catch (Exception ex)
            {
                messageText = null;
                return(false);
            }
        }
        protected IDictionary <string, object> LoadFromFile(string filePath, ConfigurationFileInfo configFileInfo)
        {
            IDictionary <string, object> result = null;
            string text = null;

            if (configFileInfo.IsResourceFile)
            {
                TextAsset textAsset = Resources.Load(filePath, typeof(TextAsset)) as TextAsset;
                if (textAsset != null)
                {
                    text = textAsset.text;
                }
            }
            if (text == null)
            {
                text = File.ReadAllText(filePath);
            }
            string text2 = null;

            if (configFileInfo.IsEncrypted)
            {
                try
                {
                    text2 = AesCipher.Decrypt(text, configFileInfo.EncryptionKey);
                }
                catch (Exception)
                {
                }
            }
            if (text2 == null)
            {
                text2 = text;
            }
            if (text2 != null)
            {
                result = LPFJsonMapper.ToObjectSimple(text2) as IDictionary <string, object>;
            }
            return(result);
        }