-1

I have two methods that use Encrypt and Decrypt in them what I do is read the file data the encrypted and modify the file with encrypted data, this operation done correctly but when I go to decrypt the encrypted data is not returning me the value not original if I returned strange characters: These are the functions:

public void EncryptFile(string[] conexion)
        {

            var mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);


            var inputFile = mydocpath + @"\ProjectCONFIG.sigc";

            const string password = @"*PROJECT-CONFIG-FILE-ENCRYPTED-2016*"; // Your Key Here

            var cryptFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var fsEncrypt = new FileStream(inputFile, FileMode.Create);

            var rmCrypto = new RijndaelManaged();

            Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes(password, 10000);
            rmCrypto.BlockSize = 256;

            byte[] key = pwdGen.GetBytes(rmCrypto.KeySize / 8);   //This will generate a 256 bits key
            byte[] iv = pwdGen.GetBytes(rmCrypto.BlockSize / 8);  //This will generate a 256 bits IV

            var cs = new CryptoStream(fsEncrypt, rmCrypto.CreateEncryptor(key, iv), CryptoStreamMode.Write);
            StreamWriter streamWriter = new StreamWriter(cs);
            streamWriter.WriteLine(conexion);

            streamWriter.Close();
            cs.Close();
            fsEncrypt.Close();
        }

        public string DecryptFile(string inputFile)
        {

            const string password = @"*PROJECT-CONFIG-FILE-ENCRYPTED-2016*"; // Your Key Here

            var cryptFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var fsEncrypt = new FileStream(inputFile, FileMode.Create);

            var rmCrypto = new RijndaelManaged();

            Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes(password, 10000);
            rmCrypto.BlockSize = 256;

            byte[] key = pwdGen.GetBytes(rmCrypto.KeySize / 8);   //This will generate a 256 bits key
            byte[] iv = pwdGen.GetBytes(rmCrypto.BlockSize / 8);  //This will generate a 256 bits IV

            var cs = new CryptoStream(fsEncrypt, rmCrypto.CreateEncryptor(key, iv), CryptoStreamMode.Read);

            StreamReader streamReader = new StreamReader(cs);
            string conexion = streamReader.ReadLine();


            streamReader.Close();
            cs.Close();
            fsEncrypt.Close();

            return conexion;
        }

Any suggestions ......

1 Answers1

0

@Artjom B. He realized I made a mistake in the decrypting function which would read:

 public string DecryptFile(string inputFile)
        {

            const string password = @"*PROJECT-CONFIG-FILE-ENCRYPTED-2016*"; // Your Key Here

            var cryptFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var fsEncrypt = new FileStream(inputFile, FileMode.Create);

            var rmCrypto = new RijndaelManaged();

            Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes(password, 10000);
            rmCrypto.BlockSize = 256;

            byte[] key = pwdGen.GetBytes(rmCrypto.KeySize / 8);   //This will generate a 256 bits key
            byte[] iv = pwdGen.GetBytes(rmCrypto.BlockSize / 8);  //This will generate a 256 bits IV

// The mistake is that he was calling the CreateEncryptor function ()
                var cs = new CryptoStream(fsEncrypt, rmCrypto.CreateDecryptor(key, iv), CryptoStreamMode.Read);

            StreamReader streamReader = new StreamReader(cs);
            string conexion = streamReader.ReadLine();


            streamReader.Close();
            cs.Close();
            fsEncrypt.Close();

            return conexion;
        }