public void Encrypt_To_File_Using_Main_Using_Manually_Filename()
        {
            const string encryptedOutputFile        = "manually.enc";
            var          merchantPublicKeyFileName  = @"Certificates\merchant.crt";
            var          mobilePayPublicKeyFileName = @"Certificates\mobilepay.crt";
            var          merchantSubscriptionKey    = Guid.NewGuid().ToString();

            string[] args = new[] { merchantSubscriptionKey, merchantPublicKeyFileName, mobilePayPublicKeyFileName, encryptedOutputFile };

            Program.Main(args);

            var csp          = new RSACryptoServiceProvider();
            var streamReader = File.OpenText(@"Certificates\mobilepay.pvk");
            var pr           = new PemReader(streamReader);

            var privateKey = (RsaPrivateCrtKeyParameters)pr.ReadObject();
            var rsa        = DotNetUtilities.ToRSAParameters(privateKey);

            csp.ImportParameters(rsa);
            var encryptedFile = File.ReadAllBytes(encryptedOutputFile);

            var decrypted = Encoding.UTF8.GetString(RSAEncryptionService.Decrypt(csp, encryptedFile));

            var decryptedSplits                  = decrypted.Split(';');
            var decryptedMerchantPublicKey       = decryptedSplits[0];
            var decryptedMerchantSubscriptionKey = decryptedSplits[1];

            var merchantPublicKey = File.ReadAllText(merchantPublicKeyFileName);

            Assert.Equal(merchantSubscriptionKey, decryptedMerchantSubscriptionKey);
            Assert.Equal(Regex.Replace(merchantPublicKey, @"\r\n?|\n", ""), Regex.Replace(decryptedMerchantPublicKey, @"\r\n?|\n", ""));
        }
        public void TestEncryptedStringCanBeDecrypted()
        {
            RSAEncryptionService service = new RSAEncryptionService();

            string testString = "Hello world encrypt me";

            byte[] encrypted = service.Encrypt(Encoding.UTF8.GetBytes(testString));

            Assert.AreEqual(testString, Encoding.UTF8.GetString(service.Decrypt(encrypted)));
        }
        public void TestDecryptDontReturnNull()
        {
            RSAEncryptionService service = new RSAEncryptionService();

            string testString = "Hello world encrypt me";

            byte[] encrypted = service.Encrypt(Encoding.UTF8.GetBytes(testString));

            Assert.NotNull(service.Decrypt(encrypted));
        }
        public void TestEncryptWorksWhenOnlyPublicKeyIsPresent()
        {
            RSAParameters privateAndPublicKey = RSAEncryptionService.GenerateKeys();

            RSAEncryptionService baseLineService = new RSAEncryptionService(privateAndPublicKey);
            string textToEncrypt = "Testing this encrypts";

            byte[] baselineEncrypted = baseLineService.Encrypt(Encoding.UTF8.GetBytes(textToEncrypt));

            RSAParameters publicKey = new RSAParameters
            {
                Modulus  = privateAndPublicKey.Modulus,
                Exponent = privateAndPublicKey.Exponent
            };
            RSAEncryptionService encryptionService = new RSAEncryptionService(publicKey);

            byte[] encryptedTest = encryptionService.Encrypt(Encoding.UTF8.GetBytes(textToEncrypt));

            //Assert That the encrypted text comes back as equals
            Assert.AreEqual(baseLineService.Decrypt(baselineEncrypted), baseLineService.Decrypt(encryptedTest));
        }
        public static bool ValidateHardwareId(string encryptedDataHardwareId)
        {
            var hardwareIdForValidation = RSAEncryptionService.Decrypt(encryptedDataHardwareId);

            if (!ValidateHardwareIdFormat(hardwareIdForValidation))
            {
                throw new ArgumentException("Wrong Hardware ID");
            }

            var currentHardwareId = $"{GetProcessorId()}{splitter}{GetMotherboardID()}{splitter}{GetDiskVolumeSerialNumber()}";

            return(hardwareIdForValidation == currentHardwareId);
        }
        public void TestDecryptingWithOnlyPublicKeyThrowsException()
        {
            RSAParameters privateAndPublicKey = RSAEncryptionService.GenerateKeys();

            RSAEncryptionService baseLineService = new RSAEncryptionService(privateAndPublicKey);
            string textToEncrypt = "Testing this encrypts";

            byte[] baselineEncrypted = baseLineService.Encrypt(Encoding.UTF8.GetBytes(textToEncrypt));

            RSAParameters publicKey = new RSAParameters
            {
                Modulus  = privateAndPublicKey.Modulus,
                Exponent = privateAndPublicKey.Exponent
            };
            RSAEncryptionService encryptionService = new RSAEncryptionService(publicKey);

            Assert.Throws <PrivateKeyNotPresentException>(() => encryptionService.Decrypt(baselineEncrypted));
        }
Exemple #7
0
        private void DecryptMessage_Click(object sender, RoutedEventArgs e)
        {
            PlainTextMessage.Text = Encoding.UTF8.GetString(encryptionService.Decrypt(Convert.FromBase64String(EncryptedMessage.Text)));

            MessageBox.Show(PlainTextMessage.Text);
        }