Exemple #1
0
        private String Crypt(String text, CryptMode mode)
        {
            StringBuilder stringBuilder = new StringBuilder();
            String        keyWord       = GetRepeatKeyWord(text.Length);

            for (var i = 0; i < text.Length; i++)
            {
                Char textSymbol    = text[i];
                Char keyWordSymbol = keyWord[i];
                var  value         = char.ToLower(textSymbol);
                if (!Language.Alphabet.Contains(value))
                {
                    stringBuilder.Append(textSymbol);
                    continue;
                }

                var index = (Language.Count + Language.Alphabet.IndexOf(value, StringComparison.OrdinalIgnoreCase) +
                             Language.Alphabet.IndexOf(keyWordSymbol, StringComparison.OrdinalIgnoreCase) *
                             (Int32)mode) % Language.Count;
                var symbolNew = Language.GetSymbolByIndex(index);
                stringBuilder.Append
                (
                    char.IsUpper(textSymbol) ? char.ToUpper(symbolNew) : symbolNew
                );
            }

            return(stringBuilder.ToString());
        }
Exemple #2
0
 public Cipher(string inputText, LanguageMode language, string key, CryptMode cryptCrypt)
 {
     InputText = inputText;
     Language  = language;
     Key       = key;
     Crypt     = cryptCrypt;
 }
        /// <summary>
        /// 获取加密后的字符串
        /// </summary>
        /// <param name="selValue"></param>
        /// <param name="str"></param>
        /// <param name="cm"></param>
        /// <returns></returns>
        public string GetStr(string selValue, string str, CryptMode cm)
        {
            XMLOperation xmlOp = new XMLOperation();
            DataSet ds = xmlOp.GetAllInfo("TypeList.xml");

            EncryptContext ec = new EncryptContext();
            DataRow dr = ((DataRow[])ds.Tables[0].Select("name='" + selValue + "'"))[0];
            ec.SetBehavior((EncryptSuper)Assembly.Load(ASSEMBLY_NAME).CreateInstance(ASSEMBLY_NAME + "." + dr["class"].ToString(), false, BindingFlags.Default, null, null, null, null));
            if (cm == CryptMode.Encrypt) return ec.GetEnResult(str);
            else return ec.GetDeResult(str);
        }
Exemple #4
0
        private CryptResult Crypt(CryptMode mode, string storeName, ref string password)
        {
            var e      = new CryptEventArgs(_plugin.PluginNumber, _cryptoNumber, (int)mode, storeName, password);
            var result = GetCryptResult(_plugin.OnTcPluginEvent(e));

            if (result == CryptResult.OK)
            {
                password = e.Password;
            }

            return(result);
        }
