public static void EncryptDecrypt(string iFileName, string oFileName, string key)
        {
            long l = 0;

            Init_S(key);

            using (FileStream iFileStream = new FileStream(iFileName, FileMode.Open))
            {
                var lenght = iFileStream.Length;
                SetMaximum(lenght);
                using (FileStream oFileStream = new FileStream(oFileName, FileMode.Create))
                {
                    while (lenght > l)
                    {
                        i = (i + 1) % RC4_SIZE;
                        j = (j + S[i]) % RC4_SIZE;
                        var tmp = S[i];
                        S[i] = S[j];
                        S[j] = tmp;
                        var t  = (S[i] + S[j]) % RC4_SIZE;
                        var K  = S[t];
                        var bt = (byte)iFileStream.ReadByte();
                        oFileStream.WriteByte((byte)((int)bt ^ (int)K));
                        l++;
                        SetValue.Invoke(l);
                    }
                    EncodeFinished($"Файл\n{iFileName}\nуспешно зашифрован\n\nСоздан файл\n{oFileName}\nс шифротекстом");
                }
            }
        }
        private static List <UInt64> CBC(bool isEncrypt = true)
        {
            List <UInt64> result = new List <ulong>();
            UInt64        c      = IV;

            foreach (var block in _blocks)
            {
                if (isEncrypt)
                {
                    var newBlock = c ^ block;
                    var tmp      = DES.EncodeBlock(newBlock, _key);
                    c = tmp;
                    result.Add(tmp);
                }
                else
                {
                    var decryptedBlock = DES.DecodeBlock(block, _key);
                    var tmpBlock       = block;
                    result.Add(c ^ decryptedBlock);
                    c = tmpBlock;
                }
                codingValue++;
                SetValue.Invoke(codingValue);
            }

            return(result);
        }
        private static List <UInt64> CFB(bool isEncrypt = true)
        {
            UInt64        c      = IV;
            List <UInt64> result = new List <ulong>();

            foreach (var block in _blocks)
            {
                var tmpC = DES.EncodeBlock(c, _key);
                if (isEncrypt)
                {
                    var tmp = block ^ tmpC;
                    result.Add(tmp);
                    c = tmp;
                }
                else
                {
                    c = block;
                    result.Add(block ^ tmpC);
                }
                codingValue++;
                SetValue.Invoke(codingValue);
            }

            return(result);
        }
        public static void EncryptDecrypt(string iFineName, string oFileName, int[] mix, bool NeedEncrypt = false)
        {
            long i = 0;

            using (FileStream iFileStream = new FileStream(iFineName, FileMode.Open))
            {
                var lenght = iFileStream.Length;
                SetMaximum(lenght);
                using (FileStream oFileStream = new FileStream(oFileName, FileMode.Create))
                {
                    while (lenght > i)
                    {
                        var bt = (byte)iFileStream.ReadByte();
                        if (NeedEncrypt)
                        {
                            oFileStream.WriteByte(MixBitsE(bt, mix));
                        }
                        else
                        {
                            oFileStream.WriteByte(MixBitsD(bt, mix));
                        }
                        i++;
                        SetValue.Invoke(i);
                    }
                    EncodeFinished($"Файл\n{iFineName}\nуспешно зашифрован\n\nСоздан файл\n{oFileName}\nс шифротекстом");
                }
            }
        }
        public static void Decrypt(string iFineName, string oFileName, string keyFileName)
        {
            long i = 0;

            using (FileStream iFileStream = new FileStream(iFineName, FileMode.Open))
            {
                var lenght = iFileStream.Length;
                SetMaximum(lenght);
                using (FileStream oFileStream = new FileStream(oFileName, FileMode.Create))
                {
                    using (FileStream keyFileStream = new FileStream(keyFileName, FileMode.Open))
                    {
                        while (lenght > i)
                        {
                            var  bt  = (byte)iFileStream.ReadByte();
                            byte key = (byte)keyFileStream.ReadByte();
                            //oFileStream.WriteByte((byte)(bt ^ key));
                            oFileStream.WriteByte(EncodeBytes(bt, key));
                            i++;
                            SetValue.Invoke(i);
                        }
                        EncodeFinished($"Файл\n{iFineName}\nуспешно дешифрован\n\nСоздан файл\n{oFileName}\nс открытым текстом");
                    }
                }
            }

            //File.Delete(keyFileName);
        }
        public static void Encrypt(string iFineName, string oFileName, string keyFileName)
        {
            long i = 0;

            using (FileStream iFileStream = new FileStream(iFineName, FileMode.Open))
            {
                var lenght = iFileStream.Length;
                SetMaximum(lenght);
                using (FileStream oFileStream = new FileStream(oFileName, FileMode.Create))
                {
                    using (FileStream keyFileStream = new FileStream(keyFileName, FileMode.Create))
                    {
                        while (lenght > i)
                        {
                            var  bt  = (byte)iFileStream.ReadByte();
                            byte key = (byte)rnd.Next();
                            //oFileStream.WriteByte((byte)(bt ^ key));
                            oFileStream.WriteByte(EncodeBytes(bt, key));
                            keyFileStream.WriteByte(key);
                            i++;
                            SetValue.Invoke(i);
                        }
                        EncodeFinished($"Файл\n{iFineName}\nуспешно зашифрован\n\nСоздан файл\n{oFileName}\nс шифротекстом\n\nКлюч сохранён в файле\n{keyFileName}");
                    }
                }
            }
        }
        public static void cryptFile(int E, int N, string iFileName, string oFileName)
        {
            long       i        = 0;
            FileStream iFStream = new FileStream(iFileName, FileMode.Open);

            using (BinaryReader iFileStream = new BinaryReader(iFStream))
            {
                var lenght = (int)iFStream.Length;
                SetMaximum(iFStream.Length);
                FileStream oFStream = new FileStream(oFileName, FileMode.Create);
                using (BinaryWriter oFileStream = new BinaryWriter(oFStream))
                {
                    var         bytes    = iFileStream.ReadBytes(lenght);
                    List <byte> resBytes = new List <byte>();
                    foreach (var m in bytes)
                    {
                        int  res  = crypt(E, N, m);
                        byte res2 = (byte)res;
                        resBytes.Add(res2);
                        oFileStream.Write(res2);
                        i++;
                        SetValue.Invoke(i);
                    }
                    EncodeFinished($"Файл\n{iFileName}\nуспешно зашифрован\n\nСоздан файл\n{oFileName}\nс шифротекстом");
                }
            }
        }
 /// <summary>
 /// Raises the SetValue event, if ValueGetterFunc is left null, otherwise it will call ValueGetterFunc instead.
 /// </summary>
 /// <param name="e">A PropertySpecEventArgs that contains the event data.</param>
 protected internal virtual void OnSetValue(PropertySpecificationEventArgs e)
 {
     if (this.ValueSetterFunc == null)
     {
         SetValue?.Invoke(this, e);
     }
     else
     {
         Trap.trap();
         this.ValueSetterFunc(e.Property.Name, e.Value);
     }
 }
        private static List <UInt64> ECB(bool isEncrypt = true)
        {
            List <UInt64> result = new List <ulong>();

            foreach (var block in _blocks)
            {
                result.Add((isEncrypt) ? (DES.EncodeBlock(block, _key)) : (DES.DecodeBlock(block, _key)));
                codingValue++;
                SetValue.Invoke(codingValue);
            }

            return(result);
        }
