示例#1
0
        public static bool DecryptTrackFile(StreamUrl stream, string filepath)
        {
            try
            {
                if (!System.IO.File.Exists(filepath))
                {
                    return(false);
                }
                if (stream.EncryptionKey.IsBlank())
                {
                    return(true);
                }

                byte[] security_token = System.Convert.FromBase64String(stream.EncryptionKey);

                byte[] iv  = security_token.Skip(0).Take(16).ToArray();
                byte[] str = security_token.Skip(16).ToArray();
                byte[] dec = AESHelper.Decrypt(str, MASTER_KEY, iv);

                byte[] key    = dec.Skip(0).Take(16).ToArray();
                byte[] nonce  = dec.Skip(16).Take(8).ToArray();
                byte[] nonce2 = new byte[16];
                nonce.CopyTo(nonce2, 0);

                byte[]  txt   = ReadFile(filepath);
                AES_CTR tool  = new AES_CTR(key, nonce2);
                byte[]  newt  = tool.DecryptBytes(txt);
                bool    bfalg = WriteFile(filepath, newt);
                return(bfalg);
            }
            catch
            {
                return(false);
            }
        }
        public OriginalVsAdjusted()
        {
            // Arrays for outputs
            this.outputForStackOverflow1 = new byte[dataLength1];
            this.outputForStackOverflow2 = new byte[dataLength2];
            this.outputForStackOverflow3 = new byte[dataLength3];

            this.outputForthisProject1 = new byte[dataLength1];
            this.outputForthisProject2 = new byte[dataLength2];
            this.outputForthisProject3 = new byte[dataLength3];

            // Generate inputs
            Random rng = new Random(Seed: 1337);

            this.data1 = new byte[dataLength1];
            rng.NextBytes(this.data1);

            this.data2 = new byte[dataLength2];
            rng.NextBytes(this.data2);

            this.data3 = new byte[dataLength3];
            rng.NextBytes(this.data3);

            // Set encrypters
            this.thisProject1 = new AES_CTR(key, initialCounter);
            this.thisProject2 = new AES_CTR(key, initialCounter);
            this.thisProject3 = new AES_CTR(key, initialCounter);
        }
        /// <summary>
        /// Encrypt given bytes with given key. Returns new array with encrypted bytes
        /// </summary>
        /// <param name="bytesToEncrypt">Byte array to encrypu</param>
        /// <param name="key"></param>
        /// <returns>Encrypted bytes in new array</returns>
        public byte[] EncryptBytes(byte[] bytesToEncrypt, byte[] key)
        {
            byte[] returnArray = new byte[bytesToEncrypt.Length];

            Enum.TryParse(this.algorithm, out SymmetricEncryptionAlgorithm actualAlgorithm);

            if (actualAlgorithm == SymmetricEncryptionAlgorithm.AES_CTR)
            {
                using (AES_CTR forEncrypting = new AES_CTR(key, this.settingsAES_CTR.initialCounter))
                {
                    forEncrypting.EncryptBytes(returnArray, bytesToEncrypt, bytesToEncrypt.Length);
                }
            }
            else if (actualAlgorithm == SymmetricEncryptionAlgorithm.ChaCha20)
            {
                using (ChaCha20 forEncrypting = new ChaCha20(key, this.settingsChaCha20.nonce, settingsChaCha20.counter))
                {
                    forEncrypting.EncryptBytes(returnArray, bytesToEncrypt, bytesToEncrypt.Length);
                }
            }
            else
            {
                throw new NotImplementedException();
            }

            return(returnArray);
        }
