C# 加密算法RC4

using System;
using System.Text;

namespace XiaoFeng.Cryptography
{
    /// 
    /// RC4加密解密
    /// version : 1.0.0
    /// 
    public class RC4Crypto : ICryptography
    {
        #region 构造器
        /// 
        /// 无参构造器
        /// 
        public RC4Crypto()
        {
            this.Key = "wwwzhuovicom";
            this.encoding = Encoding.Default;
            this.encoderMode = EncoderMode.Base64Encoder;
        }
        #endregion

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

        #region 方法
        /// 
        /// 编码模式
        /// 
        public enum EncoderMode
        {
            /// 
            /// Base64模式
            /// 
            Base64Encoder,
            /// 
            /// 16进制模式
            /// 
            HexEncoder
        }
        /// 
        /// 加密
        /// 
        /// 明文
        /// 
        public String Encrypt(String data)
        {
            return this.Encrypt(data, this.Key, this.encoderMode);
        }
        /// 
        /// 带编码模式的字符串加密
        /// 
        /// 要加密的数据
        /// 密码
        /// 编码模式
        /// 加密后经过编码的字符串
        public String Encrypt(String data, String key, EncoderMode em =  EncoderMode.Base64Encoder)
        {
            if (data.IsNullOrEmpty()) return "";
            if (key.IsNullOrEmpty()) 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));
        }
        /// 
        /// 解密
        /// 
        /// 密文
        /// 
        public String Decrypt(String data)
        {
            return this.Decrypt(data, this.Key, this.encoderMode);
        }
        /// 
        /// 带编码模式的字符串解密
        /// 
        /// 要解密的数据
        /// 密码
        /// 编码模式
        /// 明文
        public String Decrypt(String data, String key, EncoderMode em)
        {
            if (data.IsNullOrEmpty()) return "";
            if (key.IsNullOrEmpty()) 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));
        }
        /// 
        /// 加密
        /// 
        /// 要加密的数据
        /// 密码
        /// 加密后经过默认编码的字符串
        public String Encrypt(String data, String key)
        {
            return this.Encrypt(data, key, this.encoderMode);
        }
        /// 
        /// 解密
        /// 
        /// 要解密的经过编码的数据
        /// 密码
        /// 明文
        public String Decrypt(String data, String key)
        {
            return this.Decrypt(data, key, this.encoderMode);
        }
        /// 
        /// 16进制转换字节
        /// 
        /// 字符串
        /// 
        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;
        }
        /// 
        /// 字节转换16进制
        /// 
        /// 字节
        /// 
        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();
        }
        /// 
        /// 加密字节
        /// 
        /// 节字
        /// key
        /// 
        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;
        }
        /// 
        /// 解密字节
        /// 
        /// 字节
        /// key
        /// 
        private Byte[] Decrypt(Byte[] data, String key)
        {
            return Encrypt(data, key);
        }
        /// 
        /// 打乱密码
        /// 
        /// 密码
        /// 密码箱长度
        /// 打乱后的密码
        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
    }
}

你可能感兴趣的:(加密算法)