using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace StekloMaster
{
    public static class Shifr
    {
        /*
        public static void SafeAndCripto(string put, string key, string dannie)
        {
            FileStream stream = new FileStream(put, FileMode.OpenOrCreate, FileAccess.Write);
            DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
            cryptic.Key = ASCIIEncoding.ASCII.GetBytes(key);
            cryptic.IV = ASCIIEncoding.ASCII.GetBytes(key);
            CryptoStream crStream = new CryptoStream(stream,
            cryptic.CreateEncryptor(), CryptoStreamMode.Write);
            byte[] data = ASCIIEncoding.ASCII.GetBytes(dannie);
            crStream.Write(data, 0, data.Length);
            crStream.Close();
            stream.Close();
        }
        public static Bitmap ReadAndDecripro(string put, string key)
        {
            try
            {
                
                FileStream stream = new FileStream(put,
                                  FileMode.Open, FileAccess.Read);

                DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();

                cryptic.Key = ASCIIEncoding.ASCII.GetBytes(key);
                cryptic.IV = ASCIIEncoding.ASCII.GetBytes(key);

                CryptoStream crStream = new CryptoStream(stream,
                    cryptic.CreateDecryptor(), CryptoStreamMode.Read);

                StreamReader reader = new StreamReader(crStream);

                Bitmap data = crStream.Read;
                reader.Close();
                stream.Close();
                return data;
            }
            catch { return ""; }
        }

        */

        /// <summary>
        /// Encrypted files
        /// </summary>
        /// <param name="sInputFile">input file</param>
        /// <param name="sOutputFile">Encrypted file</param>
        /// <param name="sKey">Encrypted keys</param>
        /// <param name="sMsg">Encrypted result message</param>
        /// <returns>return true or false.</returns>
        public static bool EncryptFile(string sInputFilename, string sOutputFilename, string sKey, out string sMsg)
        {
            bool IsSuccess = true;
            FileStream fsInput = null;
            FileStream fsEncrypted = null;
            CryptoStream cryptostream = null;
            try
            {
                fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
                fsEncrypted = new FileStream(sOutputFilename,
                                                        FileMode.Create,
                                                        FileAccess.Write);
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                ICryptoTransform desencrypt = DES.CreateEncryptor();
                cryptostream = new CryptoStream(fsEncrypted,
                                                            desencrypt,
                                                            CryptoStreamMode.Write);
                byte[] bytearrayinput = new byte[fsInput.Length];
                fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
                cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
                cryptostream.Close();
                fsInput.Close();
                fsEncrypted.Close();
                sMsg = "Encrypted is done";
                return true;
            }
            catch (Exception ex)
            {
                sMsg = ex.Message;
                IsSuccess = true;
            }
            finally
            {
                try
                {
                    if (cryptostream != null) cryptostream.Close();
                    if (fsInput != null) fsInput.Close();
                    if (fsEncrypted != null) fsEncrypted.Close();
                }
                catch
                {
                }
            }
            return IsSuccess;
        }
        
        /// <summary>
        /// Decrypt files
        /// </summary>
        /// <param name="sInputFile">input file</param>
        /// <param name="sOutputFile">Decrypt file</param>
        /// <param name="sKey">Decrypt keys</param>
        /// <param name="sMsg">Decrypt result message</param>
        /// <returns>return true or false.</returns>
        public static bool DecryptFile(string sInputFilename, string sOutputFilename, string sKey, out string sMsg)
        {
            FileStream fsread = null;
           // StreamWriter fsDecrypted = null;
            bool isSuccess = true;
            try
            {
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                //A 64 bit key and IV is required for this provider.
                //Set secret key For DES algorithm.
                DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                //Set initialization vector.
                DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                //Create a file stream to read the encrypted file back.
                fsread = new FileStream(sInputFilename,
                                               FileMode.Open,
                                               FileAccess.Read);
                //Create a DES decryptor from the DES instance.
                ICryptoTransform desdecrypt = DES.CreateDecryptor();
                //Create crypto stream set to read and do a
                //DES decryption transform on incoming bytes.
                CryptoStream cryptostreamDecr = new CryptoStream(fsread,
                                                             desdecrypt,
                                                             CryptoStreamMode.Read);
                //Print out the contents of the decrypted file.
                // isSuccess = Image.FromStream(new StreamReader(cryptostreamDecr).BaseStream);

                //  fsDecrypted = new StreamWriter(sOutputFilename);
                // fsDecrypted.Encoding = Encoding.ASCII;
                //  fsDecrypted.Write(new StreamReader(cryptostreamDecr).BaseStream);
                //  fsDecrypted.Flush();
                //  fsDecrypted.Close();
                Image.FromStream(new StreamReader(cryptostreamDecr).BaseStream).Save(sOutputFilename);
                sMsg = "Decrypted is done";
            }
            catch (Exception ex)
            {
                sMsg = ex.Message;
                isSuccess = false;
            }
            finally
            {
                try
                {
                    if (fsread != null) fsread.Close();
                 //   if (fsDecrypted != null) fsDecrypted.Close();
                }
                catch
                {
                }
            }
            return isSuccess;
        }

       
        public static bool DecryptFiless(string sInputFilename, string sOutputFilename, string sKey, out string sMsg)
        {
            bool IsSuccess = true;
            FileStream fsInput = null;
            FileStream fsDecrypted = null;
            CryptoStream cryptostream = null;
            try
            {
                fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
                fsDecrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                ICryptoTransform desdecrypt = des.CreateDecryptor();
                cryptostream = new CryptoStream(fsDecrypted,
                                                desdecrypt,
                                                CryptoStreamMode.Write);
                byte[] bytearrayinput = new byte[fsInput.Length];
                fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
                cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
                cryptostream.Close();
                fsInput.Close();
                fsDecrypted.Close();
                sMsg = "Decrypted is done";
                return true;
            }
            catch (Exception ex)
            {
                sMsg = ex.Message;
                IsSuccess = true;
            }
            finally
            {
                try
                {
                    if (cryptostream != null) cryptostream.Close();
                    if (fsInput != null) fsInput.Close();
                    if (fsDecrypted != null) fsDecrypted.Close();
                }
                catch
                {
                }
            }
            return IsSuccess;
        }

    /// <summary>
    /// Decrypt files
    /// </summary>
    /// <param name="sInputFile">input file</param>
    /// <param name="sOutputFile">Decrypt file</param>
    /// <param name="sKey">Decrypt keys</param>
    /// <param name="sMsg">Decrypt result message</param>
    /// <returns>return true or false.</returns>
    public static Image DecryptFileBit(string sInputFilename, string sKey, out string sMsg)
        {
            FileStream fsread = null;
            Image isSuccess = null;
            try
            {
                DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
                //A 64 bit key and IV is required for this provider.
                //Set secret key For DES algorithm.
                DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                //Set initialization vector.
                DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                //Create a file stream to read the encrypted file back.
                fsread = new FileStream(sInputFilename,
                                               FileMode.Open,
                                               FileAccess.Read);
                //Create a DES decryptor from the DES instance.
                ICryptoTransform desdecrypt = DES.CreateDecryptor();
                //Create crypto stream set to read and do a
                //DES decryption transform on incoming bytes.
                CryptoStream cryptostreamDecr = new CryptoStream(fsread,
                                                             desdecrypt,
                                                             CryptoStreamMode.Read);
                //Print out the contents of the decrypted file.


                isSuccess= Image.FromStream(new StreamReader(cryptostreamDecr).BaseStream);
                sMsg = "ok";
            }
            catch (Exception ex)
            {
                sMsg = ex.Message;
            }
            finally
            {
                try
                {
                    if (fsread != null) fsread.Close();
                }
                catch
                {
                }
            }
            return isSuccess;
        }
    }
}
