zoukankan      html  css  js  c++  java
  • 如何使用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;
                }
            }
  • 相关阅读:
    LeetCode 258 Add Digits
    LeetCode 231 Power of Two
    LeetCode 28 Implement strStr()
    LeetCode 26 Remove Duplicates from Sorted Array
    LeetCode 21 Merge Two Sorted Lists
    LeetCode 20 Valid Parentheses
    图形处理函数库 ImageTTFBBox
    php一些函数
    func_get_arg(),func_get_args()和func_num_args()的用法
    人生不是故事,人生是世故,摸爬滚打才不会辜负功名尘土
  • 原文地址:https://www.cnblogs.com/bayonetxxx/p/2069618.html
Copyright © 2011-2022 走看看