Esempio n. 10
0
 public void ApplySettings()
 {
     SetValue?.Invoke(lastValue);
 }
Esempio n. 11
0
 public void ApplySettings()
 {
     SetValue?.Invoke(_ColorPickerPreviewClickableInst.ImagePreview.color);
 }
Esempio n. 12
0
 public void ApplySettings()
 {
     SetValue?.Invoke((_intValues) ? ((float)Math.Floor(lastVal)) : (lastVal));
 }
Esempio n. 13
0
 protected override void ApplyValue(int value)
 {
     SetValue?.Invoke(FixValue(value));
 }
        public static void StartEncryptDecrypt(string iFileName, string oFileName, UInt64 key, CouplingMode couplingMode, bool isEncrypt = true)
        {
            codingValue   = 0;
            _key          = key;
            _wasEncrypted = 0;

            FileStream iFStream = new FileStream(iFileName, FileMode.Open);

            using (BinaryReader iFileStream = new BinaryReader(iFStream))
            {
                var lenght = iFStream.Length;                    //количество байт
                SetMaximum.Invoke(lenght * 3 / 8);
                ulong  blocks        = (ulong)lenght / (ulong)8; //количество unsigned long int (UInt64)
                ulong  cryptedBlocks = 0;
                UInt64 block         = 0;
                _blocks.Clear();
                using (BinaryWriter oFileStream = new BinaryWriter(new FileStream(oFileName, FileMode.Create)))
                {
                    while (blocks > cryptedBlocks)
                    {
                        block = iFileStream.ReadUInt64();
                        _blocks.Add(block);
                        //Write(block.ToString());
                        block = 0;

                        cryptedBlocks++;

                        codingValue++;
                        SetValue.Invoke(codingValue);
                    }

                    if (isEncrypt)
                    {
                        if (lenght % 8 != 0)
                        {
                            for (int i = 0; i < lenght % 8; i++)
                            {
                                var bt = (byte)iFileStream.ReadByte();
                                block  = block << 8;
                                block |= bt;
                            }
                            block = MakeFullBlock(block, (int)(lenght % 8));
                        }
                        else
                        {
                            block = 8;
                        }
                        _blocks.Add(block);
                        //Write(block.ToString());
                        codingValue++;
                        SetValue.Invoke(codingValue);
                    }

                    List <UInt64> newBlocks = new List <ulong>();

                    switch (SetCouplingMode)
                    {
                    case CouplingMode.ECB:
                        newBlocks = ECB(isEncrypt);
                        break;

                    case CouplingMode.CBC:
                        newBlocks = CBC(isEncrypt);
                        break;

                    case CouplingMode.CFB:
                        newBlocks = CFB(isEncrypt);
                        break;

                    case CouplingMode.OFB:
                        newBlocks = OFB(isEncrypt);
                        break;
                    }

                    for (int i = 0; i < newBlocks.Count; i++)
                    {
                        var tmpBlock = newBlocks[i];
                        if (!isEncrypt && i == newBlocks.Count - 1)
                        {
                            var tmp = MakeCuttedBlock(tmpBlock);
                            if (tmp.Length != 10)
                            {
                                foreach (var b in tmp)
                                {
                                    oFileStream.Write(b);
                                }
                            }
                        }
                        else
                        {
                            oFileStream.Write(tmpBlock);
                            //Write(tmpBlock.ToString());
                        }
                        codingValue++;
                        SetValue.Invoke(codingValue);
                    }

                    EncodeFinished($"Файл\n{iFileName}\nуспешно зашифрован\n\nСоздан файл\n{oFileName}\nс шифротекстом");
                }
            }
        }