【VB.Net】使用SHA256进行简单的XOR文本加密与解密

以下代码已经编译成库,可直接引用调用,也可自行编译。语法简单,应该不会有太多的问题。也可自行对最终的编码进行旋转、对调等。


从Runplus.Studio下载




Imports System.Security.Cryptography
Namespace Runplus.Utils.Encoder
    '************************************************
    '* https://www.runplus.studio/Utils/Encoder
    '* Likun@Runplus.cn
    '************************************************
    Module XOREncoder
        Sub Main()
            '示例代码
            Dim Ret As String = "" '定义返回参数,以传址方式传入

            If Encoder("Hello World!", "12345abcde", Ret) Then
                Console.WriteLine(Ret)
            Else
                Console.WriteLine(Ret)
            End If
            If Decoder(Ret, "12345abcde", Ret) Then
                Console.WriteLine(Ret)
            Else
                Console.WriteLine(Ret)
            End If

            Console.ReadLine()

        End Sub
        Public Function Encoder(Source As String, Key As String, ByRef Ret As String) As Boolean
            Try
                If Source.Length = 0 Or Key.Length = 0 Then
                    Ret = "明文或私钥为空,不能进行加密。"
                    Return False
                Else
                    Dim ArrKey() As Byte = MakeKey(Key)
                    Dim ArrSource() As Byte = Text.Encoding.UTF8.GetBytes(Source)
                    For i As Integer = 0 To ArrSource.Length - 1 Step 32
                        For j As Integer = 0 To 31 Step 1
                            If i + j = ArrSource.Length Then
                                Exit For
                            Else
                                ArrSource(i + j) = ArrSource(i + j) Xor ArrKey(j)
                            End If
                        Next
                    Next
                    Ret = ConvertToBase64(ArrSource)
                    Return True
                End If
            Catch ex As Exception
                Ret = ex.Message
                Return False
            End Try
        End Function

        Public Function Decoder(Source As String, key As String, ByRef Ret As String) As Boolean
            Try
                If Source.Length = 0 Or key.Length = 0 Then
                    Ret = "明文或私钥为空,不能进行加密。"
                    Return False
                Else
                    Dim ArrKey() As Byte = MakeKey(key)
                    Dim ArrSource() As Byte = ConvertFromBase64(Source)
                    For i As Integer = 0 To ArrSource.Length - 1 Step 32
                        For j As Integer = 0 To 31 Step 1
                            If i + j = ArrSource.Length Then
                                Exit For
                            Else
                                ArrSource(i + j) = ArrSource(i + j) Xor ArrKey(j)
                            End If
                        Next
                    Next
                    Ret = Text.Encoding.UTF8.GetString(ArrSource)
                    Return True
                End If
            Catch ex As Exception
                Ret = ex.Message
                Return False
            End Try
        End Function
        Private Function MakeKey(ByRef Key As String) As Byte()
            If Key.Length = 0 Then
                Return Nothing
            Else
                Return SHA256(Text.Encoding.UTF8.GetBytes(Key))
            End If
        End Function

        Private Function SHA256(Source As Byte()) As Byte()
            If Source.Length = 0 Then
                Return Nothing
            Else
                Dim shaM As New SHA256Managed
                Return shaM.ComputeHash(Source)
            End If
        End Function
        Private Function ConvertToBase64(Source As Byte()) As String
            If Source.Length = 0 Then
                Return Nothing
            Else
                Return Convert.ToBase64String(Source).Replace("+", "*").Replace("/", "!")
            End If
        End Function
        Private Function ConvertFromBase64(Source As String) As Byte()
            If Source.Length = 0 Then
                Return Nothing
            Else
                Return Convert.FromBase64String(Source.Replace("*", "+").Replace("!", "/"))
            End If
        End Function
    End Module
End Namespace



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值