• C# RC4 代码实现

    2012-09-07 15:26:54
    using System; using System.Collections.Generic; using System.Text; namespace NewLife.Cryptography { /// /// 加密类基类 /// public class CryptoBase { public enum EncoderMode
    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace NewLife.Cryptography
    {
        /// <summary>
        /// 加密类基类
        /// </summary>
        public class CryptoBase
        {
            public enum EncoderMode 
            { 
                Base64Encoder, 
                HexEncoder 
            };
    
    
            /// <summary>
            /// 编码转换器,用于字节码和字符串之间的转换,默认为本机编码
            /// </summary>
            static public Encoding Encode = Encoding.Default;
            public EncoderMode Mode = EncoderMode.Base64Encoder;
            /// <summary>
            /// 带编码模式的字符串加密
            /// </summary>
            /// <param name="data">要加密的数据</param>
            /// <param name="pass">密码</param>
            /// <param name="em">编码模式</param>
            /// <returns>加密后经过编码的字符串</returns>
            public String Encrypt(String data, String pass, CryptoBase.EncoderMode em)
            {
                if (data == null || pass == null) return null;
                if (em == EncoderMode.Base64Encoder)
                    return Convert.ToBase64String(EncryptEx(Encode.GetBytes(data), pass));
                else
                    return ByteToHex(EncryptEx(Encode.GetBytes(data), pass));
            }
            /// <summary>
            /// 带编码模式的字符串解密
            /// </summary>
            /// <param name="data">要解密的数据</param>
            /// <param name="pass">密码</param>
            /// <param name="em">编码模式</param>
            /// <returns>明文</returns>
            public String Decrypt(String data, String pass, CryptoBase.EncoderMode em)
            {
                if (data == null || pass == null) return null;
                if (em == EncoderMode.Base64Encoder)
                    return Encode.GetString(DecryptEx(Convert.FromBase64String(data), pass));
                else
                    return Encode.GetString(DecryptEx(HexToByte(data), pass));
            }
            /// <summary>
            /// 加密
            /// </summary>
            /// <param name="data">要加密的数据</param>
            /// <param name="pass">密码</param>
            /// <returns>加密后经过默认编码的字符串</returns>
            public String Encrypt(String data, String pass)
            {
                return Encrypt(data, pass, Mode);
            }
            /// <summary>
            /// 解密
            /// </summary>
            /// <param name="data">要解密的经过编码的数据</param>
            /// <param name="pass">密码</param>
            /// <returns>明文</returns>
            public String Decrypt(String data, String pass)
            {
                return Decrypt(data, pass, Mode);
            }
            /// <summary>
            /// 加密
            /// </summary>
            /// <param name="data">要加密的数据</param>
            /// <param name="pass">密钥</param>
            /// <returns>密文</returns>
            virtual public Byte[] EncryptEx(Byte[] data, String pass) { return null; }
            /// <summary>
            /// 解密
            /// </summary>
            /// <param name="data">要解密的数据</param>
            /// <param name="pass">密码</param>
            /// <returns>明文</returns>
            virtual public Byte[] DecryptEx(Byte[] data, String pass) { return null; }
            static public Byte[] HexToByte(String szHex)
            {
                // 两个十六进制代表一个字节
                Int32 iLen = szHex.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)szHex[i * 2] - (((UInt32)szHex[i * 2] >= (UInt32)'A') ? (UInt32)'A' - 10 : (UInt32)'0');
                    if (tmp1 >= 16) return null;
                    tmp2 = (UInt32)szHex[i * 2 + 1] - (((UInt32)szHex[i * 2 + 1] >= (UInt32)'A') ? (UInt32)'A' - 10 : (UInt32)'0');
                    if (tmp2 >= 16) return null;
                    pbBuffer[i] = (Byte)(tmp1 * 16 + tmp2);
                }
                return pbBuffer;
            }
            static public String ByteToHex(Byte[] vByte)
            {
                if (vByte == null || vByte.Length < 1) return null;
                StringBuilder sb = new StringBuilder(vByte.Length * 2);
                for (int i = 0; i < vByte.Length; i++)
                {
                    if ((UInt32)vByte[i] < 0) return null;
                    UInt32 k = (UInt32)vByte[i] / 16;
                    sb.Append((Char)(k + ((k > 9) ? 'A' - 10 : '0')));
                    k = (UInt32)vByte[i] % 16;
                    sb.Append((Char)(k + ((k > 9) ? 'A' - 10 : '0')));
                }
                return sb.ToString();
            }
        }
        public class RC4Crypto : CryptoBase
        {
            static public RC4Crypto RC4 = new RC4Crypto();
            public override Byte[] EncryptEx(Byte[] data, String pass)
            {
                if (data == null || pass == null) return null;
                Byte[] output = new Byte[data.Length];
                Int64 i = 0;
                Int64 j = 0;
                Byte[] mBox = GetKey(Encode.GetBytes(pass), 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) % mBox.Length];
                    // mBox[j] 一定比 mBox.Length 小,不需要在取模
                    Byte b = mBox[(mBox[i] + mBox[j]) % mBox.Length];
                    output[offset] = (Byte)((Int32)a ^ (Int32)b);
                }
                return output;
            }
            public override Byte[] DecryptEx(Byte[] data, String pass)
            {
                return EncryptEx(data, pass);
            }
            /// <summary>
            /// 打乱密码
            /// </summary>
            /// <param name="pass">密码</param>
            /// <param name="kLen">密码箱长度</param>
            /// <returns>打乱后的密码</returns>
            static 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;
            }
        }
    }


    展开全文
  • C# 加密算法RC4

    2018-07-05 09:19:17
    using System; using System.Text; namespace XiaoFeng.Cryptography { /// &... /// RC4加密解密 /// version : 1.0.0 /// &lt;/summary&gt; public class RC4Crypto : ICryptography ...
    using System;
    using System.Text;
    
    namespace XiaoFeng.Cryptography
    {
        /// <summary>
        /// RC4加密解密
        /// version : 1.0.0
        /// </summary>
        public class RC4Crypto : ICryptography
        {
            #region 构造器
            /// <summary>
            /// 无参构造器
            /// </summary>
            public RC4Crypto()
            {
                this.Key = "wwwzhuovicom";
                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 (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));
            }
            /// <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 (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));
            }
            /// <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
        }
    }
    展开全文
  • 这段c#代码极为简练,以非常简单的方法来实现RC4加密。 本代码在.net 4.5 以及mono 3.2.6测试成功
  • C# Rc4加密解密

    2019-10-12 16:00:39
    Rc4是对称的加密算法,基于密钥流加密,一般在工作中,有些信息需要进行加密处理,通常是多重加密,Rc4有时候会成为其中的一重加密算法。示例代码如下: private static string m_key = "TLn49vR0wpJleyvS"; ...

    C# Rc4加密解密

    简介

    Rc4是对称的加密算法,基于密钥流加密,一般在工作中,有些信息需要进行加密处理,通常是多重加密,Rc4有时候会成为其中的一重加密算法。示例代码如下:

    	private static string m_key = "TLn49vR0wpJleyvS";
    	private static string m_filePath = "config.ini";
    
    	public static void CryptConfig()
    	{
    		string str =BJUtility.ReadFile(m_filePath);
    		byte[] byte = str.toByte();
    		string cryptStr = Decode(byte,m_key);
    		BJUtility.WriteFile(m_filePath,cryptStr);
    	}
    
    	private static string Decode(byte[] data,string keyStr)
        {
            byte[] contents = data;
            byte[] results = new byte[contents.Length];
    
            byte[] pwds = System.Text.Encoding.UTF8.GetBytes(keyStr);
    
            byte[] key = new byte[256];
            byte[] box = new byte[256];
            for (int i = 0; i < 256; i++)
            {
                key[i] = pwds[i % pwds.Length];
                box[i] = (byte)i;
            }
    
            for (int j = 0, i = 0; i < 256; i++)
            {
                j = (j + box[i] + key[i]) % 256;
                byte tmp = box[i];
                box[i] = box[j];
                box[j] = tmp;
            }
    
            for (int a = 0, j = 0, i = 0; i < contents.Length; i++)
            {
                a = (a + 1) % 256;
                j = (j + box[a]) % 256;
    
                byte tmp = box[a];
                box[a] = box[j];
                box[j] = tmp;
                byte k = box[((box[a] + box[j]) % 256)];
    
                results[i] = (byte)(contents[i] ^ k);
            }
            return System.Text.Encoding.UTF8.GetString(results);
        }
    

    其中密钥的生成一般也是由算法生成的,这里简要去网站随机生成了一个。再来说说Rc4的不足,由于它是基于密钥流进行加密的,因此当你的密钥被人熟知,你这段内容也可以很轻松的被人破译。但是由于它是对称性加密,可以毫不费力的多加几重加密方式,保证信息很难被破译。

    展开全文
  • C# RC4Engine(加密/解密)

    2015-06-16 19:18:56
    RC4是密钥长度可变的流加密算法簇。其核心部分的S-box长度无限制 ,该算法的速度可以达到DES加密的10倍左右,且具有很高级别的非 线性。RC4包括初始化算法(KSA)与伪随机数生成算法(PRGA)两个部分。  public static ...

    RC4是密钥长度可变的流加密算法簇。其核心部分的S-box长度无限制

    ,该算法的速度可以达到DES加密的10倍左右,且具有很高级别的非

    线性。RC4包括初始化算法(KSA)与伪随机数生成算法(PRGA)两个部分。

            public static byte[] rc4_init(string key)
            {
                byte[] box = new byte[255];
                for (int i = 0; i < 255; i++)
                    box[i] = (byte)i;
                for (int i = 0, j = 0; i < 255; i++)
                {
                    j = (j + box[i] + key[i % key.Length]) % 255;
                    byte b = box[i];
                    box[i] = box[j];
                    box[j] = b;
                }
                return box;
            }

    上面是初始化算法主要随机打烂S-box中的密钥序列顺序与初始化

    Key值。首先定义一个byte[] box缓冲区,然后进行首次的循环,

    把S-Box赋入初值,第二次循环进行随机打烂S-box密钥序列。

            public static string rc4_crypt(string key, string value)
            {
                byte[] box = rc4_init(key);
                char[] buffer = value.ToCharArray();
                for (int i = 0, low = 0, high = 0, mid; i < buffer.Length; i++)
                {
                    low = (low + 1) % 255;
                    high = (high + box[i % 255]) % 255;


                    byte b = box[low];
                    box[low] = box[high];
                    box[high] = b;


                    mid = (box[low] + box[high]) % 255;
                    buffer[i] ^= (char)box[mid];
                }
                return new string(buffer);
            }

    上面是伪子随机数生成部分,首先调用rc4_init(key)获取到

    被随机打烂的密钥序列S-box,然后通过把value转换成buffer

    缓冲区,我们按照buffer.Length进行计次循环,首先我们需要

    计算伪低位索引 low = (low + 1) % 255; 然后计算伪高位索引

    high = (high + box[i % 255]) % 255,到了这里我们开始交换

    两者的值,这里为伪随机数生成部分,下面则通过把两者所以

    S-box中的值相加% 255取得伪中间索引,与buffer[i] ^= (ch

    ar)box[mid]; 进行异或并赋值 最后在把buffer返回给调用方。

    这个算法就基本完成了 如如果我解释的并不是很清楚 你也可以

    点开这个链接,http://baike.baidu.com/link?url=PpEcozy2oqUap

    zng83HJ3-9jknr_qbg-mIUReRYowP215l-RP4R5qEjw8jn0X67QG

    M8mJJlAIuEUhtPQNC4QOa

    RC4 应用代码:

            public static void Main(string[] args)
            {
                string enc = rc4_crypt("China", "Japan"); // 加密
                string dec = rc4_crypt("China", enc); // 解密
            }

    展开全文
  • C# RC4加密,解密算法

    2019-01-03 10:59:11
    完整代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; namespace RC.Web.Utility ... /// Rc4加密 解密工具类 /// &lt;/s...

    完整代码如下:

    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;
            }
            #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 (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));
            }
            /// <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 (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));
            }
            /// <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);
            }
        }
    }

     

    展开全文
  • rc4算法,原理,以密匙生成256位的密匙流,然后以车轮式滚过源数据异或加密。 1 /* 2 * 由SharpDevelop创建。 3 * 用户: YISH 4 * 日期: 04/04/2015 5 * 时间: 03:01 6 * 7 * 要改变这种模板请...
  • 这次的RC4有个基类CryptoBase,这个基类,是为了为多种加密算法提供统一接口而设计的,我这里实现了RC4、DES、RSA三种算法。和C++中一样,C#这里也要有一个十六进制字符串转换函数来编码解码。 经过测试,...
  • RC4算法的特点是算法简单,运行速度快,而且密钥长度是可变的,可变范围为1-256字节(8-2048比特), 在如今技术支持的前提下,当密钥长度为128比特时,用暴力法搜索密钥已经不太可行,所以可以预见 RC4的密钥范围...
  • RC4加密算法 c#实现

    2018-12-03 18:33:12
    最近因为用一个简单的加密的方法而被人说是对技术没追求 让我有点难受 其实用简单的加密方法是因为考虑到了加载资源时的解密效率和我看了腾讯一本安全书籍里面天天来战的方法也是类似的简单加密 还有最近看了一本叫...
  • 最近公司买了wifi探针设备,主要探测用户进出时间以及mac地址 ... 流程: 获取设备post过来的body内容-->判空-->解密(前16...网上随便搜了个RC4 JAVA版本的解密程序,使用。结果一团乱码 网上代码: package com.powercn
  • RC4  RC4是流密码算法,流算法以密钥作为种子产生密钥流,然后明文比特流和密钥流进行异或以实现加密.  RC4加密算法密钥长度可变,没有填充的麻烦. 函数  OpenSSL中RC4加密算法主要有两个函数,在openssl/rc4.h中找到...
  • OpenSSL之RC4加解密

    2019-06-26 18:55:14
    在项目中使用了RC4解密,为了解密我生成了和加密端一样的key,但是解密时老是不成功,直接使用简单的代码进行测试就成功了。细查原因,项目中的加解密调用不对称,加密方在生成key之后使用key做了一次加密测试,而...
  • RC4于1987年提出,和DES算法一样,是一种对称加密算法,也就是说使用的密钥为单钥(或称为私钥)。但不同于DES的是,RC4不是对明文进行分组处理,而是字节流的方式依次加密明文中的每一个字节,解密的时候也是依次对...
  • 部分注释代码是来源于网络,为了方便大家对库函数的理解,在...openssl/rc4.h> #include <string.h> #include <iostream> using namespace std; void TestRc4Encrypt() { char code[64] = { 0 }; ...
  • PHP RC4 加解密类

    2017-10-11 14:47:56
    //RC4算法 //对称加密,加解密使用同一套函数 function rc4 ($pwd, $data)//$pwd密钥 $data需加密字符串 { $key[] =""; $box[] =""; $cipher=''; $pwd_length = strlen($pwd); $data_length = strlen($data); ...
  • RC4算法

    2013-01-23 07:12:56
    RC4加密算法   RC4加密算法是大名鼎鼎的RSA三人组中的头号人物Ron Rivest在1987年设计的密钥长度可变的流加密算法簇。之所以称其为簇,是由于其核心部分的S-box长度可为任意,但一般为256字节。该算法的速度...
  • RC5加解密算法的C#实现,详细的事例,让你轻松搞定,还等什么
  • rc5算法C源码和C#源码

    2020-05-29 23:30:05
    rc5分组密码算法是1994由麻萨诸塞技术研究所的Ronald l. rivest教授发明的,并由rsa实验室分析。它是参数可变的分组密码算法,三个可变的参数是:分组大小、密钥大小和加密轮数。rc5算法C源码和C#源码。
1 2 3 4 5 ... 20
收藏数 10,764
精华内容 4,305