Exemple #1
0
        public EncryptedMessage Encrypt(byte[] input)
        {
            var em = new EncryptedMessage();

            using (var aes = new AesManaged())
            {
                aes.KeySize = 256;

                aes.GenerateIV();
                aes.GenerateKey();

                em.IV             = aes.IV;
                em.Key            = aes.Key;
                em.EncryptionType = MessageEncryptionType.Aes;

                using (var encryptor = aes.CreateEncryptor())
                {
                    using (var msOutput = new MemoryStream())
                    {
                        using (var cryptoStream = new CryptoStream(msOutput, encryptor, CryptoStreamMode.Write)) {
                            using (var msInput = new MemoryStream(input)) {
                                msInput.CopyTo(cryptoStream);
                            }
                        }
                        em.CipherBytes = msOutput.ToArray();
                    }
                }

                return(em);
            }
        }
        public static EncryptedMessage FromStream(Stream stream)
        {
            var em = new EncryptedMessage();

            byte[] header = new byte[_headerSize];
            stream.Read(header, 0, _headerSize);

            MessageEncryptionType encryptionType = (MessageEncryptionType)header[0];
            KeyEncryptionType     keyType        = (KeyEncryptionType)header[1];

            int keySize = ByteLengthFromPoSize(header[2]);
            int ivSize  = header[3];

            // read dataSize from header using a offset of 2
            ushort dataSize = BitConverter.ToUInt16(header, 4);

            byte[] iv = new byte[ivSize];
            stream.Read(iv, 0, ivSize);

            byte[] key = new byte[keySize];
            stream.Read(key, 0, keySize);

            byte[] data = new byte[dataSize];
            stream.Read(data, 0, dataSize);

            em.IV             = iv;
            em.Key            = key;
            em.CipherBytes    = data;
            em.KeyType        = keyType;
            em.EncryptionType = encryptionType;

            return(em);
        }
Exemple #3
0
        public EncryptedMessage Encrypt(byte[] input)
        {
            var em = new EncryptedMessage();
            using (var aes = new AesManaged())
            {
                aes.KeySize = 256;

                aes.GenerateIV();
                aes.GenerateKey();

                em.IV = aes.IV;
                em.Key = aes.Key;
                em.EncryptionType = MessageEncryptionType.Aes;

                using (var encryptor = aes.CreateEncryptor())
                {
                    using (var msOutput = new MemoryStream())
                    {
                        using (var cryptoStream = new CryptoStream(msOutput, encryptor, CryptoStreamMode.Write)) {
                            using (var msInput = new MemoryStream(input)) {
                                msInput.CopyTo(cryptoStream);
                            }
                        }
                        em.CipherBytes = msOutput.ToArray();
                    }
                }

                return em;
            }
        }
        public static EncryptedMessage FromStream(Stream stream)
        {
            var em = new EncryptedMessage();
            byte[] header = new byte[_headerSize];
            stream.Read(header, 0, _headerSize);

            MessageEncryptionType encryptionType = (MessageEncryptionType)header[0];
            KeyEncryptionType keyType = (KeyEncryptionType)header[1];

            int keySize = ByteLengthFromPoSize(header[2]);
            int ivSize = header[3];

            // read dataSize from header using a offset of 2
            ushort dataSize = BitConverter.ToUInt16(header, 4);

            byte[] iv = new byte[ivSize];
            stream.Read(iv, 0, ivSize);

            byte[] key = new byte[keySize];
            stream.Read(key, 0, keySize);

            byte[] data = new byte[dataSize];
            stream.Read(data, 0, dataSize);

            em.IV = iv;
            em.Key = key;
            em.CipherBytes = data;
            em.KeyType = keyType;
            em.EncryptionType = encryptionType;

            return em;
        }
