pb加密转换成C#

2021-07-17 12:19

阅读:553

标签:char   integer   loop   封装   ++   second   new   col   ota   

本来想把PB 的函数封装成COM组件调用,结果怎么都搞不定,只能讲代码搞出来换种语言实现了.

string s_ret
integer i_first,i_second,i_third,i_fourth
integer by4,i,reallen
long i_total
string is_key
string is_base64
string EncMap[0 to 63]
int DecMap[0 to 127]
integer by3
long i_len

is_base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
is_base64 = is_base64 + "abcdefghijklmnopqrstuvwxyz"
is_base64 = is_base64 + "0123456789"
is_base64 = is_base64 + "+/"
for i = 1 to 63
    EncMap[i]=mid(is_base64,i+1,1)
next
for i = 1 to 63
    DecMap[asc(EncMap[i])] = i
next
choose case a_type
    case 0//加密
        s_ret = ""
        if len(a_text)= 0 then 
            return s_ret
        end if
        by3=len(a_text) - mod(len(a_text),3)
        i=1
        do while i by3
            i_first = asc(mid(a_text,i + 0,1))
            i_second = asc(mid(a_text, i + 1,1))
            i_third = asc(mid(a_text,i + 2,1))
            s_ret = s_ret + encmap[i_first / 4]
            s_ret = s_ret + encmap[mod(i_first , 4) * 16 + i_second / 16 ]
            s_ret = s_ret + encmap[mod(i_second , 16) * 4 + i_third / 64 ]
            s_ret = s_ret + encmap[mod(i_third , 64)] 
            i = i + 3
        loop
        if i  len(a_text) then
            i_first = asc(mid(a_text, i + 0, 1))
            if mod(len(a_text), 3)=2 then
                i_second =asc(mid(a_text,i+1,1))
                s_ret = s_ret + encmap[i_first / 4]
                s_ret = s_ret + encmap[mod(i_first , 4) * 16 + i_second / 16 ]
                s_ret = s_ret + encmap[mod(i_second , 16) * 4 ]
                s_ret = s_ret + "="        
            else
                s_ret = s_ret + encmap[i_first / 4]
                s_ret = s_ret + encmap[mod(i_first,4) * 16]
                s_ret = s_ret + "=="
            end if
        end if
        i_len = len(s_ret)
        s_ret = left(string(i_len+10),2) + s_ret
        
    case 1//解密
        if len(a_text)= 0 then 
            s_ret = ""
            return s_ret
        end if
        a_text=right(a_text,len(a_text)-2)
        reallen = len(a_text)
        do while mid(a_text,reallen,1) = "=" 
            reallen = reallen - 1
        loop
        by4=reallen - mod(reallen,4)
        i=1
        do while i by4
            i_first = decmap[asc(mid(a_text,i + 0,1))]
            i_second = decmap[asc(mid(a_text, i + 1,1))]
            i_third = decmap[asc(mid(a_text,i + 2,1))]
            i_fourth = decmap[asc(mid(a_text,i + 3,1))]
            s_ret = s_ret + char(i_first * 4 + i_second / 16)
            s_ret = s_ret + char(mod(i_second,16) * 16 + i_third / 4 )
            if mod(i_third,4) * 64 + i_fourth  0 then
                s_ret = s_ret + char(mod(i_third,4) * 64 + i_fourth)
            end if
            i = i + 4
        loop
        if i//只可能是xx==或xxx=两种形式有效
            if mod(reallen,4) = 2 then    //xx==形式
                i_first = decmap[asc(mid(a_text,i + 0,1))]
                i_second = decmap[asc(mid(a_text, i + 1,1))]
                s_ret = s_ret + char(i_first * 4 + i_second / 16)
                s_ret = s_ret + char(mod(i_second,16) * 16)
            else
                i_first = decmap[asc(mid(a_text,i + 0,1))]
                i_second = decmap[asc(mid(a_text, i + 1,1))]
                i_third = decmap[asc(mid(a_text,i + 2,1))]
                s_ret = s_ret + char(i_first * 4 + i_second / 16)
                s_ret = s_ret + char(mod(i_second,16) * 16 + i_third / 4 )
            end if
        end if

