首页 > 编程 > .NET > 正文

字符串加解密的类(VB.NET Source Code)

2024-07-10 13:02:29
字体:
来源:转载
供稿:网友
'ez_crypt.vb(字符串加密和解密类)

'参考namespace
imports system
imports microsoft.visualbasic
imports system.collections
imports system.configuration

namespace security
    public class crypt
    '加密字符串
        public function encrypt(byval plainstr as string, byval key as string) as string
            dim strchar, keychar, newstr as string
            dim pos as integer
            dim i, intlen as integer
            dim side1, side2 as string
            pos = 1

            for i = 1 to len(plainstr)
                strchar = mid(plainstr, i, 1)
                keychar = mid(key, pos, 1)
                newstr = newstr & chr(asc(strchar) xor asc(keychar))
                if pos = len(key) then pos = 0
                pos = pos + 1
            next

            if len(newstr) mod 2 = 0 then
                side1 = strreverse(left(newstr, (len(newstr) / 2)))
                side2 = strreverse(right(newstr, (len(newstr) / 2)))
                newstr = side1 & side2
            end if

            encrypt = newstr
        end function

    '解密字符串
        public function decrypt(byval plainstr as string, byval key as string) as string
            dim strchar, keychar, newstr as string
            dim pos as integer
            dim i as integer
            dim side1 as string
            dim side2 as string
            pos = 1

            if len(plainstr) mod 2 = 0 then
                side1 = strreverse(left(plainstr, (len(plainstr) / 2)))
                side2 = strreverse(right(plainstr, (len(plainstr) / 2)))
                plainstr = side1 & side2
            end if

            for i = 1 to len(plainstr)
                strchar = mid(plainstr, i, 1)
                keychar = mid(key, pos, 1)
                newstr = newstr & chr(asc(strchar) xor asc(keychar))
                if pos = len(key) then pos = 0
                pos = pos + 1
            next

            decrypt = newstr
        end function
    end class
end namespace

收集最实用的网页特效代码!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表