using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace GcDevicePc
{
    class AESfile
    {
        private string filename = "default.tw";
        private string default_password = "Tet-chrom";
        private string default_iv = "2016201620162016";

     //   private FileStream aesfs;

        public AESfile(string file)
        {
            filename =  file;
        }

        private string ByteToString(byte[] InBytes)
        {
            string StringOut = "";
            foreach (byte InByte in InBytes)
            {
                StringOut = StringOut + String.Format("{0:X2} ", InByte);
            }
            return StringOut;
        }
       
        // 把十六进制字符串转换成字节型  
        private  byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }


        private string AESEncrypt(string text, string password, string iv)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;
            rijndaelCipher.KeySize = 128;
            rijndaelCipher.BlockSize = 128;

            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
            byte[] keyBytes = new byte[16];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length) len = keyBytes.Length;
            System.Array.Copy(pwdBytes, keyBytes, len);

            rijndaelCipher.Key = keyBytes;
            byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
            rijndaelCipher.IV = ivBytes;
            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();

            byte[] plainText = Encoding.UTF8.GetBytes(text);
            byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);

            return ByteToString(cipherBytes);

        }
        
        private string AESDecrypt(string text, string password, string iv)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;
            rijndaelCipher.KeySize = 128;
            rijndaelCipher.BlockSize = 128;
            byte[] encryptedData = strToToHexByte(text);
            byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
            byte[] keyBytes = new byte[16];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length) len = keyBytes.Length;
            System.Array.Copy(pwdBytes, keyBytes, len);
            rijndaelCipher.Key = keyBytes;
            byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
            rijndaelCipher.IV = ivBytes;
            ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
            byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
            return Encoding.UTF8.GetString(plainText);
        }

        public bool Create_filetw()
        {
            if (File.Exists(this.filename) == true)
            {
                return false;
            }
            else
            {
                FileStream aesfs = new FileStream(this.filename, FileMode.Create);
                aesfs.Close();
                return true;
            }
        }

        public bool Write_filetw(float Time, float Voltage)
        {
            string line;
            StreamWriter fswriter = new StreamWriter(filename, true);

            line = AESEncrypt(Time.ToString("0.000000"), this.default_password, this.default_iv);
            fswriter.WriteLine(line);
            line = AESEncrypt(Voltage.ToString("0.000"), this.default_password, this.default_iv);
            fswriter.WriteLine(line);
            fswriter.Close();

            return true;       
        }

        public bool Read_filetw(ref double[] Time, ref double[] Voltage)
        {
            string line;
            int i = 0;

            StreamReader fsread = new StreamReader(filename);    
            while(fsread.Peek() >= 0)
            {
              
                line = fsread.ReadLine();
                Time[i] = double.Parse(AESDecrypt(line, this.default_password, this.default_iv));
                line = fsread.ReadLine();
                Voltage[i] = double.Parse(AESDecrypt(line, this.default_password, this.default_iv));
                i++;
            }

            fsread.Close();
            return true;
        }

    }
}
