Пример #1
0
        public ActionResult CryptoEncrypted(HttpPostedFileBase upload1, string key, string algoritm)
        {
            var baseLocation = Server.MapPath("~/Files/");

            var fileName1     = GetUniqueFileName(upload1.FileName);
            var fileLocation1 = Path.Combine(baseLocation, fileName1);

            upload1.SaveAs(fileLocation1);

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

            if (algoritm == "AES")
            {
                text = Crypto.EncryptStringAES(text, key);
            }
            else if (algoritm == "TripleDES")
            {
                text = TripleDes.Encrypt <TripleDESCryptoServiceProvider>(text, key);
            }
            else if (algoritm == "Rijndael")
            {
                text = TripleDes.Encrypt <RijndaelManaged>(text, key);
            }

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

            System.IO.File.AppendAllText(fileLocationRes, text, Encoding.Default);
            TempData["FileLocationRes"] = fileLocationRes;
            return(RedirectToAction("PreviewCryptoEn"));
        }
        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);
        }
Пример #3
0
        public string EncryptTest([PexAssumeUnderTest] TripleDes target, string value)
        {
            string result = target.Encrypt(value);

            return(result);
            // TODO: add assertions to method TripleDesTest.EncryptTest(TripleDes, String)
        }
Пример #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());
        }
Пример #5
0
        public ActionResult  Encrypted(HttpPostedFileBase upload1, HttpPostedFileBase upload2, string key, string algoritm)
        {
            var baseLocation = Server.MapPath("~/Files/");

            var fileName1     = GetUniqueFileName(upload1.FileName);
            var fileLocation1 = Path.Combine(baseLocation, fileName1);

            upload1.SaveAs(fileLocation1);

            var fileName2     = GetUniqueFileName(upload2.FileName);
            var fileLocation2 = Path.Combine(baseLocation, fileName2);

            upload2.SaveAs(fileLocation2);


            Image image = Image.FromStream(upload2.InputStream);

            image.Save(fileLocation2);
            string imagePath = fileLocation2;
            Bitmap bmp       = new Bitmap(imagePath);

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

            if (algoritm == "AES")
            {
                text = Crypto.EncryptStringAES(text, key);
            }
            else if (algoritm == "TripleDES")
            {
                text = TripleDes.Encrypt <TripleDESCryptoServiceProvider>(text, key);
            }
            else if (algoritm == "Rijndael")
            {
                text = TripleDes.Encrypt <RijndaelManaged>(text, key);
            }

            Bitmap bmp1 = SteganoHelper.embedText(text, bmp);

            arr = bmp1.ToByteArray(ImageFormat.Png);
            MemoryStream ms          = new MemoryStream(arr);
            Image        imageResult = Image.FromStream(ms);

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

            imageResult.Save(fileLocationRes);

            TempData["ImageLocation"] = fileNameRes;
            return(RedirectToAction("Preview"));
        }
Пример #6
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);
        }