Example #1
0
        public void EncryptAndSignUsingPfxTest()
        {
            var rsa = new Asymmetric(Path.Join(path, @"Data\certificate.pfx"), password);

            // EXAMPLE WITH STRING
            var signature = rsa.Sign(message);

            Assert.IsTrue(rsa.Verify(message, signature));
            var cipher = rsa.Encrypt(message);

            Assert.AreEqual(message, rsa.Decrypt(cipher));

            // CONDENSED EXAMPLE WITH BYTES
            Assert.IsTrue(rsa.Verify(message, rsa.Sign(message)));
            Assert.AreEqual(message, rsa.Decrypt(rsa.Encrypt(message)));
        }
Example #2
0
        public void Encrypt_Decrypt_Equal()
        {
            var sut            = new Asymmetric();
            var encryptedBytes = sut.Encrypt(PlainText);
            var decryptedText  = sut.Decrypt(encryptedBytes);

            Assert.AreEqual(PlainText, decryptedText);
        }
        public void BasicCanDecryptTest()
        {
            const string PLAINTEXT  = "This is a test of the SmartEncryption system...";
            var          senderKeys = Asymmetric.GenerateKeyPair();
            var          recipKeys  = Asymmetric.GenerateKeyPair();

            var cipher = Asymmetric.Encrypt(Encoding.UTF8.GetBytes(PLAINTEXT), senderKeys.PrivateKey,
                                            recipKeys.PublicKey);
            var plain = Asymmetric.Decrypt(cipher, recipKeys.PrivateKey, senderKeys.PublicKey);

            Assert.AreEqual(PLAINTEXT, Encoding.UTF8.GetString(plain));
        }
Example #4
0
        public IActionResult Decrypt(string ciphertext)
        {
            string privateKey = _configuration["Asymmetric:PrivateKey"];

            Asymmetric a = new Asymmetric();

            byte[] decryptedBytes = a.Decrypt(ciphertext, privateKey);

            ViewBag.Ciphertext = ciphertext;
            ViewBag.Plaintext  = Encoding.UTF8.GetString(decryptedBytes);

            return(View());
        }
Example #5
0
        public static bool DecryptBlackBoxFile(string decryptedFileName, string readFileName)
        {
            byte[] encryptedBytes = File.ReadAllBytes(readFileName);
            byte[] decryptedBytes = Asymmetric.Decrypt(encryptedBytes);

            if (decryptedBytes != null)
            {
                File.WriteAllBytes(decryptedFileName, decryptedBytes);
                return(true);
            }

            return(false);
        }
Example #6
0
 public void Decrypt_Cipher_Size_Exception()
 {
     try
     {
         var sut = new Asymmetric(privateKey: PrivateKey);
         sut.Decrypt(new byte[100]);
     }
     catch (Exception ex)
     {
         Assert.AreEqual("Parameter encryptedBytes must be divisible by 256.", ex.Message);
         throw;
     }
 }
Example #7
0
 public void Decrypt_No_Private_Key_Exception()
 {
     try
     {
         var sut = new Asymmetric(PublicKey);
         sut.Decrypt(new byte[100]);
     }
     catch (Exception ex)
     {
         Assert.AreEqual("PrivateKey missing.", ex.Message);
         throw;
     }
 }
Example #8
0
        public void Encrypt_Decrypt_Existing_Key_Pair_Equal()
        {
            var encryptor = new Asymmetric(PublicKey);
            var decryptor = new Asymmetric(privateKey: PrivateKey);

            Assert.IsNull(encryptor.PrivateKey);
            Assert.IsNull(decryptor.PublicKey);

            var encryptedBytes = encryptor.Encrypt(PlainText);
            var decryptedText  = decryptor.Decrypt(encryptedBytes);

            Assert.AreEqual(PlainText, decryptedText);
        }
Example #9
0
        private static byte[] DecryptData(byte[] encryptedData)
        {
            if (encryptedData == null)
            {
                return(null);
            }

            try
            {
                XDocument xd;

                using (var stream = new MemoryStream(encryptedData))
                {
                    using (XmlReader xmlReader = XmlReader.Create(stream))
                    {
                        xd = XDocument.Load(xmlReader);
                    }
                }

                XElement root = xd.Root;

                if (root == null)
                {
                    return(null);
                }

                var crVersion = (int?)root.Attribute("CrVersion");

                if (!crVersion.HasValue)
                {
                    return(null);
                }

                switch (crVersion.Value)
                {
                case (int)CryptoVersion.CrV0AsymRaw1:
                    return(Asymmetric.Decrypt(encryptedData));

                case (int)CryptoVersion.CrV1SymRaw1:
                case (int)CryptoVersion.CrV4SymRaw2:
                case (int)CryptoVersion.CrV5SymRaw3:
                    return(EncryptionSymmHelper.Decrypt(encryptedData));

                case (int)CryptoVersion.CrV2SymSettings1:
                case (int)CryptoVersion.CrV6SymSettings2:
                    return(EncryptionSettingsHelper.Decrypt(encryptedData));

                case (int)CryptoVersion.CrV3AsymRaw2:
                    return(Asymmetric.Decrypt(encryptedData));

                default:
                    return(null);
                }
            }
            catch (Exception e)
            {
                string message = $"Decryption failed \n {e.Message} \n {e.InnerException}";
                MessageBox.Show(message, "Decryption failed");
            }

            return(null);
        }