예제 #1
0
        public void TestDecryptWrongAesKey()
        {
            const string dataFileName = "PNG_transparency_demonstration_1.png";

            Assert.Equal(true, File.Exists(dataFileName));
            var dataToEncrypt = File.ReadAllBytes(dataFileName);
            var pass          = Encoding.UTF8.GetBytes("TestPassword");
            var salt          = new byte[32];
            var mySha256      = SHA256.Create();
            int seed          =
                BitConverter.ToInt32(mySha256.ComputeHash(BitConverter.GetBytes((int)DateTime.UtcNow.Ticks & DateTime.UtcNow.Millisecond)), 0);
            var randomSource = new Random(seed);

            randomSource.NextBytes(salt);
            var keyIvGenerated = CryptConfigFileHelper.GenKeyAndIv(pass, salt);

            byte[] encryptedData = keyIvGenerated.EncryptData(dataToEncrypt);

            var changedKey = new byte[keyIvGenerated.Key.Length];

            keyIvGenerated.Key.CopyTo(changedKey, 0);
            changedKey[0] = (byte)(changedKey[0] ^ 3);
            Assert.Throws <EnumException <AesKeyIvPair.EDecryptDataErrCodes> >(() =>
            {
                byte[] originData = new AesKeyIvPair {
                    Iv = keyIvGenerated.Iv, Key = changedKey
                }
                .DecryptData(encryptedData);
                _output.WriteLine("Err key decrypted");
                Assert.Equal(originData, dataToEncrypt);
            });
        }
예제 #2
0
        public void TestEncryptDecrypt()
        {
            var passBytes       = Encoding.UTF8.GetBytes("TestPassword");
            var testCertificate =
                LightCertificatesHelper.GenerateSelfSignedCertificate(
                    ELightCertificateSignType.Rsa,
                    2048,
                    ELightCertificateEncryptType.Rsa,
                    2048,
                    EPrivateKeysKeyDerivationFunction.ScryptDefault,
                    Guid.NewGuid(),
                    "TestCertificate",
                    passBytes
                    );

            foreach (var n in new[] { 1, 2, 3, 5, 10, 16, 32, 48, 64, 63, 65, 191 })
            {
                var originData = new byte[n];
                MiscFuncs.GetRandomBytes(originData);
                var encryptedData  = testCertificate.EncryptData(originData);
                var originDataCopy = testCertificate.DecryptData(encryptedData, passBytes);
                Assert.Equal(originData.Length, originDataCopy.Length);
                Assert.Equal(originData, originDataCopy);
            }
            for (int i = 0; i < 10; i++)
            {
                var aesPair       = AesKeyIvPair.GenAesKeyIvPair();
                var encryptedPair = LightCertificatesHelper.EncryptAesKeyIvPair(
                    aesPair,
                    testCertificate
                    );
                var decryptedPair = LightCertificatesHelper.DecryptAesKeyIvPair(
                    encryptedPair,
                    testCertificate,
                    passBytes
                    );
                Assert.Equal(
                    aesPair.WriteObjectToJson(),
                    decryptedPair.WriteObjectToJson()
                    );
            }
        }