using System.Text; using XGame.Framework.Utils; namespace XGame.Framework.Network { public class RC4Encryptor : IMsgEncryptor { private string _key; private byte[] _keyBytes; public void SetKey(string key) { _key = key; _keyBytes = Encoding.UTF8.GetBytes(key); } /// /// 直接对原数组进行rc4加密 /// /// /// /// /// private byte[] RC4(byte[] bytes, int offset, int len) { SecurityUtils.RC4(_keyBytes, bytes, offset, len, bytes); return bytes; } public void Encrypt(ref byte[] bytes, ref int offset, ref int length) { if (_keyBytes == null) { Log.Error("RC4密钥为空."); return; } RC4(bytes, offset, length); } public void Decrypt(ref byte[] bytes, ref int offset, ref int length) { if (_keyBytes == null) { Log.Error("RC4密钥为空."); return; } RC4(bytes, offset, length); } } }