• 如何使用RC4进行数据加减密[转]。


    namespace CryptoRC4
    {
        using System;
        using System.Text;

        public class clsRC4Engine
        {
            private static long m_nBoxLen = 255;

            protected clsRC4Engine()
            {

            }
            private static void GetKeyBytes( string Key, out byte[] m_nBox )
            {
                long index2 = 0;
                m_nBox = new byte[m_nBoxLen]; 
                Encoding ascii      = Encoding.ASCII;
                Encoding unicode    = Encoding.Unicode;
                byte[] asciiBytes = Encoding.Convert(unicode,ascii, unicode.GetBytes( Key ));
               char[] asciiChars = new char[ascii.GetCharCount(asciiBytes,0,asciiBytes.Length)];

                ascii.GetChars(asciiBytes,0,asciiBytes.Length,asciiChars,0);
                long KeyLen = Key.Length;
                for ( long count = 0; count < m_nBoxLen ; count ++ )
                {
                    m_nBox[count] = (byte)count;
                }
                for ( long count = 0; count < m_nBoxLen ; count ++ )
                {

                    index2 = (index2 + m_nBox[count] + asciiChars[ count % KeyLen ]) % m_nBoxLen;
                    byte temp       = m_nBox[count];
                    m_nBox[count]   = m_nBox[index2];
                    m_nBox[index2]  = temp;
                }
            }

            private static bool GetEncryptBytes( string sData, byte[] m_nBox,out byte[] EncryptedBytes )
            {
                EncryptedBytes = null;
                bool toRet = true;
                try
                {
                    long i=0;
                    long j=0;
                    Encoding enc_default = Encoding.Unicode;
                    byte[] input  = enc_default.GetBytes( sData );
                    EncryptedBytes = new byte[input.Length];
                    byte[] n_LocBox = new byte[m_nBoxLen];
                    m_nBox.CopyTo(n_LocBox,0);
                    long ChipherLen = input.Length + 1;
                    for ( long offset = 0; offset < input.Length ; offset++ )
                    {
                        i = ( i + 1 ) % m_nBoxLen;
                        j = ( j + n_LocBox[i] ) %  m_nBoxLen;
                        byte temp =  n_LocBox[i];
                        n_LocBox[i] = n_LocBox[j];
                        n_LocBox[j] = temp;
                        byte a = input[offset];
                        byte b = n_LocBox[(n_LocBox[i]+n_LocBox[j])% m_nBoxLen];
                        EncryptedBytes[offset] = (byte)((int)a^(int)b);
                    } 
                }
                catch
                {
                    EncryptedBytes = null;

                    toRet = false;
                }
                return toRet;
            }

            public static bool Encrypt( string sData, string Key, out string EncryptedString )
            {
                EncryptedString = null;

                if( sData == null || Key == null ) return false;

                byte[] m_nBox;

                GetKeyBytes( Key, out m_nBox );

              

                byte[] output;

                if( GetEncryptBytes( sData, m_nBox, out output ) )

                {

                    // Convert data to hex-data

                    EncryptedString = "";

                    for( int i = 0; i < output.Length; i++ )

                        EncryptedString += output[i].ToString( "X2" );

     

                    return true;

                }

                else

                    return false;
            }

     

            /// <summary>

            /// Decrypt data using specific key

            /// </summary>

            /// <param name="EncryptedString"></param>

            /// <param name="Key"></param>

            /// <param name="sData"></param>

            /// <returns></returns>

            public static bool Decrypt( string EncryptedString, string Key, out string sData )

            {

                sData = null;

                if( EncryptedString == null || Key == null ) return false;

                else if( EncryptedString.Length % 2 != 0 ) return false;

                byte[] m_nBox;

                GetKeyBytes( Key, out m_nBox );

     

                // Convert data from hex-data to string

                byte[] bData = new byte[EncryptedString.Length / 2];

                for( int i = 0; i < bData.Length; i++ )

                    bData[i] = Convert.ToByte( EncryptedString.Substring( i * 2, 2 ), 16 );

     

                EncryptedString = Encoding.Unicode.GetString( bData );

              

                byte[] output;

                if( GetEncryptBytes( EncryptedString, m_nBox, out output ) )

                {

                    sData = Encoding.Unicode.GetString( output );

                    return true;

                }

                else

                    return false;

            }

        }

    }

     

    调用如下:

        //Encrypt data

        string strEncryptedString;

        if( clsRC4Engine.Encrypt( strValue, strKey, out strEncryptedString ) )

             MessageBox.Show( strEncryptedString );

     

        //Decrypt data

        string strDecryptedString;

        if( clsRC4Engine.Decrypt( strValue, strKey, out strDecryptedString ) )

             MessageBox.Show( strDecryptedString );

    另外一种加密解密的[简单]:
           public static string encrypt_str( string str )
            {
                string s = "";
                int i_Encrypt = ClsSetConst.m_Set_Encrypt;
                char[] s_array = str.ToCharArray();
                for(int i = 0; i < s_array.Length; i++)
                {
                    int x = ((int)s_array[i]) + i_Encrypt;
                    s += (char)(x);
                }
                return s;
            }
            public void decript_str(string str)
            {
                string s = "";
                int i_Encrypt = ClsSetConst.m_Set_Encrypt;
                char[] s_array = str.ToCharArray();
                for(int i = 0; i < s_array.Length; i++)
                {
                    int x = ((int)s_array[i]) - i_Encrypt;
                    s += (char)x;
                }
            }
  • 相关阅读:
    单选框和复选框(radiobox、checkbox)
    三种alertconfirmprompt弹窗的处理方法
    iframe的切换
    python的class(类)中的object是什么意思?
    loadrunner12自带的机票预订服务,解决httpd: Could not reliably determine the server's fully qualified domain name 问题
    使用错误的用户名和密码也能运行通过
    win10删除IE某些文件导致不可用恢复的方法
    win10系统删除需要Trustedlnstaller权限的文件
    loadrunner各版本对应的ie浏览器版本
    vue之vue-router加深印象
  • 原文地址:https://www.cnblogs.com/bayonetxxx/p/2069618.html
Copyright © 2020-2023  润新知