1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<pre class="inline:true class:language-dotnet decode:1 " > ''' <summary> ''' BASE64编码 ''' </summary> ''' <param name="code">待加密字符串</param> ''' <returns>加密后字符串</returns> ''' <remarks></remarks> Public Shared Function EncodeBase64(ByVal code As String) As String Dim encode As String = "" Dim bytes As Byte() = Encoding.Default.GetBytes(code) Try encode = Convert.ToBase64String(bytes) Catch encode = code End Try Return encode End Function ''' <summary> ''' BASE64解码 ''' </summary> ''' <param name="code">加密字符串</param> ''' <returns>解密后字符串</returns> ''' <remarks></remarks> Public Function DecodeBase64(ByVal code As String) As String Dim decode As String = "" Dim bytes As Byte() = Convert.FromBase64String(code) Try decode = Encoding.Default.GetString(bytes) Catch decode = code End Try Return decode End Function |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<pre class="inline:true class:language-dotnet decode:1 " > ''' <summary> ''' AES加密 ''' </summary> ''' <param name="str">需加密字符串</param> ''' <param name="key">KEY</param> ''' <returns></returns> ''' <remarks></remarks> Public Shared Function AesEncrypt(ByVal str As String, ByVal key As String) As String Dim ReStr As String = "" If String.IsNullOrEmpty(str) Then Return Nothing Try Dim toEncryptArray As Byte() = Encoding.UTF8.GetBytes(str) Dim rm As System.Security.Cryptography.RijndaelManaged = New System.Security.Cryptography.RijndaelManaged With { .Key = Encoding.UTF8.GetBytes(key), .Mode = System.Security.Cryptography.CipherMode.ECB, .Padding = System.Security.Cryptography.PaddingMode.PKCS7 } Dim cTransform As System.Security.Cryptography.ICryptoTransform = rm.CreateEncryptor() Dim resultArray As Byte() = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length) ReStr = Convert.ToBase64String(resultArray, 0, resultArray.Length) Catch ex As Exception End Try AesEncrypt = ReStr End Function ''' <summary> ''' AES解密 ''' </summary> ''' <param name="str">需解密字符串</param> ''' <param name="key">KEY</param> ''' <returns></returns> ''' <remarks></remarks> Shared Function AesDecrypt(ByVal str As String, ByVal key As String) As String Dim ReStr As String = "" If String.IsNullOrEmpty(str) Then Return Nothing Try Dim toEncryptArray As Byte() = Convert.FromBase64String(str) Dim rm As System.Security.Cryptography.RijndaelManaged = New System.Security.Cryptography.RijndaelManaged With { .Key = Encoding.UTF8.GetBytes(key), .Mode = System.Security.Cryptography.CipherMode.ECB, .Padding = System.Security.Cryptography.PaddingMode.PKCS7 } Dim cTransform As System.Security.Cryptography.ICryptoTransform = rm.CreateDecryptor() Try Dim resultArray As Byte() = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length) ReStr = Encoding.UTF8.GetString(resultArray) Catch ex As Exception '加密字符串被非法改动 End Try Catch ex As Exception '解密失败 '日志记录 End Try AesDecrypt = ReStr End Function |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<pre class="inline:true class:language-dotnet decode:1 " > '''URL编码函数 ''' <summary> ''' URL编码 ''' </summary> ''' <param name="strURL">需要编码字符串</param> ''' <returns></returns> ''' <remarks></remarks> Shared Function URLEncode(ByRef strURL As String) As String Dim tmpstr As String = "" Dim I As Long Dim tempStr As String For I = 1 To Len(strURL) If Asc(Mid(strURL, I, 1)) < 0 Then tempStr = "%" & Right(CStr(Hex(Asc(Mid(strURL, I, 1)))), 2) tempStr = "%" & Left(CStr(Hex(Asc(Mid(strURL, I, 1)))), Len(CStr(Hex(Asc(Mid(strURL, I, 1))))) - 2) & tempStr tmpstr = tmpstr & tempStr ElseIf (Asc(Mid(strURL, I, 1)) >= 65 And Asc(Mid(strURL, I, 1)) <= 90) Or (Asc(Mid(strURL, I, 1)) >= 97 And Asc(Mid(strURL, I, 1)) <= 122) Then tmpstr = tmpstr & Mid(strURL, I, 1) Else tmpstr = tmpstr & "%" & Hex(Asc(Mid(strURL, I, 1))) End If Next Return tmpstr End Function |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<pre class="inline:true class:language-dotnet decode:1 " > '''MD5 ''' <summary> ''' MD5加密 ''' </summary> ''' <param name="strSource">需要加密的字符串</param> ''' <param name="Code">加密位数 16 或 32 </param> ''' <returns>加密后字符串(小写)</returns> ''' <remarks></remarks> Public Shared Function MD5(ByVal strSource As String, ByVal Code As Int16) As String Dim dataToHash As Byte() = (New System.Text.ASCIIEncoding).GetBytes(strSource) Dim hashvalue As Byte() = CType(System.Security.Cryptography.CryptoConfig.CreateFromName("MD5"), System.Security.Cryptography.HashAlgorithm).ComputeHash(dataToHash) Dim ATR As String = "" Dim i As Integer Select Case Code Case 16 '选择16位字符的加密结果 For i = 4 To 11 ATR &= Hex(hashvalue(i)).PadLeft(2, "0").ToLower Next Case 32 '选择32位字符的加密结果 For i = 0 To 15 ATR &= Hex(hashvalue(i)).PadLeft(2, "0").ToLower Next Case Else 'Code错误时,返回全部字符串,即32位字符 For i = 0 To 15 ATR &= Hex(hashvalue(i)).PadLeft(2, "0").ToLower Next End Select Return ATR End Function |
浏览量: 65
?