Exemple #1
0
 private void SendEncryptedLengthPrefixOnlyTcp(Socket recipient, byte[] encryptionKey, Action <BitBuffer> payloadWriter)
 {
     byte[] encrypted;
     using (_sendBuffer) {
         payloadWriter(_sendBuffer);
         encrypted = _crypto.Encrypt(encryptionKey, _sendBuffer.Array, 0, _sendBuffer.Size);
     }
     using (_sendBuffer) {
         _sendBuffer.Write((ushort)encrypted.Length);
         _sendBuffer.Write(encrypted);
         _tcp.Send(recipient, _sendBuffer.Array, 0, _sendBuffer.Size);
     }
 }
Exemple #2
0
        public void TestAnyKeyCrypto()
        {
            Random       random = new Random();
            AnyKeyCrypto crypto = new AnyKeyCrypto();

            byte[] key = new byte[KeyLength];

            for (int length = 0; length < MaxDataLength; length++)
            {
                random.NextBytes(key);
                byte[] original = new byte[length];

                for (int i = 0; i < DataPerKeyCount; i++)
                {
                    random.NextBytes(original);
                    byte[] encrypted = crypto.Encrypt(key, original, 0, original.Length);
                    byte[] decrypted = crypto.Decrypt(key, encrypted, 0, encrypted.Length);
                    CollectionAssert.AreEqual(original, decrypted);
                }
            }
        }