private XmlNode EncryptDecrypt(bool encrypt, XmlNode node)
        {
            string data;

            using (var tripleDes = new TripleDes(_secret))
            {
                data = encrypt
                           ? tripleDes.Encrypt(node.OuterXml)
                           : tripleDes.Decrypt(node.InnerText);
            }
            var xmlDoc = new XmlDocument
            {
                PreserveWhitespace = true
            };

            string value = data;

            if (encrypt)
            {
                const string encryptedDataTag = "EncryptedData";
                value = string.Format(CultureInfo.InvariantCulture, Constants.XmlTag, encryptedDataTag, data);
            }

            xmlDoc.LoadXml(value);
            // ReSharper disable once AssignNullToNotNullAttribute
            return(xmlDoc.DocumentElement);
        }
Beispiel #2
0
        public string DecryptTest([PexAssumeUnderTest] TripleDes target, string encryptedValue)
        {
            string result = target.Decrypt(encryptedValue);

            return(result);
            // TODO: add assertions to method TripleDesTest.DecryptTest(TripleDes, String)
        }
Beispiel #3
0
 public void Open()
 {
     if (sqlConnection == null)
     {
         sqlConnection = new SqlConnection(TripleDes.Decrypt(CommonObjects.GetConnectionString()));
         sqlConnection.Open();
         sqlTransaction = sqlConnection.BeginTransaction();
     }
 }
Beispiel #4
0
        public void TestEncryptString()
        {
            var encryptor = new TripleDes();
            string raw = "Đây là Unicode string", key = "password", salt = "this_is_salt";
            byte[] encrypted; byte[] decrypted;

            // without salt
            encrypted = encryptor.Encrypt(raw, key);
            Assert.NotNull(encrypted);
            decrypted = encryptor.Decrypt(encrypted, key);
            Assert.NotNull(decrypted);
            Assert.Equal(raw, decrypted.GetString());

            // with salt
            encrypted = encryptor.Encrypt(raw, key, salt);
            Assert.NotNull(encrypted);
            decrypted = encryptor.Decrypt(encrypted, key);
            Assert.NotNull(decrypted);
            Assert.Equal(raw, decrypted.GetString());
        }
Beispiel #5
0
        public void EncryptDescryptTest()
        {
            // Arrange
            var    tripleDes     = new TripleDes();
            string expectedValue = MethodBase.GetCurrentMethod()
                                   .Name;
            // Act
            string value       = tripleDes.Encrypt(expectedValue);
            string actualValue = tripleDes.Decrypt(value);

            // Assert
            Assert.AreEqual(expectedValue, actualValue);
            Assert.AreNotEqual(expectedValue, value);
        }
Beispiel #6
0
        public ActionResult Decrypted(HttpPostedFileBase upload, string key, string algoritm)
        {
            string fileName = GetUniqueFileName(upload.FileName);
            Image  image    = Image.FromStream(upload.InputStream);

            image.Save(Server.MapPath("~/Files/" + fileName));

            string imagePath     = Server.MapPath("~/Files/" + fileName);
            Bitmap bmp           = new Bitmap(imagePath);
            string extractedText = SteganoHelper.extractText(bmp);


            var baseLocation    = Server.MapPath("~/Text/");
            var fileNameRes     = GetUniqueFileName("res.txt");
            var fileLocationRes = Path.Combine(baseLocation, fileNameRes);

            try
            {
                if (algoritm == "AES")
                {
                    extractedText = Crypto.DecryptStringAES(extractedText, key) + Environment.NewLine;
                }
                else if (algoritm == "TripleDES")
                {
                    extractedText = TripleDes.Decrypt <TripleDESCryptoServiceProvider>(extractedText, key) + Environment.NewLine;
                }
                else if (algoritm == "Rijndael")
                {
                    extractedText = TripleDes.Decrypt <RijndaelManaged>(extractedText, key) + Environment.NewLine;
                }

                System.IO.File.AppendAllText(fileLocationRes, extractedText, Encoding.Default);
                TempData["FileLocation"] = fileLocationRes;
                return(RedirectToAction("PreviewDecrypt"));
            }
            catch
            {
                extractedText = "Wrong password";
                System.IO.File.AppendAllText(fileLocationRes, extractedText, Encoding.Default);
                TempData["FileLocation"] = fileLocationRes;
                return(RedirectToAction("PreviewDecrypt"));
            }
        }
Beispiel #7
0
        public ActionResult CryptoDecrypted(HttpPostedFileBase upload, string key, string algoritm)
        {
            var baseLocation  = Server.MapPath("~/Text/");
            var fileName1     = GetUniqueFileName(upload.FileName);
            var fileLocation1 = Path.Combine(baseLocation, fileName1);

            upload.SaveAs(fileLocation1);

            StreamReader sr            = new StreamReader(fileLocation1, Encoding.Default);
            string       extractedText = sr.ReadToEnd();


            var fileNameRes     = GetUniqueFileName("res.txt");
            var fileLocationRes = Path.Combine(baseLocation, fileNameRes);

            try
            {
                if (algoritm == "AES")
                {
                    extractedText = Crypto.DecryptStringAES(extractedText, key) + Environment.NewLine;
                }
                else if (algoritm == "TripleDES")
                {
                    extractedText = TripleDes.Decrypt <TripleDESCryptoServiceProvider>(extractedText, key) + Environment.NewLine;
                }
                else if (algoritm == "Rijndael")
                {
                    extractedText = TripleDes.Decrypt <RijndaelManaged>(extractedText, key) + Environment.NewLine;
                }

                System.IO.File.AppendAllText(fileLocationRes, extractedText, Encoding.Default);
                TempData["FileLocationCrypto"] = fileLocationRes;
                return(RedirectToAction("PreviewCryptoDecrypt"));
            }
            catch
            {
                extractedText = "Wrong password";
                System.IO.File.AppendAllText(fileLocationRes, extractedText, Encoding.Default);
                TempData["FileLocationCrypto"] = fileLocationRes;
                return(RedirectToAction("PreviewCryptoDecrypt"));
            }
        }