Exemple #1
0
        protected virtual void KeyExchangeReceive(object sender, PacketEventArgs e)
        {
            if (!e.IsComplete)
            {
                var keyExchange = e.Read <KeyExchange>();
                var key         = MD5Hash.GetKey(keyExchange.Nonce.Data);

                var rc4Encryption = new Rc4Encryption(key);

                if (e.Chain.IsInput)
                {
                    if (!IsClient)
                    {
                        Session.Connection.EncodeStack.Setup(new MppcPacker());
                    }
                    Session.Connection.EncodeStack.Setup(rc4Encryption);
                }
                else
                {
                    Session.Connection.DecodeStack.Setup(rc4Encryption);
                    if (IsClient)
                    {
                        Session.Connection.DecodeStack.Setup(new MppcUnpacker());
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 获取加密字符串 UTF-8
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="passKey"></param>
        /// <param name="outString"></param>
        /// <returns></returns>
        public static bool Rc4EncryptFromFileOytString(string filePath, string passKey, out string outString)
        {
            if (!File.Exists(filePath))
            {
                outString = string.Empty;
                return(false);
            }
            #region 读
            byte[] buffer;
            using (FileStream fsRead = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                buffer = new byte[fsRead.Length];
                int n = fsRead.Read(buffer, 0, buffer.Length);
            }
            Rc4Encryption rc4        = new Rc4Encryption();
            byte[]        buffer_out = rc4.EncryptEx(buffer, passKey);
            #endregion

            outString = Encoding.UTF8.GetString(buffer_out);

            if (string.IsNullOrWhiteSpace(outString))
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Exemple #3
0
        /// <summary>
        /// 获取解密文件
        /// </summary>
        /// <param name="inputFilePath"></param>
        /// <param name="outputFilePath"></param>
        /// <param name="passKey"></param>
        public static void Rc4DecryptFile(string inputFilePath, string outputFilePath, string passKey)
        {
            #region 读
            byte[] buffer;
            using (FileStream fsRead = File.Open(inputFilePath, FileMode.Open, FileAccess.Read))
            {
                buffer = new byte[fsRead.Length];
                int n = fsRead.Read(buffer, 0, buffer.Length);
            }
            Rc4Encryption rc4        = new Rc4Encryption();
            byte[]        buffer_out = rc4.DecryptEx(buffer, passKey);
            #endregion

            #region 写
            using (FileStream fsWrite = File.Open(outputFilePath, FileMode.Create, FileAccess.Write))
            {
                fsWrite.Write(buffer_out, 0, buffer_out.Length);
            }
            #endregion
        }
Exemple #4
0
 public void InitializeDec(byte[] key)
 {
     Rc4Dec = new Rc4Encryption(key);
 }
Exemple #5
0
 public void InitializeDec(byte[] key)
 {
     Unpacker = new MppcUnpacker();
     Rc4Dec   = new Rc4Encryption(key);
 }
Exemple #6
0
 public void InitializeEnc(byte[] key)
 {
     Packer = new MppcPacker();
     Rc4Enc = new Rc4Encryption(key);
 }