Beispiel #1
0
        public static String EncryptToHexString(this AesManaged aes, String source, String key)
        {
            var bkey = Settings.KeyEncoding.GetBytes(key);
            var bb   = aes.Encrypt(Settings.Encoding.GetBytes(source), bkey, bkey);

            return(ByteToHexString(bb));
        }
Beispiel #2
0
        internal async Task _Sender()
        {
            bool _Dequeue(out byte[] buf)
            {
                lock (_loc)
                {
                    if (_msglen > Links.BufferQueueLimit)
                    {
                        throw new LinkException(LinkError.QueueLimited);
                    }
                    if (_msglen > 0)
                    {
                        buf      = _msgs.Dequeue();
                        _msglen -= buf.Length;
                        return(true);
                    }
                }

                buf = null;
                return(false);
            }

            while (_cancel.IsCancellationRequested == false)
            {
                if (_Dequeue(out var buf))
                {
                    await _socket.SendAsyncExt(_aes.Encrypt(buf));
                }
                else
                {
                    await Task.Delay(Links.Delay);
                }
                continue;
            }
        }
        public void TestAesAlgorithm()
        {
            var provider = new AesManaged();

            provider.Key     = new Byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
            provider.IV      = new Byte[] { 0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
            provider.Padding = PaddingMode.Zeros;
            provider.Mode    = CipherMode.CBC;

            var encodedData = provider.Encrypt(_clearData);
            var decodedData = provider.Decrypt(encodedData);

            CollectionAssert.AreEqual(_clearData, decodedData);
        }
 public static byte[] EncryptUseAes(this byte[] bytes, byte[] key, byte[] iv,
                                    CipherMode mode     = CipherMode.CBC,
                                    PaddingMode padding = PaddingMode.PKCS7)
 {
     using (var aes = new AesManaged()
     {
         IV = iv,
         Key = key,
         Mode = mode,
         Padding = padding
     })
     {
         return(aes.Encrypt(bytes));
     }
 }
 public static byte[] EncryptUseAes(this byte[] bytes, byte[] key, byte[] iv,
     CipherMode mode = CipherMode.CBC,
     PaddingMode padding = PaddingMode.PKCS7)
 {
     using (var aes = new AesManaged()
     {
         IV = iv,
         Key = key,
         Mode = mode,
         Padding = padding
     })
     {
         return aes.Encrypt(bytes);
     }
 }
Beispiel #6
0
 private void m_encoderUDP_NewPacket(byte[] data, int position, int length)
 {
     // Encrypt payload
     if ((object)m_key != null)
     {
         using (AesManaged aes = new AesManaged())
         {
             aes.KeySize = 256;
             aes.Key     = m_key;
             aes.IV      = m_iv;
             data        = aes.Encrypt(data, position, data.Length, m_key, m_iv);
             position    = 0;
             length      = data.Length;
         }
     }
     m_udpSocket.Send(data, position, length);
 }
Beispiel #7
0
        public static void EncryptDecryptKnownECB192()
        {
            byte[] plainTextBytes =
                new ASCIIEncoding().GetBytes("This is a sentence that is longer than a block, it ensures that multi-block functions work.");

            byte[] encryptedBytesExpected = new byte[]
            {
                0xC9, 0x7F, 0xA5, 0x5B, 0xC3, 0x92, 0xDC, 0xA6,
                0xE4, 0x9F, 0x2D, 0x1A, 0xEF, 0x7A, 0x27, 0x03,
                0x04, 0x9C, 0xFB, 0x56, 0x63, 0x38, 0xAE, 0x4F,
                0xDC, 0xF6, 0x36, 0x98, 0x28, 0x05, 0x32, 0xE9,
                0xF2, 0x6E, 0xEC, 0x0C, 0x04, 0x9D, 0x12, 0x17,
                0x18, 0x35, 0xD4, 0x29, 0xFC, 0x01, 0xB1, 0x20,
                0xFA, 0x30, 0xAE, 0x00, 0x53, 0xD4, 0x26, 0x25,
                0xA4, 0xFD, 0xD5, 0xE6, 0xED, 0x79, 0x35, 0x2A,
                0xE2, 0xBB, 0x95, 0x0D, 0xEF, 0x09, 0xBB, 0x6D,
                0xC5, 0xC4, 0xDB, 0x28, 0xC6, 0xF4, 0x31, 0x33,
                0x9A, 0x90, 0x12, 0x36, 0x50, 0xA0, 0xB7, 0xD1,
                0x35, 0xC4, 0xCE, 0x81, 0xE5, 0x2B, 0x85, 0x6B,
            };

            byte[] aes192Key = new byte[]
            {
                0xA6, 0x1E, 0xC7, 0x54, 0x37, 0x4D, 0x8C, 0xA5,
                0xA4, 0xBB, 0x99, 0x50, 0x35, 0x4B, 0x30, 0x4D,
                0x6C, 0xFE, 0x3B, 0x59, 0x65, 0xCB, 0x93, 0xE3,
            };

            using (var alg = new AesManaged())
            {
                // The CipherMode and KeySize are different than the default values; this ensures the type
                // forwards the state properly to Aes.
                alg.Mode = CipherMode.ECB;
                alg.Key  = aes192Key;

                byte[] encryptedBytes = alg.Encrypt(plainTextBytes);
                Assert.Equal(encryptedBytesExpected, encryptedBytes);

                byte[] decryptedBytes = alg.Decrypt(encryptedBytes);
                Assert.Equal(plainTextBytes, decryptedBytes);
            }
        }
Beispiel #8
0
        public static void EncryptDecryptKnownECB192()
        {
            byte[] plainTextBytes =
                new ASCIIEncoding().GetBytes("This is a sentence that is longer than a block, it ensures that multi-block functions work.");

            byte[] encryptedBytesExpected = new byte[]
            {
                0xC9, 0x7F, 0xA5, 0x5B, 0xC3, 0x92, 0xDC, 0xA6,
                0xE4, 0x9F, 0x2D, 0x1A, 0xEF, 0x7A, 0x27, 0x03,
                0x04, 0x9C, 0xFB, 0x56, 0x63, 0x38, 0xAE, 0x4F,
                0xDC, 0xF6, 0x36, 0x98, 0x28, 0x05, 0x32, 0xE9,
                0xF2, 0x6E, 0xEC, 0x0C, 0x04, 0x9D, 0x12, 0x17,
                0x18, 0x35, 0xD4, 0x29, 0xFC, 0x01, 0xB1, 0x20,
                0xFA, 0x30, 0xAE, 0x00, 0x53, 0xD4, 0x26, 0x25,
                0xA4, 0xFD, 0xD5, 0xE6, 0xED, 0x79, 0x35, 0x2A,
                0xE2, 0xBB, 0x95, 0x0D, 0xEF, 0x09, 0xBB, 0x6D,
                0xC5, 0xC4, 0xDB, 0x28, 0xC6, 0xF4, 0x31, 0x33,
                0x9A, 0x90, 0x12, 0x36, 0x50, 0xA0, 0xB7, 0xD1,
                0x35, 0xC4, 0xCE, 0x81, 0xE5, 0x2B, 0x85, 0x6B,
            };

            byte[] aes192Key = new byte[]
            {
                0xA6, 0x1E, 0xC7, 0x54, 0x37, 0x4D, 0x8C, 0xA5,
                0xA4, 0xBB, 0x99, 0x50, 0x35, 0x4B, 0x30, 0x4D,
                0x6C, 0xFE, 0x3B, 0x59, 0x65, 0xCB, 0x93, 0xE3,
            };

            using (var alg = new AesManaged())
            {
                // The CipherMode and KeySize are different than the default values; this ensures the type
                // forwards the state properly to Aes.
                alg.Mode = CipherMode.ECB;
                alg.Key = aes192Key;

                byte[] encryptedBytes = alg.Encrypt(plainTextBytes);
                Assert.Equal(encryptedBytesExpected, encryptedBytes);

                byte[] decryptedBytes = alg.Decrypt(encryptedBytes);
                Assert.Equal(plainTextBytes, decryptedBytes);
            }
        }