end choose
return s_ret 

 

 

        public static string right(string s1, int str)
        {
            return s1.Substring(s1.Trim().Length - str, str);
        }

        public static string left(string s1, int str)
        {
            return s1.Substring( 0, str);
        }

        public static char mid(string s1, int str, int len)
        {
            return s1.Substring(str, len).ToArray()[0];
        }

        public static int asc(char c1)
         {
             return(int)c1;
         }

        public static int mod(int x,int y)
        {
            return x % y;
        }

        public static int len(string s1)
        {
            return s1.Length;
        }

        public static string encrypt(string a_text, string type = "1")
        {
            string s_ret;
            int i_first, i_second, i_third, i_fourth;
            int by4, i, reallen;
            long i_total;
            string is_key;
            string is_base64;
            char[] encmap = new char[64];
            int[] decmap = new int[128];
            int by3 = 0;
            long i_len;

            is_base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            is_base64 = is_base64 + "abcdefghijklmnopqrstuvwxyz";
            is_base64 = is_base64 + "0123456789";
            is_base64 = is_base64 + "+/";

            char[] tk = is_base64.ToCharArray();

            for (i = 1; i 63; i++)
            {
                encmap[i] = tk[i];
            }

            for (i = 1; i 63; i++)
            {
                int tp = (int)asc(encmap[i]);
                decmap[tp] = i;
            }


            s_ret = "";
            if (a_text.Trim() == "")
            {
                return s_ret;
            }

            if (type == "1")
            {
                by3 = a_text.Length - (a_text.Length % 3);

                i = 0;
                while (i  by3)
                {
                    i_first = asc(mid(a_text, i + 0, 1));

                    i_second = asc(mid(a_text, i + 1, 1));
                    i_third = asc(mid(a_text, i + 2, 1));
                    s_ret = s_ret + encmap[i_first / 4];
                    s_ret = s_ret + encmap[mod(i_first, 4) * 16 + i_second / 16];
                    s_ret = s_ret + encmap[mod(i_second, 16) * 4 + i_third / 64];
                    s_ret = s_ret + encmap[mod(i_third, 64)];
                    i = i + 3;
                }
                if (i  len(a_text))
                {
                    i_first = asc(mid(a_text, i + 0, 1));
                    if ((a_text.Length % 3) == 2)
                    {
                        i_second = asc(mid(a_text, i + 1, 1));
                        s_ret = s_ret + encmap[i_first / 4];
                        s_ret = s_ret + encmap[mod(i_first, 4) * 16 + i_second / 16];
                        s_ret = s_ret + encmap[mod(i_second, 16) * 4];
                        s_ret = s_ret + "=";
                    }
                    else
                    {
                        s_ret = s_ret + encmap[i_first / 4];
                        s_ret = s_ret + encmap[mod(i_first, 4) * 16];
                        s_ret = s_ret + "==";
                    }
                }
                i_len = len(s_ret);
                s_ret = left((i_len + 10).ToString(), 2) + s_ret;
            }
            else
            {

                a_text = right(a_text, len(a_text) - 2);
                reallen = len(a_text);
                while (mid(a_text, reallen - 1, 1) == =)
                {
                    reallen = reallen - 1;
                }

                by4 = reallen - mod(reallen, 4);

                i = 0;


                while (i  by4)
                {
                    i_first = decmap[asc(mid(a_text, i + 0, 1))];
                    i_second = decmap[asc(mid(a_text, i + 1, 1))];
                    i_third = decmap[asc(mid(a_text, i + 2, 1))];
                    i_fourth = decmap[asc(mid(a_text, i + 3, 1))];
                    s_ret = s_ret + (char)(i_first * 4 + i_second / 16);
                    s_ret = s_ret + (char)(mod(i_second, 16) * 16 + i_third / 4);
                    if (mod(i_third, 4) * 64 + i_fourth != 0)
                    {
                        s_ret = s_ret + (char)(mod(i_third, 4) * 64 + i_fourth);
                    }
                    i = i + 4;
                }
                if (i  reallen)
                {
                    //只可能是xx==或xxx=两种形式有效
                    if (mod(reallen, 4) == 2)    //xx==形式
                    {
                        i_first = decmap[asc(mid(a_text, i + 0, 1))];
                        i_second = decmap[asc(mid(a_text, i + 1, 1))];
                        s_ret = s_ret + (char)(i_first * 4 + i_second / 16);
                        s_ret = s_ret + (char)(mod(i_second, 16) * 16);
                    }
                    else
                    {
                        i_first = decmap[asc(mid(a_text, i + 0, 1))];
                        i_second = decmap[asc(mid(a_text, i + 1, 1))];
                        i_third = decmap[asc(mid(a_text, i + 2, 1))];
                        s_ret = s_ret + (char)(i_first * 4 + i_second / 16);
                        s_ret = s_ret + (char)(mod(i_second, 16) * 16 + i_third / 4);
                    }
                }
            }
            return s_ret;
        }

 

pb加密转换成C#

标签:char   integer   loop   封装   ++   second   new   col   ota   

原文地址:https://www.cnblogs.com/CoreXin/p/8143223.html


评论


亲,登录后才可以留言!