Exemple #5
0
 public EncryptedMessage Encrypt(byte[] input)
 {
     var em = new EncryptedMessage();
     em.EncryptionType = MessageEncryptionType.None;
     em.IV = new byte[0];
     em.Key = new byte[0];
     em.CipherBytes = input;
     return em;
 }
        public EncryptedMessage Encrypt(byte[] input)
        {
            var em = new EncryptedMessage();

            em.EncryptionType = MessageEncryptionType.None;
            em.IV             = new byte[0];
            em.Key            = new byte[0];
            em.CipherBytes    = input;
            return(em);
        }
Exemple #7
0
        protected string decryptMessage(EncryptedMessage em)
        {
            var me = getMessageEncryption(em);
            var ke = getKeyEncryption(em);
            em.Key = ke.DecryptData(em.Key, KeyStore?.PrivateKey?.Key);

            var bytes = me.Decrypt(em);

            return Encoding.UTF8.GetString(bytes);
        }
Exemple #8
0
 private IKeyEncryption getKeyEncryption(EncryptedMessage em)
 {
     switch (em.KeyType)
     {
         case KeyEncryptionType.Rsa:
             return RSA.Default;
         default:
         case KeyEncryptionType.None:
             return NoKey.Default;
     }
 }
Exemple #9
0
        public byte[] Decrypt(EncryptedMessage em)
        {
            using (var aes = new AesManaged())
            {
                aes.Key = em.Key;
                aes.IV  = em.IV;

                using (var decryptor = aes.CreateDecryptor())
                {
                    using (var msInput = new MemoryStream(em.CipherBytes))
                    {
                        using (var cryptoStream = new CryptoStream(msInput, decryptor, CryptoStreamMode.Read))
                        {
                            using (var msOutput = new MemoryStream())
                            {
                                cryptoStream.CopyTo(msOutput);
                                return(msOutput.ToArray());
                            }
                        }
                    }
                }
            }
        }
Exemple #10
0
        public byte[] Decrypt(EncryptedMessage em)
        {
            using (var aes = new AesManaged())
            {
                aes.Key = em.Key;
                aes.IV = em.IV;

                using (var decryptor = aes.CreateDecryptor())
                {
                    using (var msInput = new MemoryStream(em.CipherBytes))
                    {
                        using (var cryptoStream = new CryptoStream(msInput, decryptor, CryptoStreamMode.Read))
                        {
                            using (var msOutput = new MemoryStream())
                            {
                                cryptoStream.CopyTo(msOutput);
                                return msOutput.ToArray();
                            }
                        }
                    }
                }
            }
        }
Exemple #11
0
 public byte[] Decrypt(EncryptedMessage em)
 {
     return em.CipherBytes;
 }
Exemple #12
0
        protected override void encryptKey(ref EncryptedMessage em, Guid remoteId)
        {
            if (KeyEncryption is NoKey) return;

            em.Key = KeyEncryption.EncryptData(em.Key, HubPublicKey?.Key);
            em.KeyType = KeyEncryption.Type;
        }
Exemple #13
0
        protected override void encryptKey(ref EncryptedMessage em, Guid remoteId)
        {
            if (KeyEncryption is NoKey) return;

            if (!NodesPublicKeys.ContainsKey(remoteId))
                throw new Exception(String.Format("No RSA public key for node \"{0}\" found!", remoteId.ToString()));

            em.Key = KeyEncryption.EncryptData(em.Key, NodesPublicKeys[remoteId].Key);
            em.KeyType = KeyEncryption.Type;

        }
 public byte[] Decrypt(EncryptedMessage em)
 {
     return(em.CipherBytes);
 }
Exemple #15
0
 private IMessageEncryption getMessageEncryption(EncryptedMessage em)
 {
     switch(em.EncryptionType)
     {
         case MessageEncryptionType.Aes:
             return Rijndael.Default;
         default:
         case MessageEncryptionType.None:
             return PlainText.Default;
     }
 }
Exemple #16
0
 protected abstract void encryptKey(ref EncryptedMessage em, Guid remoteId);