示例#4
0
        public static bool DecryptTrackFile(StreamUrl stream, string filepath)
        {
            if (!File.Exists(filepath))
            {
                return(false);
            }
            if (stream.EncryptionKey.IsBlank())
            {
                return(true);
            }

            byte[] master_key     = System.Convert.FromBase64String("UIlTTEMmmLfGowo/UC60x2H45W6MdGgTRfo/umg4754=");
            byte[] security_token = System.Convert.FromBase64String(stream.EncryptionKey);

            byte[] iv  = security_token.Skip(0).Take(16).ToArray();
            byte[] str = security_token.Skip(16).ToArray();
            byte[] dec = AESHelper.Decrypt(str, master_key, iv);

            byte[] key    = dec.Skip(0).Take(16).ToArray();
            byte[] nonce  = dec.Skip(16).Take(8).ToArray();
            byte[] nonce2 = new byte[16];
            nonce.CopyTo(nonce2, 0);

            byte[]  txt  = ReadFile(filepath);
            AES_CTR tool = new AES_CTR(key, nonce2);

            byte[] newt  = tool.DecryptBytes(txt);
            bool   bfalg = WriteFile(filepath, newt);

            return(bfalg);
        }
        public void TestDisposable()
        {
            // Arrange
            byte[] key            = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            byte[] initialCounter = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05 };

            byte[] content       = new byte[] { 11, 24, 22, 134, 234, 33, 4, 14, 34, 56, 23 };
            int    contentLength = content.Length;

            byte[] encrypted = new byte[contentLength];
            byte[] decrypted = new byte[contentLength];

            // Act
            using (AES_CTR forEncrypting = new AES_CTR(key, initialCounter))
            {
                forEncrypting.EncryptBytes(encrypted, content, contentLength);
            }

            using (AES_CTR forDecrypting = new AES_CTR(key, initialCounter))
            {
                forDecrypting.DecryptBytes(decrypted, encrypted, contentLength);
            }

            // Assert
            CollectionAssert.AreEqual(content, decrypted);
        }
        public async Task AsyncTestStreamEncryptDecryptNonPowerOfTwo()
        {
            // Arrange
            Random rng = new Random(Seed: 1339);

            byte[] key            = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            byte[] initialCounter = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05 };

            const int lengthOfData = 21111;

            byte[] randomContent = new byte[lengthOfData];

            byte[] encryptedContent1 = new byte[lengthOfData];
            byte[] decryptedContent1 = new byte[lengthOfData];

            AES_CTR forEncrypting1 = null;
            AES_CTR forDecrypting1 = null;

            // Act
            rng.NextBytes(randomContent);

            forEncrypting1 = new AES_CTR(key, initialCounter);
            forDecrypting1 = new AES_CTR(key, initialCounter);

            await forEncrypting1.EncryptStreamAsync(new MemoryStream(encryptedContent1), new MemoryStream(randomContent));

            await forDecrypting1.DecryptStreamAsync(new MemoryStream(decryptedContent1), new MemoryStream(encryptedContent1));

            // Assert
            CollectionAssert.AreEqual(randomContent, decryptedContent1);
            CollectionAssert.AreNotEqual(randomContent, encryptedContent1);
        }
