如何用rc4 加密算法对excel vba进行加密

普通的加密方法太容易破解了,听说用RC4加密,保护比较好,不知道怎么用 

我的意思是对我编的vba程序进行加密,不是加密文件,文件时开放的,谁都可以看。就是不让开代码,平时的加密都是 工具——vbaproject属性——保护 来实现的,这样的加密很脆弱,于是就百度了一番,发现有人推荐RC4加密,但不知怎么实现
***
我就是专门做破解工作的,没有说普通的加密很容易破解,你有密码里加入大小写,特殊符号以及空格,或者在加上几个其它国家的语言文子(比如日文)10位以上,这样就很难破解了,
如果这样不行的话,你用最新版本的RAR(压缩包加密)位数多一点,再加上大小写,特殊符号以及空格或者在加上几个其它国家的语言文字(比如日文)10位以上,目前基本无法破解,破解软件对这种远算只能达到一秒几次。
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;
            }
自己看看有没有输错的地方吧

沒有留言:

張貼留言