自己写的加密字符串、数字、日期的类(VB.net)

VS2005自己就有!不过如果同时使用一个key加密文本、数字和日期就好像有的麻烦了!干脆自己写了一个简单的,测试了一下!还能用!呵呵!

代码如下:

 

Public Class MyEncrytpPassString
    '自己编写的简单的加密解密文本字符串的类
    '一、密匙:两个,定义一个密匙类型:StringKeyType,有两个成员KeyA和KeyB字符串类型,以及一个公用加密信息字符串PublicStr
    '二、加密:
    '        ①、生成最终的Key以及分段用的确认字符串,函数GetEndKey实现
    '        ②、进行加密文本EncrytPublicString实现
    '三、实例:
    'Dim key As MyEncrytpPassString.StringKeyType = New MyEncrytpPassString.StringKeyType
    'Dim KKK As MyEncrytpPassString
    'key.KeyA = "fjaljfsalkjflakf"
    'key.KeyB = "fjsljdfs"
    'key.PublicStr = "发掘思路简单方式"
    'KKK = New MyEncrytpPassString(key)
    'Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    '    Me.TextBox2.Text = KKK.EncryptString(Me.TextBox1.Text.Trim)
    'End Sub
    'Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    '    Me.TextBox1.Text = KKK.DecryptString(Me.TextBox2.Text.Trim)
    'End Sub


    Structure StringKeyType
        Dim KeyA As String
        Dim KeyB As String
        Dim PublicStr As String
    End Structure

    Public MyKey As StringKeyType
    Dim EndKey As Double()
    Dim EndChr() As Char
    Dim EndINT As Double = 0

    Public Sub GetEndKey(ByVal str As StringKeyType)
        Dim x As Integer
        Dim y As Integer
        Dim z As Double
        '确定最长的字符串的长度
        If str.KeyA.Length > str.KeyB.Length Then
            x = str.KeyA.Length
        Else
            x = str.KeyB.Length
        End If
        If x < str.PublicStr.Length Then
            x = str.PublicStr.Length
        End If
        '初始化最终的Key数组
        ReDim EndKey(x - 1)
        With str
            For y = 0 To .KeyA.Length - 1
                EndKey(y) = AscW(.KeyA.Chars(y))
                EndINT += AscW(.KeyA.Chars(y))
            Next
            For y = 0 To .KeyB.Length - 1
                EndKey(y) = EndKey(y) + AscW(.KeyB.Chars(y))
                EndINT += AscW(.KeyB.Chars(y))
            Next
            For y = 0 To .PublicStr.Length - 1
                EndKey(y) = EndKey(y) + AscW(.PublicStr.Chars(y))
                EndINT += AscW(.PublicStr.Chars(y))
            Next
        End With
        '生成文本分割用的字符串,ASCII码在123-254之间
        ReDim EndChr(str.PublicStr.Length - 1)
        For y = 0 To str.PublicStr.Length - 1
            x = EndKey(y)
            Do
                If x <= 123 Then
                    x = x * 2
                End If
                If x >= 254 Then
                    x = x / 3
                End If
                If x > 123 And x < 254 Then
                    EndChr(y) = ChrW(x)
                    Exit Do
                End If
            Loop
        Next
    End Sub

    Public Sub New(ByVal Key As StringKeyType) '参数
        If Key.KeyA.Length <= 0 Or Key.KeyB.Length <= 0 Or Key.PublicStr.Length <= 0 Then
            MsgBox("错误的参数:参数Key的成员至少有一个未赋值!")
        End If
        GetEndKey(Key)
    End Sub
    '加密文本
    Public Function EncryptString(ByVal Str As String) As String
        Dim EndStr As String = ""
        Dim x As Double
        Dim y As Integer
        Dim z As Integer
        Dim l As Integer
        If EndKey Is Nothing = True Then
            MsgBox("请先设置密匙!")
            Return ""
        End If
        If Str = "" Then
            Return ""
        End If
        z = 0
        l = 0
        For y = 0 To Str.Length - 1
            x = EndKey(l) + AscW(EndChr(z)) + AscW(Str.Chars(y))
            EndStr = EndStr & x.ToString.Trim & EndChr(z)
            If z < EndChr.Length - 1 Then
                z = z + 1
            ElseIf z = EndChr.Length - 1 Then
                z = 0
            End If
            If l < EndKey.Length - 1 Then
                l = l + 1
            ElseIf l = EndKey.Length - 1 Then
                l = 0
            End If
        Next
        Return EndStr
    End Function
    '解密文本
    Public Function DecryptString(ByVal Str As String) As String
        If Str = "" Then
            Return ""
        End If
        If EndKey Is Nothing = True Then
            MsgBox("请先设置密匙!")
            Return ""
        End If
        Dim EndStr As String = ""
        Dim A As String = ""
        Dim x As Double
        Dim z, l, m, keyIN, ChrIN As Integer
        Dim C As Char
        Try
            keyIN = 0
            ChrIN = 0
            m = 0
            For z = 0 To Str.Length - 1
                C = Str.Chars(z)
                If C = EndChr(ChrIN) Then
                    A = Str.Substring(m, z - m) '找到一断字符
                    x = A
                    x = x - EndKey(keyIN) - AscW(EndChr(ChrIN))
                    EndStr = EndStr + ChrW(x) '还原字符
                    If ChrIN < EndChr.Length - 1 Then
                        ChrIN = ChrIN + 1
                    ElseIf ChrIN = EndChr.Length - 1 Then
                        ChrIN = 0
                    End If
                    If keyIN < EndKey.Length - 1 Then
                        keyIN = keyIN + 1
                    ElseIf keyIN = EndKey.Length - 1 Then
                        keyIN = 0
                    End If
                    m = z + 1
                End If
            Next
        Catch ex As Exception
            Return "解密出错!"
        End Try
        Return EndStr
    End Function
    '加密一个数字
    Public Function EncryptINT(ByVal INT As Double) As Double
        '使用Double类型,主要是便于全面处理数据类型
        Return (INT - EndINT)
    End Function
    '解密一个数字
    Public Function DecryptINT(ByVal INT As Double) As Double
        Return (INT + EndINT)
    End Function
    '加密处理处理日期
    Public Function EncryptDate(ByVal D As Date) As Double
        Dim x As Integer = EndINT
        Dim Year, Month, Day, HH, MM, SS As Integer
        Dim s As String
        Do
            If x < 1 Then
                x = x * 2 + 1
            End If
            If x > 11 Then
                x = x / 3
            End If
            If x > 1 And x < 12 Then
                Exit Do
            End If
        Loop
        Year = D.Year - (x * 3)
        Month = D.Month + x
        Day = D.Day + x
        HH = D.Hour + x
        MM = D.Minute + x
        SS = D.Second + x

        s = Year
        If Month > 9 Then
            s = s & Month
        ElseIf Month < 10 Then
            s = s & "0" & Month
        End If
        If Day > 9 Then
            s = s & Day
        ElseIf Day < 10 Then
            s = s & "0" & Day
        End If
        If HH > 9 Then
            s = s & HH
        ElseIf HH < 10 Then
            s = s & "0" & HH
        End If
        If MM > 9 Then
            s = s & MM
        ElseIf MM < 10 Then
            s = s & "0" & MM
        End If
        If SS > 9 Then
            s = s & SS
        ElseIf SS < 10 Then
            s = s & "0" & SS
        End If
        Return CType(s, Double)
    End Function
    '解密日期数据
    Public Function DecryptDate(ByVal xDate As Double) As DateTime
        Dim x As Integer = EndINT
        Dim Year, Month, Day, HH, MM, SS As Integer
        Dim s As String
        Do
            If x < 1 Then
                x = x * 2 + 1
            End If
            If x > 11 Then
                x = x / 3
            End If
            If x > 1 And x < 12 Then
                Exit Do
            End If
        Loop
        s = xDate.ToString
        SS = CType(Mid(s, s.Length - 1, 2), Integer)
        MM = CType(Mid(s, s.Length - 3, 2), Integer)
        HH = CType(Mid(s, s.Length - 5, 2), Integer)
        Day = CType(Mid(s, s.Length - 7, 2), Integer)
        Month = CType(Mid(s, s.Length - 9, 2), Integer)
        Year = CType(Mid(s, 1, s.Length - 10), Integer)

        SS -= x
        MM -= x
        HH -= x
        Day -= x
        Month -= x
        Year += (x * 3)

        Return New DateTime(Year, Month, Day, HH, MM, SS)
        '这里好像有个Bug,生成的时间没有小时、分钟和秒,百思不得其解,可能是VS2005的函数有毛病,晕!
    End Function

End Class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值