Exemple #5
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (txtFileName.Text == "")
            {
                MessageBox.Show(this, lang.PleaseSelectFile, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // Save path
            Path = txtFileName.Text;

            // Check if path valid
            try
            {
                Files.IsFileNameValid(Path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // If object does not exist
            if (!File.Exists(Path) && !Directory.Exists(Path))
            {
                MessageBox.Show(this, lang.SpecifiedObjectNotFound, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // If decrypt directory => ERROR
            if (Files.IsPathDirectory(Path) && comboBoxMode.SelectedItem.ToString() == lang.Decrypt)
            {
                MessageBox.Show(this, lang.SpecifiedObjectNotFound, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // Save mode
            if (comboBoxMode.SelectedIndex == 1)
            {
                Mode = CryptMode.Encrypt;
            }
            else if (comboBoxMode.SelectedIndex == 2)
            {
                Mode = CryptMode.Decrypt;
            }
            else
            {
                Mode = Files.GetCryptModeByExt(Path);
            }

            // By closing, Program.cs will run the end of the program.
            this.Close();
        }
        private ArchiveResult Crypt(CryptMode mode, string connectionName, ref string password)
        {
            var passwordBuilder = new StringBuilder(password);

            passwordBuilder.EnsureCapacity(Win32.MAX_PATH);

            var result = callback(
                cryptoNumber,
                (int)mode,
                !string.IsNullOrEmpty(connectionName) ? connectionName : null,
                passwordBuilder,
                Win32.MAX_PATH);

            password = passwordBuilder.ToString();

            return((ArchiveResult)result);
        }
Exemple #7
0
        public static MemoryStream BBTAGMD5CryptStream(MemoryStream ms, string path, CryptMode mode,
                                                       bool leaveOpen = false)
        {
            ms.Position = 0;

            var file     = path;
            var filename = Path.GetFileName(file);

            if (mode == CryptMode.Decrypt)
            {
                index = StringToByteArray(MD5Tools.CreateMD5(filename))[7] % 43;
            }
            else if (mode == CryptMode.Encrypt && filename.Length > 32 && MD5Tools.IsMD5(filename.Substring(0, 32)))
            {
                filename = filename.Substring(0, 32);
                index    = StringToByteArray(MD5Tools.CreateMD5(filename))[7] % 43;
            }
            else if (mode == CryptMode.Encrypt && file.LastIndexOf("data") >= 0)
            {
                var datapath    = file.Substring(file.LastIndexOf("data"), file.Length - file.LastIndexOf("data"));
                var filenameMD5 = MD5Tools.CreateMD5(datapath.Replace("\\", "/"));
                index = StringToByteArray(MD5Tools.CreateMD5(filenameMD5))[7] % 43;
            }
            else
            {
                return(ms);
            }

            var count = (int)(ms.Length - ms.Position);

            var bytes     = new byte[count];
            var newStream = new MemoryStream();

            using (var reader = new BinaryReader(ms, Encoding.Default, leaveOpen))
            {
                for (var i = 0; i < count; i++)
                {
                    newStream.WriteByte((byte)(EncryptionKey[index] ^ reader.ReadByte()));
                    index = (index + 1) % 43;
                }
            }

            newStream.Position = 0;

            return(newStream);
        }
Exemple #8
0
        private String Crypt(String text, CryptMode mode)
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (Char symbol in text)
            {
                var value = symbol.ToString().ToLower();
                if (!Language.Alphabet.Contains(value))
                {
                    stringBuilder.Append(symbol);
                    continue;
                }

                var index = (Language.Count + Language.Alphabet.IndexOf(value, StringComparison.OrdinalIgnoreCase) +
                             _step * (Int32) mode) % Language.Count;
                var symbolNew = Language.GetSymbolByIndex(index);
                stringBuilder.Append
                (
                    char.IsUpper(symbol) ? char.ToUpper(symbolNew) : symbolNew
                );
            }

            return stringBuilder.ToString();
        }
        public static MemoryStream FPACCryptStream(Stream stream, string path, CryptMode mode, bool onlyHeader = false)
        {
            stream.Position = 0;
            var ms = new MemoryStream();

            var fileName = Path.GetFileName(path).ToUpperInvariant();
            var fileSize = stream.Length;

            uint decryptInitKey = 0x0;

            foreach (var c in fileName)
            {
                decryptInitKey *= 0x89;
                decryptInitKey += Convert.ToByte(c);
            }

            var decryptKey = new uint[0x270];

            decryptKey[0] = decryptInitKey;

            for (uint i = 1; i < decryptKey.Length; i++)
            {
                var val  = decryptKey[i - 1];
                var val2 = val >> 0x1E;
                val2         ^= val;
                val2         *= 0x6C078965;
                val2         += i;
                decryptKey[i] = val2;
            }

            uint repeatVal = 1;
            uint xorVal    = 0x43415046;

            uint value;

            uint decryptIndex = 0;

            var size = onlyHeader && fileSize >= 40 ? 40 : fileSize;

            for (var byteIndex = 0; byteIndex < size; byteIndex += 4)
            {
                --repeatVal;
                if (repeatVal == 0)
                {
                    decryptIndex = 0;

                    uint index = 0;

                    repeatVal = 0x270;

                    for (uint i = 0xE3; i > 0; i--)
                    {
                        var val    = decryptKey[index + 1];
                        var value2 = decryptKey[index];
                        value2           ^= val;
                        value2           &= 0x7FFFFFFE;
                        value2           ^= decryptKey[index];
                        val              &= 1;
                        value2          >>= 1;
                        val               = (uint)(val * -1);
                        val              &= 0x9908B0DF;
                        value2           ^= val;
                        value2           ^= decryptKey[index + 0x18D];
                        decryptKey[index] = value2;
                        index++;
                    }

                    for (uint i = 0x18C; i > 0; i--)
                    {
                        var val    = decryptKey[index + 1];
                        var value2 = decryptKey[index];
                        value2           ^= val;
                        value2           &= 0x7FFFFFFE;
                        value2           ^= decryptKey[index];
                        val              &= 1;
                        value2          >>= 1;
                        val               = (uint)(val * -1);
                        val              &= 0x9908B0DF;
                        value2           ^= val;
                        value2           ^= decryptKey[index - 0xE3];
                        decryptKey[index] = value2;
                        index++;
                    }

                    var valOne  = decryptKey[0];
                    var valLast = decryptKey[index];
                    valLast          ^= valOne;
                    valLast          &= 0x7FFFFFFE;
                    valLast          ^= decryptKey[index];
                    valOne           &= 1;
                    valLast         >>= 1;
                    valOne            = (uint)(valOne * -1);
                    valOne           &= 0x9908B0DF;
                    valLast          ^= valOne;
                    valLast          ^= decryptKey[index - 0xE3];
                    decryptKey[index] = valLast;
                }

                value = decryptKey[decryptIndex];
                decryptIndex++;
                var val2 = value;
                val2 >>= 0x0B;
                value ^= val2;
                val2   = value;
                val2  &= 0xFF3A58AD;
                val2 <<= 0x07;
                value ^= val2;
                val2   = value;
                val2  &= 0xFFFFDF8C;
                val2 <<= 0x0F;
                value ^= val2;
                val2   = value;
                val2 >>= 0x12;
                value ^= val2;

                var tmpBytes = new byte[4];
                stream.Read(tmpBytes, 0, 4);
                value ^= BitConverter.ToUInt32(tmpBytes, 0);

                value ^= xorVal;
                var k = 0;
                foreach (var b in BitConverter.GetBytes(value))
                {
                    ms.WriteByte(b);
                    k++;
                }

                if (mode == CryptMode.Encrypt)
                {
                    value = BitConverter.ToUInt32(tmpBytes, 0);
                }

                xorVal = value;

                if (byteIndex == 0 && mode == CryptMode.Decrypt)
                {
                    if (xorVal != 0x43415046 && xorVal != 0x53414644)
                    {
                        ms.Close();
                        ms.Dispose();
                        ms = new MemoryStream();
                        stream.CopyTo(ms);
                        ms.Position = 0;
                        return(ms);
                    }

                    if (xorVal == 0x53414644)
                    {
                        size = fileSize;
                    }
                }
            }

            ms.Position = 0;
            return(ms);
        }
        private FileOperationResult Crypt(CryptMode mode, string connectionName, ref string password)
        {
            var passwordBuilder = new StringBuilder(password);
            passwordBuilder.EnsureCapacity(Win32.MAX_PATH);

            var result = callback(
                pluginNumber,
                cryptoNumber,
                (int)mode,
                !string.IsNullOrEmpty(connectionName) ? connectionName : null,
                passwordBuilder,
                Win32.MAX_PATH);

            password = passwordBuilder.ToString();

            return (FileOperationResult)result;
        }
Exemple #11
0
 /// <summary>
 /// 签名加密
 /// </summary>
 /// <param name="target"></param>
 /// <param name="cryptMode">加密方式</param>
 /// <returns></returns>
 public static string Sign(this string target, CryptMode cryptMode=CryptMode.Md5)
 {
     if (cryptMode == CryptMode.Md5)
     {
         return target.Md5();
     }
     return target.Hmac("SYSTEM.SIGNIN.SIGN.ENCRYPT");
 }
Exemple #12
0
        public static byte[] AesCrypt(byte[] keyBytes, byte[] ivBytes, byte[] inputBytes, CryptMode cryptMode)
        {
            using (var aes = new RijndaelManaged())
            {
                aes.Mode = CipherMode.CBC;

                aes.Key = keyBytes;
                aes.IV  = ivBytes;

                aes.Padding = PaddingMode.None; // mongocrypt level is responsible for padding

                using (var encrypto = CreateCryptoTransform(aes))
                {
                    byte[] encryptedBytes = encrypto.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
                    return(encryptedBytes);
                }

                ICryptoTransform CreateCryptoTransform(RijndaelManaged rijndaelManaged)
                {
                    switch (cryptMode)
                    {
                    case CryptMode.Encrypt: return(rijndaelManaged.CreateEncryptor());

                    case CryptMode.Decrypt: return(rijndaelManaged.CreateDecryptor());

                    default: throw new InvalidOperationException($"Unsupported crypt mode {cryptMode}.");     // should not be reached
                    }
                }
            }
        }