public void TestEncryptDontReturnNull()
        {
            RSAEncryptionService service = new RSAEncryptionService();

            string testString = "Hello world encrypt me";

            Assert.NotNull(service.Encrypt(Encoding.UTF8.GetBytes(testString)));
        }
        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 static void ToFile(
            string merchantSubscriptionKey,
            string merchantPublicKeyFileName,
            string mobilePayPublicKeyFileName,
            string encryptedOutputFile = "merchant.enc")
        {
            if (string.IsNullOrWhiteSpace(merchantSubscriptionKey))
            {
                throw new ArgumentException("Argument 1, your merchant subscription key, is not valid");
            }

            string merchantPublicKeyCrt;

            try
            {
                var x509Certificate2 = new X509Certificate2(merchantPublicKeyFileName);
                merchantPublicKeyCrt = ExportToPem(x509Certificate2);
            }
            catch (Exception e)
            {
                throw new ArgumentException(
                          "Argument 2, your merchant public key file path, is not pointing to a valid X509Certificate file", e);
            }

            X509Certificate2 mobilePayPublicKeyCrt;

            try
            {
                mobilePayPublicKeyCrt = new X509Certificate2(mobilePayPublicKeyFileName);
            }
            catch (Exception e)
            {
                throw new ArgumentException(
                          "Argument 3, MobilePay's public key file path, is not pointing to a valid X509Certificate file", e);
            }

            var encryptedBytes = RSAEncryptionService.Encrypt(
                mobilePayPublicKeyCrt.PublicKey.Key as RSACryptoServiceProvider,
                Encoding.UTF8.GetBytes(merchantPublicKeyCrt + ';' + merchantSubscriptionKey));

            using (var outputFileStream = new FileStream(encryptedOutputFile, FileMode.Create, FileAccess.ReadWrite))
            {
                outputFileStream.Write(encryptedBytes, 0, encryptedBytes.Length);
            }
        }
        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));
        }
        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));
        }
Пример #6
0
 private void EncryptMessage_Click(object sender, RoutedEventArgs e)
 {
     EncryptedMessage.Text = Convert.ToBase64String(encryptionService.Encrypt(Encoding.UTF8.GetBytes(PlainTextMessage.Text)));
 }
Пример #7
0
        public static string EncryptHardwareId(string hardwareId)
        {
            var encryptedData = RSAEncryptionService.Encrypt(hardwareId);

            return(encryptedData);
        }