12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
-
- 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);
- }
- /// <summary>
- /// 直接对原数组进行rc4加密
- /// </summary>
- /// <param name="bytes"></param>
- /// <param name="offset"></param>
- /// <param name="len"></param>
- /// <returns></returns>
- 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);
- }
- }
- }
|