Пример #1
0
        public void PackingAndUnpackingWorksWithKeyOnly()
        {
            // If the encryption is started with a key instead of a password, there are not all parametes set.
            CryptoHeader header = new CryptoHeader
            {
                PackageName   = "MyAppName",
                AlgorithmName = "Twofish",
                KdfName       = null,
                Cost          = null,
                Nonce         = CommonMocksAndStubs.FilledByteArray(8, 66),
                Salt          = null,
                Compression   = "gzip",
            };

            byte[] cipher = CommonMocksAndStubs.FilledByteArray(16, 88);

            byte[] packedHeader = CryptoHeaderPacker.PackHeaderAndCypher(header, cipher);
            CryptoHeaderPacker.UnpackHeaderAndCipher(packedHeader, "MyAppName", out CryptoHeader unpackedHeader, out byte[] unpackedCipher);

            Assert.AreEqual(header.PackageName, unpackedHeader.PackageName);
            Assert.AreEqual(header.AlgorithmName, unpackedHeader.AlgorithmName);
            Assert.AreEqual(header.KdfName, unpackedHeader.KdfName);
            Assert.AreEqual(header.Cost, unpackedHeader.Cost);
            Assert.AreEqual(header.Nonce, unpackedHeader.Nonce);
            Assert.AreEqual(header.Salt, unpackedHeader.Salt);
            Assert.AreEqual(cipher, unpackedCipher);
            Assert.AreEqual(header.Compression, unpackedHeader.Compression);
        }
Пример #2
0
        public void PackingAndUnpackingResultsInOriginal()
        {
            CryptoHeader header = new CryptoHeader
            {
                PackageName   = "MyAppName",
                AlgorithmName = "Twofish",
                KdfName       = "PBKDF2",
                Cost          = "10",
                Nonce         = CommonMocksAndStubs.FilledByteArray(8, 66),
                Salt          = CommonMocksAndStubs.FilledByteArray(8, 77),
                Compression   = "gzip",
            };

            byte[] cipher = CommonMocksAndStubs.FilledByteArray(16, 88);

            byte[] packedHeader = CryptoHeaderPacker.PackHeaderAndCypher(header, cipher);
            CryptoHeaderPacker.UnpackHeaderAndCipher(packedHeader, "MyAppName", out CryptoHeader unpackedHeader, out byte[] unpackedCipher);

            Assert.AreEqual(header.PackageName, unpackedHeader.PackageName);
            Assert.AreEqual(header.AlgorithmName, unpackedHeader.AlgorithmName);
            Assert.AreEqual(header.KdfName, unpackedHeader.KdfName);
            Assert.AreEqual(header.Cost, unpackedHeader.Cost);
            Assert.AreEqual(header.Nonce, unpackedHeader.Nonce);
            Assert.AreEqual(header.Salt, unpackedHeader.Salt);
            Assert.AreEqual(cipher, unpackedCipher);
            Assert.AreEqual(header.Compression, unpackedHeader.Compression);
        }
Пример #3
0
        public void EnsureBackwardsCompatibility()
        {
            // Ensure that a once stored packed header can always be unpacked even after changes in the liberary
            string packedBase64Header = "TXlBcHBOYW1lJFR3b2Zpc2gkUWtKQ1FrSkNRa0k9JFBCS0RGMiRUVTFOVFUxTlRVMD0kMTAkWFhYWFhYWFhYWFhYWFhYWA==";

            byte[] packedHeader = CryptoUtils.Base64StringToBytes(packedBase64Header);
            CryptoHeaderPacker.UnpackHeaderAndCipher(packedHeader, "MyAppName", out CryptoHeader unpackedHeader, out byte[] unpackedCipher);

            Assert.AreEqual("MyAppName", unpackedHeader.PackageName);
            Assert.AreEqual("Twofish", unpackedHeader.AlgorithmName);
            Assert.AreEqual("PBKDF2", unpackedHeader.KdfName);
            Assert.AreEqual("10", unpackedHeader.Cost);
            Assert.AreEqual(CommonMocksAndStubs.FilledByteArray(8, 66), unpackedHeader.Nonce);
            Assert.AreEqual(CommonMocksAndStubs.FilledByteArray(8, 77), unpackedHeader.Salt);
            Assert.AreEqual(CommonMocksAndStubs.FilledByteArray(16, 88), unpackedCipher);
        }