Example #1
0
        public static string Encrypt(string plainText, string pass)
        {
            string initVector = INIT_VECTOR_ENCRYPT_DECRYPT;

            // Before encrypting data, we will append plain text to a random
            // salt value, which will be between 4 and 8 bytes long (implicitly
            // used defaults).
            RijndaelEnhanced rijndaelKey =
                new RijndaelEnhanced(pass, initVector);

            //Console.WriteLine(String.Format("Plaintext   : {0}\n", plainText));

            // Encrypt the same plain text data 10 time (using the same key,
            // initialization vector, etc) and see the resulting cipher text;
            // encrypted values will be different.
            return rijndaelKey.Encrypt(plainText);
        }
Example #2
0
        public static bool EncryptFile(string pathFile, string pass)
        {
            string initVector = INIT_VECTOR_ENCRYPT_DECRYPT; // must be 16 bytes

            // Before encrypting data, we will append plain text to a random
            // salt value, which will be between 4 and 8 bytes long (implicitly
            // used defaults).
            RijndaelEnhanced rijndaelKey =
                new RijndaelEnhanced(pass, initVector);

            //Console.WriteLine(String.Format("Plaintext   : {0}\n", plainText));

            // Encrypt the same plain text data 10 time (using the same key,
            // initialization vector, etc) and see the resulting cipher text;
            // encrypted values will be different.
            //return 

            if (!File.Exists(pathFile))
            {
                return false;
            }
            try
            {
                // Reading the content of the file into an array of bytes.
                byte[] content = File.ReadAllBytes(pathFile);
                byte[] EncryptContent = rijndaelKey.EncryptToBytes(content);
                File.WriteAllBytes(pathFile, EncryptContent);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }

            return true;
        }
Example #3
0
 public static string Descrypt(string cipherText, string pass)
 {
     string initVector = INIT_VECTOR_ENCRYPT_DECRYPT;
     RijndaelEnhanced re = new RijndaelEnhanced(pass, initVector);
     return re.Decrypt(cipherText);
 }
Example #4
0
        public static bool DescryptFile(string pathFile, string pass, out MemoryStream stream)
        {
            string initVector = INIT_VECTOR_ENCRYPT_DECRYPT;
            RijndaelEnhanced re = new RijndaelEnhanced(pass, initVector);

            if (!File.Exists(pathFile))
            {
                stream = null;
                return false;
            }
            try
            {
                // Reading the content of the file into an array of bytes.
                byte[] content = File.ReadAllBytes(pathFile);
                byte[] DecryptContent = re.DecryptToBytes(content);
                //byte[] buffer = new byte[DecryptContent.Length];
                // File.WriteAllBytes("test.xml", EncryptContent);

                stream = new MemoryStream(DecryptContent);

                // Crazy debug
                // StreamReader sr = new StreamReader(stream);
                // File.WriteAllText("test1.xml", sr.ReadToEnd());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                stream = null;
                return false;
            }

            return true;

        }