Example #1
0
        public void RSALocalKeyDecryptionProviderMissingThrows()
        {
            byte[] toEncrypt          = new byte[] { 1, 2, 3, 4 };
            ManagedRSAEncryption util = new ManagedRSAEncryption();

            byte[] encrypted = util.EncryptWithLocalKey(TestProvider, TestKeyName, toEncrypt);
            byte[] decrypted = util.DecryptWithLocalKey("FakeProviderName", TestKeyName, encrypted);
        }
Example #2
0
        public void TestLine(string line)
        {
            eventLog1.WriteEntry(string.Format("Testing Line:{0}", line));
            string[] split = line.Split(TestFileSeparator);
            if (split.Length != 6)
            {
                StringBuilder testFormatBuilder = new StringBuilder();
                testFormatBuilder.AppendLine(string.Format("Test line is of incorrect format, should be of format unencrypted{0}encrypted{0}providername{0}keyname{0}hashalgorithm{0}paddingflags for line {1}", TestFileSeparator, line));
                testFormatBuilder.AppendLine(string.Format("Encrypted value should be Base64 encoded"));
                testFormatBuilder.AppendLine(string.Format("HashAlgorithm can be SHA1, SHA256, SHA384, SHA512"));
                testFormatBuilder.AppendLine(string.Format("PaddingFlags can be 1, 2, 4, 8"));
                eventLog1.WriteEntry(testFormatBuilder.ToString(), EventLogEntryType.Error);
                return;
            }

            string unencrypted   = split[0];
            string encrypted     = split[1];
            string provider      = split[2];
            string keyname       = split[3];
            string hashAlgorithm = split[4];
            int    paddingFlags  = int.Parse(split[5]);


            byte[] encryptedAsBytes = Convert.FromBase64String(encrypted);

            string               resultString = "";
            EventLogEntryType    entryType    = EventLogEntryType.Information;
            ManagedRSAEncryption crypto       = new ManagedRSAEncryption();

            try
            {
                byte[] decrypted       = crypto.DecryptWithLocalKey(provider, keyname, encryptedAsBytes, hashAlgorithm, paddingFlags);
                string decryptedString = Encoding.ASCII.GetString(decrypted);

                if (String.Equals(unencrypted, decryptedString))
                {
                    resultString = string.Format("Successfully decrypted to value {0} for line {1}", unencrypted, line);
                }
                else
                {
                    resultString = string.Format("Failed to decrypt to value {0}, decrypted value was {1} for line {2}", unencrypted, decryptedString, line);
                    entryType    = EventLogEntryType.Error;
                }
            }
            catch (Exception e)
            {
                resultString = string.Format("Failed to decrypt line with exception {0} the line was: {1}", e, line);
                entryType    = EventLogEntryType.Error;
            }

            eventLog1.WriteEntry(resultString, entryType);
            string assemblyPath = Path.GetDirectoryName(typeof(IntunePFXCertConnectorValidationService).Assembly.Location);

            eventLog1.WriteEntry(string.Format("Wrinting results to {0}", Path.Combine(assemblyPath, ResultFile)));
            File.WriteAllText(Path.Combine(assemblyPath, ResultFile), contents: resultString);
        }
Example #3
0
        private void ValidatePasswordDecryptable(UserPFXCertificate userPFXResult, string expectedPassword, string hashAlgorithm, int paddingFlags)
        {
            ManagedRSAEncryption encryptionUtility = new ManagedRSAEncryption();

            byte[] passwordBytes       = Convert.FromBase64String(userPFXResult.EncryptedPfxPassword);
            byte[] unencryptedPassword = encryptionUtility.DecryptWithLocalKey(userPFXResult.ProviderName, userPFXResult.KeyName, passwordBytes, hashAlgorithm, paddingFlags);
            string clearTextPassword   = Encoding.ASCII.GetString(unencryptedPassword);

            Assert.AreEqual(clearTextPassword, expectedPassword);
        }
Example #4
0
        public void RSALocalKeyEncryptionDecryptionOAEPE2E()
        {
            byte[] toEncrypt          = new byte[] { 1, 2, 3, 4 };
            ManagedRSAEncryption util = new ManagedRSAEncryption();

            byte[] encrypted = util.EncryptWithLocalKey(TestProvider, TestKeyName, toEncrypt, PaddingHashAlgorithmNames.SHA256, PaddingFlags.OAEPPadding);
            byte[] decrypted = util.DecryptWithLocalKey(TestProvider, TestKeyName, encrypted, PaddingHashAlgorithmNames.SHA256, PaddingFlags.OAEPPadding);

            CollectionAssert.AreNotEqual(encrypted, toEncrypt);
            CollectionAssert.AreNotEqual(encrypted, decrypted);
            CollectionAssert.AreEqual(toEncrypt, decrypted);

            Assert.AreNotEqual(0, encrypted.Length);
            Assert.AreNotEqual(0, decrypted.Length);
            Assert.IsNotNull(encrypted);
            Assert.IsNotNull(decrypted);
        }
Example #5
0
        public void RSALocalKeyEncryptionDecryptionE2E()
        {
            byte[] toEncrypt          = new byte[] { 1, 2, 3, 4 };
            ManagedRSAEncryption util = new ManagedRSAEncryption();

            byte[] encrypted = util.EncryptWithLocalKey(TestProvider, TestKeyName, toEncrypt);
            byte[] decrypted = util.DecryptWithLocalKey(TestProvider, TestKeyName, encrypted);

            CollectionAssert.AreNotEqual(encrypted, toEncrypt);
            CollectionAssert.AreNotEqual(encrypted, decrypted);
            CollectionAssert.AreEqual(toEncrypt, decrypted);

            Assert.AreNotEqual(0, encrypted.Length);
            Assert.AreNotEqual(0, decrypted.Length);
            Assert.IsNotNull(encrypted);
            Assert.IsNotNull(decrypted);
        }
Example #6
0
        public void ExportImportEncryptTest()
        {
            byte[] toEncrypt = new byte[] { 1, 2, 3, 4 };
            string filePath  = @".\TestPublic.Key";

            try
            {
                File.Delete(filePath);
            }
            catch (Exception) { }
            ManagedRSAEncryption util = new ManagedRSAEncryption();

            util.ExportPublicKeytoFile(TestProvider, TestKeyName, filePath);
            Assert.IsTrue(File.Exists(filePath));
            byte[] encryptedBytes = util.EncryptWithFileKey(filePath, toEncrypt);
            Assert.IsNotNull(encryptedBytes);
            byte[] decryptedBytes = util.DecryptWithLocalKey(TestProvider, TestKeyName, encryptedBytes);
            Assert.AreEqual(System.Convert.ToBase64String(toEncrypt), System.Convert.ToBase64String(decryptedBytes));
            try
            {
                File.Delete(filePath);
            }
            catch (Exception) { }
        }