示例#7
0
        public void FailedInputOrOutput()
        {
            // Arrange
            byte[] key = new byte[16] {
                0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
            };
            byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            const int lengthOfData = 128;

            byte[] validOutputArray = new byte[lengthOfData];
            byte[] validInputArray  = new byte[lengthOfData];

            byte[] invalidInput1  = null;
            byte[] invalidOutput1 = null;

            AES_CTR nullInput  = new AES_CTR(key, initialCounter);
            AES_CTR nullOutput = new AES_CTR(key, initialCounter);

            // Act

            // Assert
            Assert.That(() => nullInput.EncryptBytes(validOutputArray, invalidInput1, lengthOfData), Throws.ArgumentNullException);
            Assert.That(() => nullInput.EncryptBytes(invalidOutput1, validInputArray, lengthOfData), Throws.ArgumentNullException);

            Assert.Throws <ArgumentOutOfRangeException>(() => nullInput.EncryptBytes(validOutputArray, validInputArray, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => nullInput.EncryptBytes(validOutputArray, validInputArray, lengthOfData + 1));
            Assert.Throws <ArgumentOutOfRangeException>(() => nullInput.EncryptBytes(new byte[lengthOfData / 2], validInputArray, lengthOfData));
        }
        public byte[] Decryptdata(byte[] encrypted, byte[] key, byte[] iv)
        {
            AES_CTR forEncrypting = new AES_CTR(key, iv);

            byte[] plain = new byte[encrypted.Length];
            forEncrypting.DecryptBytes(plain, encrypted);
            return(plain);
        }
        public byte[] Encryptdata(byte[] plain, byte[] key, byte[] iv)
        {
            // Encrypt
            AES_CTR forEncrypting = new AES_CTR(key, iv);

            byte[] encrypted = new byte[plain.Length];
            forEncrypting.EncryptBytes(encrypted, plain);
            return(encrypted);
        }
        public void Known_Text_CTR_AES128Test()
        {
            // Arrange
            // These values are from https://github.com/ricmoo/aes-js/blob/master/README.md
            byte[] key            = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            byte[] initialCounter = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05 };

            byte[] bytesToEncrypt = System.Text.Encoding.UTF8.GetBytes("Text may be any length you wish, no padding is required.");
            byte[] expectedOutput = new byte[] { 0xa3, 0x38, 0xed, 0xa3, 0x87, 0x4e, 0xd8, 0x84, 0xb6, 0x19, 0x91, 0x50, 0xd3, 0x6f, 0x49, 0x98, 0x8c, 0x90, 0xf5, 0xc4, 0x7f, 0xe7, 0x79, 0x2b, 0x0c, 0xf8, 0xc7, 0xf7, 0x7e, 0xef, 0xfd, 0x87, 0xea, 0x14, 0x5b, 0x73, 0xe8, 0x2a, 0xef, 0xcf, 0x20, 0x76, 0xf8, 0x81, 0xc8, 0x88, 0x79, 0xe4, 0xe2, 0x5b, 0x1d, 0x7b, 0x24, 0xba, 0x27, 0x88 };
            byte[] actualOutput   = new byte[expectedOutput.Length];
            // Act
            AES_CTR aesCtr = new AES_CTR(key, initialCounter);

            aesCtr.EncryptBytes(actualOutput, bytesToEncrypt, bytesToEncrypt.Length);

            // Assert
            CollectionAssert.AreEqual(expectedOutput, actualOutput);
        }
        public void TestStringToUTF8BytesAndBack()
        {
            // Arrange
            //Random rng = new Random(Seed: 1337);
            byte[] key            = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            byte[] initialCounter = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05 };

            string testContent = "this is test content 😊";

            AES_CTR forEncrypting1 = new AES_CTR(key, initialCounter);
            AES_CTR forDecrypting1 = new AES_CTR(key, initialCounter);

            // Act
            byte[] encryptedContent = forEncrypting1.EncryptString(testContent);
            string decryptedString  = forDecrypting1.DecryptUTF8ByteArray(encryptedContent);

            // Assert
            Assert.AreEqual(testContent, decryptedString);
        }
        public void Known_CTR_AES128DecryptTest()
        {
            // Arrange

            // These are from https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf (F.5 CTR Example Vectors )
            byte[] key = new byte[16] {
                0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
            };
            byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            byte[] bytesToDecrypt1 = new byte[] { 0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce };
            byte[] expectedOutput1 = new byte[] { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a };
            byte[] actualOutput1   = new byte[bytesToDecrypt1.Length];

            byte[] bytesToDecrypt2 = new byte[] { 0x98, 0x06, 0xf6, 0x6b, 0x79, 0x70, 0xfd, 0xff, 0x86, 0x17, 0x18, 0x7b, 0xb9, 0xff, 0xfd, 0xff };
            byte[] expectedOutput2 = new byte[] { 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51 };
            byte[] actualOutput2   = new byte[bytesToDecrypt2.Length];

            byte[] bytesToDecrypt3 = new byte[] { 0x5a, 0xe4, 0xdf, 0x3e, 0xdb, 0xd5, 0xd3, 0x5e, 0x5b, 0x4f, 0x09, 0x02, 0x0d, 0xb0, 0x3e, 0xab };
            byte[] expectedOutput3 = new byte[] { 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef };
            byte[] actualOutput3   = new byte[bytesToDecrypt3.Length];

            byte[] bytesToDecrypt4 = new byte[] { 0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe, 0x03, 0xd1, 0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee };
            byte[] expectedOutput4 = new byte[] { 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
            byte[] actualOutput4   = new byte[bytesToDecrypt4.Length];

            // Act
            AES_CTR aesCtr = new AES_CTR(key, initialCounter);

            aesCtr.DecryptBytes(actualOutput1, bytesToDecrypt1, bytesToDecrypt1.Length);
            aesCtr.DecryptBytes(actualOutput2, bytesToDecrypt2, bytesToDecrypt2.Length);
            aesCtr.DecryptBytes(actualOutput3, bytesToDecrypt3, bytesToDecrypt3.Length);
            aesCtr.DecryptBytes(actualOutput4, bytesToDecrypt4, bytesToDecrypt4.Length);

            // Assert
            CollectionAssert.AreEqual(expectedOutput1, actualOutput1);
            CollectionAssert.AreEqual(expectedOutput2, actualOutput2);
            CollectionAssert.AreEqual(expectedOutput3, actualOutput3);
            CollectionAssert.AreEqual(expectedOutput4, actualOutput4);
        }
        public void Known_CTR_AES256DecryptTest()
        {
            // Arrange

            // These are from https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf (F.5 CTR Example Vectors )
            byte[] key = new byte[32] {
                0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
            };
            byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            byte[] bytesToDecrypt1 = new byte[] { 0x60, 0x1e, 0xc3, 0x13, 0x77, 0x57, 0x89, 0xa5, 0xb7, 0xa7, 0xf5, 0x04, 0xbb, 0xf3, 0xd2, 0x28 };
            byte[] expectedOutput1 = new byte[] { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a };
            byte[] actualOutput1   = new byte[bytesToDecrypt1.Length];

            byte[] bytesToDecrypt2 = new byte[] { 0xf4, 0x43, 0xe3, 0xca, 0x4d, 0x62, 0xb5, 0x9a, 0xca, 0x84, 0xe9, 0x90, 0xca, 0xca, 0xf5, 0xc5 };
            byte[] expectedOutput2 = new byte[] { 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51 };
            byte[] actualOutput2   = new byte[bytesToDecrypt2.Length];

            byte[] bytesToDecrypt3 = new byte[] { 0x2b, 0x09, 0x30, 0xda, 0xa2, 0x3d, 0xe9, 0x4c, 0xe8, 0x70, 0x17, 0xba, 0x2d, 0x84, 0x98, 0x8d };
            byte[] expectedOutput3 = new byte[] { 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef };
            byte[] actualOutput3   = new byte[bytesToDecrypt3.Length];

            byte[] bytesToDecrypt4 = new byte[] { 0xdf, 0xc9, 0xc5, 0x8d, 0xb6, 0x7a, 0xad, 0xa6, 0x13, 0xc2, 0xdd, 0x08, 0x45, 0x79, 0x41, 0xa6 };
            byte[] expectedOutput4 = new byte[] { 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
            byte[] actualOutput4   = new byte[bytesToDecrypt4.Length];

            // Act
            AES_CTR aesCtr = new AES_CTR(key, initialCounter);

            aesCtr.DecryptBytes(actualOutput1, bytesToDecrypt1, bytesToDecrypt1.Length);
            aesCtr.DecryptBytes(actualOutput2, bytesToDecrypt2, bytesToDecrypt2.Length);
            aesCtr.DecryptBytes(actualOutput3, bytesToDecrypt3, bytesToDecrypt3.Length);
            aesCtr.DecryptBytes(actualOutput4, bytesToDecrypt4, bytesToDecrypt4.Length);

            // Assert
            CollectionAssert.AreEqual(expectedOutput1, actualOutput1);
            CollectionAssert.AreEqual(expectedOutput2, actualOutput2);
            CollectionAssert.AreEqual(expectedOutput3, actualOutput3);
            CollectionAssert.AreEqual(expectedOutput4, actualOutput4);
        }
        public void Known_CTR_AES192DecryptTest()
        {
            // Arrange

            // These are from https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf (F.5 CTR Example Vectors )
            byte[] key = new byte[24] {
                0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
            };
            byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            byte[] bytesToDecrypt1 = new byte[] { 0x1a, 0xbc, 0x93, 0x24, 0x17, 0x52, 0x1c, 0xa2, 0x4f, 0x2b, 0x04, 0x59, 0xfe, 0x7e, 0x6e, 0x0b };
            byte[] expectedOutput1 = new byte[] { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a };
            byte[] actualOutput1   = new byte[bytesToDecrypt1.Length];

            byte[] bytesToDecrypt2 = new byte[] { 0x09, 0x03, 0x39, 0xec, 0x0a, 0xa6, 0xfa, 0xef, 0xd5, 0xcc, 0xc2, 0xc6, 0xf4, 0xce, 0x8e, 0x94 };
            byte[] expectedOutput2 = new byte[] { 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51 };
            byte[] actualOutput2   = new byte[bytesToDecrypt2.Length];

            byte[] bytesToDecrypt3 = new byte[] { 0x1e, 0x36, 0xb2, 0x6b, 0xd1, 0xeb, 0xc6, 0x70, 0xd1, 0xbd, 0x1d, 0x66, 0x56, 0x20, 0xab, 0xf7 };
            byte[] expectedOutput3 = new byte[] { 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef };
            byte[] actualOutput3   = new byte[bytesToDecrypt3.Length];

            byte[] bytesToDecrypt4 = new byte[] { 0x4f, 0x78, 0xa7, 0xf6, 0xd2, 0x98, 0x09, 0x58, 0x5a, 0x97, 0xda, 0xec, 0x58, 0xc6, 0xb0, 0x50 };
            byte[] expectedOutput4 = new byte[] { 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 };
            byte[] actualOutput4   = new byte[bytesToDecrypt4.Length];

            // Act
            AES_CTR aesCtr = new AES_CTR(key, initialCounter);

            aesCtr.DecryptBytes(actualOutput1, bytesToDecrypt1, bytesToDecrypt1.Length);
            aesCtr.DecryptBytes(actualOutput2, bytesToDecrypt2, bytesToDecrypt2.Length);
            aesCtr.DecryptBytes(actualOutput3, bytesToDecrypt3, bytesToDecrypt3.Length);
            aesCtr.DecryptBytes(actualOutput4, bytesToDecrypt4, bytesToDecrypt4.Length);

            // Assert
            CollectionAssert.AreEqual(expectedOutput1, actualOutput1);
            CollectionAssert.AreEqual(expectedOutput2, actualOutput2);
            CollectionAssert.AreEqual(expectedOutput3, actualOutput3);
            CollectionAssert.AreEqual(expectedOutput4, actualOutput4);
        }
示例#15
0
        public void DisposeAndTryToUse()
        {
            // Arrange
            byte[] key = new byte[16] {
                0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
            };
            byte[] initialCounter = new byte[] { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };

            byte[] content       = new byte[] { 11, 24, 22, 134, 234, 33, 4, 14, 34, 56, 23 };
            int    contentLength = content.Length;

            byte[] encrypted = new byte[contentLength];

            AES_CTR forEncrypting = new AES_CTR(key, initialCounter);

            // Act
            forEncrypting.EncryptBytes(encrypted, content, contentLength);
            forEncrypting.Dispose();

            // Assert
            Assert.Throws <ObjectDisposedException>(() => forEncrypting.EncryptBytes(encrypted, content, contentLength));
        }
示例#16
0
        static void Main(string[] args)
        {
            TextWriter errorWriter = Console.Error;

            int limit = 0;

            if (args.Length > 1 && !int.TryParse(args[1], out limit))
            {
                errorWriter.WriteLine($"{args[1]} is not a valid integer");
                return;
            }

            errorWriter.WriteLine("Starting throughput harness...");

            if (limit > 0)
            {
                errorWriter.WriteLine($"Limit is {limit} bytes");
            }
            else
            {
                errorWriter.WriteLine($"No byte limit");
            }

            byte[] key            = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            byte[] initialCounter = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05 };

            int bufferSize = 1024;

            int bytesProcessed = 0;

            byte[] buffer = new byte[bufferSize];

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            using (AES_CTR forEncrypting = new AES_CTR(key, initialCounter))
            {
                // Read from input stream as long as there is something
                using (Stream inputStream = Console.OpenStandardInput())
                {
                    // Write to output stream
                    using (Stream outputStream = Console.OpenStandardOutput())
                    {
                        int readAmount = inputStream.Read(buffer, 0, bufferSize);
                        while (readAmount > 0 && limit > -1)
                        {
                            outputStream.Write(forEncrypting.EncryptBytes(buffer, readAmount));

                            if (limit > 0)
                            {
                                limit -= readAmount;
                            }

                            bytesProcessed += readAmount;

                            readAmount = inputStream.Read(buffer, 0, bufferSize);
                        }
                    }
                }
            }

            stopwatch.Stop();
            errorWriter.WriteLine($"Processed {bytesProcessed} bytes in {stopwatch.Elapsed.TotalSeconds} seconds");
        }
        public void TestOverloadsNonPowerOfTwo()
        {
            // Arrange
            Random rng = new Random(Seed: 1337);

            byte[] key            = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
            byte[] initialCounter = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05 };

            const int lengthOfData = 13339;

            byte[] randomContent = new byte[lengthOfData];

            byte[] encryptedContent1 = new byte[lengthOfData];
            byte[] decryptedContent1 = new byte[lengthOfData];

            byte[] encryptedContent2 = null;
            byte[] decryptedContent2 = null;

            byte[] encryptedContent3 = null;
            byte[] decryptedContent3 = null;

            AES_CTR forEncrypting1 = null;
            AES_CTR forDecrypting1 = null;

            AES_CTR forEncrypting2 = null;
            AES_CTR forDecrypting2 = null;

            AES_CTR forEncrypting3 = null;
            AES_CTR forDecrypting3 = null;

            // Act
            rng.NextBytes(randomContent);

            forEncrypting1 = new AES_CTR(key, initialCounter);
            forDecrypting1 = new AES_CTR(key, initialCounter);

            forEncrypting2 = new AES_CTR(key, initialCounter);
            forDecrypting2 = new AES_CTR(key, initialCounter);

            forEncrypting3 = new AES_CTR(key, initialCounter);
            forDecrypting3 = new AES_CTR(key, initialCounter);

            forEncrypting1.EncryptBytes(encryptedContent1, randomContent);
            forDecrypting1.DecryptBytes(decryptedContent1, encryptedContent1);

            encryptedContent2 = forEncrypting2.EncryptBytes(randomContent, randomContent.Length);
            decryptedContent2 = forDecrypting2.DecryptBytes(encryptedContent2, encryptedContent2.Length);

            encryptedContent3 = forEncrypting3.EncryptBytes(randomContent);
            decryptedContent3 = forDecrypting3.DecryptBytes(encryptedContent3);

            // Assert
            CollectionAssert.AreEqual(randomContent, decryptedContent1);
            CollectionAssert.AreNotEqual(randomContent, encryptedContent1);

            CollectionAssert.AreEqual(randomContent, decryptedContent2);
            CollectionAssert.AreNotEqual(randomContent, encryptedContent2);

            CollectionAssert.AreEqual(randomContent, decryptedContent3);
            CollectionAssert.AreNotEqual(randomContent, encryptedContent3);
        }