卓越2008

用一颗谦虚的心面对大家,用一颗坚定的心面对困难,用一颗执著的心面对理想,用一颗虔诚的心面对技术。

导航

传奇3封包加解密C#版

Posted on 2007-04-26 13:20  Casm  阅读(1469)  评论(0)    收藏  举报

加密算法1:
        public string Base64Encode(string source)
        {
            byte[] a1,a2,a3;
            BitArray b1,b2,b3;
            a1 = Encoding.Default.GetBytes(source);
            if (a1.Length % 3 == 0) a2 = new byte[a1.Length*4/3];
            else a2 = new byte[a1.Length*4/3+1];
            a3 = new byte[1];
            b1 = new BitArray(a1);
            b2 = new BitArray(6);
            b3 = new BitArray(b1.Length);
            for (int i = 0, k = b1.Length / 8; i < k; i++) 
            {
                for(int j=7;j>=0;j--)b3[i*8+j]=b1[i*8+7-j];
            }
            for (int i = 0,k=b1.Length/6; i < k; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    b2[5-j] = b3[i * 6 + j];
                }
                b2.CopyTo(a3,0);
                a2[i] = a3[0];
            }
            if (a1.Length % 3 == 1) a2[a2.Length - 1] = (byte)((byte)(a1[a1.Length - 1] << 6) >> 2);
            else if (a1.Length % 3 == 2) a2[a2.Length - 1] = (byte)((byte)(a1[a1.Length - 1] << 4 )>> 2);
            for (int i=0; i < a2.Length; i++) a2[i] = (byte)(a2[i] + 0x3c);
            return Encoding.Default.GetString(a2);
        }

加密算法2:

        public string Base64Encode(string source)
        {
            byte[] a1, a2;
            a1 = Encoding.Default.GetBytes(source);
            if (a1.Length % 3 == 0) a2 = new byte[a1.Length * 4 / 3];
            else a2 = new byte[a1.Length * 4 / 3 + 1];
            for (int i = 0, k = a1.Length / 3; i < k; i++) 
            {
                a2[i*4] = (byte)(a1[i*3] >> 2);
                a2[i*4 + 1] = (byte)((byte)((byte)(a1[i*3] << 6) >> 2) + (byte)(a1[i*3 + 1] >> 4));
                a2[i*4 + 2] = (byte)((byte)((byte)(a1[i*3 + 1] << 4) >> 2) + (byte)(a1[i*3 + 2] >> 6));
                a2[i*4 + 3] = (byte)((byte)(a1[i*3 + 2] << 2) >> 2);
            }
            if (a1.Length % 3 == 1)
            {
                a2[a2.Length - 2] = (byte)(a1[a1.Length - 1] >> 2);
                a2[a2.Length - 1] = (byte)((byte)(a1[a1.Length - 1] << 6) >> 2);
            }
            else
                if (a1.Length % 3 == 2)
                {
                    a2[a2.Length - 3] = (byte)(a1[a1.Length - 2] >> 2);
                    a2[a2.Length - 2] = (byte)((byte)((byte)(a1[a1.Length - 2] << 6) >> 2) + (byte)(a1[a1.Length - 1] >> 4));
                    a2[a2.Length - 1] = (byte)((byte)(a1[a1.Length - 1] << 4) >> 2);
                }
            for (int i = 0; i < a2.Length; i++) a2[i] = (byte)(a2[i] + 0x3c);
            return Encoding.Default.GetString(a2);
        }


解密算法:

        public string Base64Decode(string source) 
        {
            byte[] a1, a2;
            a1 = Encoding.Default.GetBytes(source);
            a2 = new byte[a1.Length * 3 / 4];
            for (int i = 0; i < a1.Length; i++) a1[i] = (byte)(a1[i] - 0x3c);
            for (int i = 0, k = a1.Length / 4; i < k; i++)
            {
                a2[i * 3] = (byte)((byte)(a1[i * 4] << 2) + (byte)(a1[i * 4 + 1] >> 4));
                a2[i * 3 + 1] = (byte)((byte)(a1[i * 4 + 1] << 4) + (byte)(a1[i * 4 + 2] >> 2));
                a2[i * 3 + 2] = (byte)((byte)(a1[i * 4 + 2] << 6) + a1[i * 4 + 3]);
            }
            if (a2.Length % 3 == 2)
            {
                a2[a2.Length - 2] = (byte)((byte)(a1[a1.Length - 3] << 2) + (byte)(a1[a1.Length - 2] >> 4));
                a2[a2.Length - 1] = (byte)((byte)(a1[a1.Length - 2] << 4) + (byte)(a1[a1.Length - 1] >> 2));
            }
            else
            if (a2.Length % 3 == 1) a2[a2.Length - 1] = (byte)((byte)(a1[a1.Length - 2] << 2) + (byte)(a1[a1.Length - 1] >> 4));
            return Encoding.Default.GetString(a2);
        }