RC4加密/解密

创建RC4Crypto类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace RC.Web.Utility
{
    /// <summary>
    /// Rc4加密 解密工具类
    /// </summary>
    public class RC4Crypto
    {
        #region 构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        public RC4Crypto()
        {
            this.Key = "123456";
            this.encoding = Encoding.Default;
            this.encoderMode = EncoderMode.Base64Encoder;
        }
        /// <summary>
        /// 有参构造器
        /// </summary>
        /// <param name="key">密码</param>
        public RC4Crypto(string key)
        {
            this.Key =key;
            this.encoding = Encoding.Default;
            this.encoderMode = EncoderMode.Base64Encoder;
        }
        #endregion

        #region 属性
        /// <summary>
        /// 密钥
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 编码
        /// </summary>
        public Encoding encoding { get; set; }
        /// <summary>
        /// 编码模式
        /// </summary>
        public EncoderMode encoderMode { get; set; }
        /// <summary>
        /// 实例方法
        /// </summary>
        public static RC4Crypto RC4 { get { return new RC4Crypto(); } }
        #endregion

        #region 方法
        /// <summary>
        /// 编码模式
        /// </summary>
        public enum EncoderMode
        {
            /// <summary>
            /// Base64模式
            /// </summary>
            Base64Encoder,
            /// <summary>
            /// 16进制模式
            /// </summary>
            HexEncoder
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="data">明文</param>
        /// <returns></returns>
        public String Encrypt(String data)
        {
            return this.Encrypt(data, this.Key, this.encoderMode);
        }
        /// <summary>
        /// 带编码模式的字符串加密
        /// </summary>
        /// <param name="data">要加密的数据</param>
        /// <param name="key">密码</param>
        /// <param name="em">编码模式</param>
        /// <returns>加密后经过编码的字符串</returns>
        public String Encrypt(String data, String key, EncoderMode em = EncoderMode.Base64Encoder)
        {
            if (string.IsNullOrEmpty(data)) return "";
            if (string.IsNullOrEmpty(key)) key = this.Key;
            if (em == EncoderMode.Base64Encoder)
                return Convert.ToBase64String(this.Encrypt(data.GetBytes(this.encoding), key));
            else
                return this.ByteToHex(this.Encrypt(data.GetBytes(this.encoding), key));
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data">密文</param>
        /// <returns></returns>
        public String Decrypt(String data)
        {
            return this.Decrypt(data, this.Key, this.encoderMode);
        }
        /// <summary>
        /// 带编码模式的字符串解密
        /// </summary>
        /// <param name="data">要解密的数据</param>
        /// <param name="key">密码</param>
        /// <param name="em">编码模式</param>
        /// <returns>明文</returns>
        public String Decrypt(String data, String key, EncoderMode em)
        {
            if (string.IsNullOrEmpty(data)) return "";
            if (string.IsNullOrEmpty(key)) key = this.Key;
            if (em == EncoderMode.Base64Encoder)
                return this.encoding.GetString(this.Decrypt(Convert.FromBase64String(data), key));
            else
                return this.encoding.GetString(this.Decrypt(this.HexToByte(data), key));
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="data">要加密的数据</param>
        /// <param name="key">密码</param>
        /// <returns>加密后经过默认编码的字符串</returns>
        public String Encrypt(String data, String key)
        {
            return this.Encrypt(data, key, this.encoderMode);
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data">要解密的经过编码的数据</param>
        /// <param name="key">密码</param>
        /// <returns>明文</returns>
        public String Decrypt(String data, String key)
        {
            return this.Decrypt(data, key, this.encoderMode);
        }
        /// <summary>
        /// 16进制转换字节
        /// </summary>
        /// <param name="hex">字符串</param>
        /// <returns></returns>
        private Byte[] HexToByte(String hex)
        {
            Int32 iLen = hex.Length;
            if (iLen <= 0 || 0 != iLen % 2)
            {
                return null;
            }
            Int32 dwCount = iLen / 2;
            UInt32 tmp1, tmp2;
            Byte[] pbBuffer = new Byte[dwCount];
            for (Int32 i = 0; i < dwCount; i++)
            {
                tmp1 = (UInt32)hex[i * 2] - (((UInt32)hex[i * 2] >= (UInt32)'A') ? (UInt32)'A' - 10 : (UInt32)'0');
                if (tmp1 >= 16) return null;
                tmp2 = (UInt32)hex[i * 2 + 1] - (((UInt32)hex[i * 2 + 1] >= (UInt32)'A') ? (UInt32)'A' - 10 : (UInt32)'0');
                if (tmp2 >= 16) return null;
                pbBuffer[i] = (Byte)(tmp1 * 16 + tmp2);
            }
            return pbBuffer;
        }
        /// <summary>
        /// 字节转换16进制
        /// </summary>
        /// <param name="bytes">字节</param>
        /// <returns></returns>
        private String ByteToHex(Byte[] bytes)
        {
            if (bytes == null || bytes.Length < 1) return null;
            StringBuilder sbr = new StringBuilder(bytes.Length * 2);
            for (int i = 0; i < bytes.Length; i++)
            {
                if ((UInt32)bytes[i] < 0) return null;
                UInt32 k = (UInt32)bytes[i] / 16;
                sbr.Append((Char)(k + ((k > 9) ? 'A' - 10 : '0')));
                k = (UInt32)bytes[i] % 16;
                sbr.Append((Char)(k + ((k > 9) ? 'A' - 10 : '0')));
            }
            return sbr.ToString();
        }
        /// <summary>
        /// 加密字节
        /// </summary>
        /// <param name="data">节字</param>
        /// <param name="key">key</param>
        /// <returns></returns>
        private Byte[] Encrypt(Byte[] data, string key)
        {
            if (data == null || key == null) return null;
            Byte[] output = new Byte[data.Length];
            Int64 i = 0;
            Int64 j = 0;
            Byte[] mBox = GetKey(key.GetBytes(this.encoding), 256);
            for (Int64 offset = 0; offset < data.Length; offset++)
            {
                i = (i + 1) % mBox.Length;
                j = (j + mBox[i]) % mBox.Length;
                Byte temp = mBox[i];
                mBox[i] = mBox[j];
                mBox[j] = temp;
                Byte a = data[offset];
                Byte b = mBox[(mBox[i] + mBox[j]) % mBox.Length];
                output[offset] = (Byte)((Int32)a ^ (Int32)b);
            }
            return output;
        }
        /// <summary>
        /// 解密字节
        /// </summary>
        /// <param name="data">字节</param>
        /// <param name="key">key</param>
        /// <returns></returns>
        private Byte[] Decrypt(Byte[] data, String key)
        {
            return Encrypt(data, key);
        }
        /// <summary>
        /// 打乱密码
        /// </summary>
        /// <param name="pass">密码</param>
        /// <param name="kLen">密码箱长度</param>
        /// <returns>打乱后的密码</returns>
        private Byte[] GetKey(Byte[] pass, Int32 kLen)
        {
            Byte[] mBox = new Byte[kLen];

            for (Int64 i = 0; i < kLen; i++)
            {
                mBox[i] = (Byte)i;
            }
            Int64 j = 0;
            for (Int64 i = 0; i < kLen; i++)
            {
                j = (j + mBox[i] + pass[i % pass.Length]) % kLen;
                Byte temp = mBox[i];
                mBox[i] = mBox[j];
                mBox[j] = temp;
            }
            return mBox;
        }
        #endregion

    }

    /// <summary>
    /// Rc4  扩展方法
    /// </summary>
    public static class Rc4Extend
    {

        /// <summary>
        /// 字符串转数据流
        /// </summary>
        /// <param name="str"></param>
        /// <param name="en"></param>
        /// <returns></returns>
        public static byte[] GetBytes(this string str, Encoding en)
        {

            return en.GetBytes(str);
        }
    }
}

下面是调用部分

using RC.Web.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace RC4
{
    class Program
    {
        static void Main(string[] args)
        {
            RC4Crypto rC4Crypto = new RC4Crypto("b6GsG2J4NYtsNH8zFZYkCfQrWkwk63Si");

            RC4Crypto.EncoderMode mode= (RC4Crypto.EncoderMode)1;//Base64Encoder=0,HexEncoder=1

            string a = rC4Crypto.Encrypt("1234",null,mode);
            Console.WriteLine(a);
            Console.ReadLine();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值