Excel VBA程序开发

perfect131 Lv.5

关注
本帖最后由 perfect131 于 2023-3-21 13:47 编辑

        直接用 System.Security.Cryptography.MD5CryptoServiceProvider,两种方式encode


  1. Function StringToMD5Hex(ByVal s As String) As String
  2.     Dim enc As Object
  3.     Dim bytes() As Byte
  4.     Dim pos As Long
  5.     Dim outstr As String
  6.     Set enc = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
  7.     bytes = encode(s)
  8.     bytes = enc.ComputeHash_2(encode(s))
  9.     outstr = ""
  10.     For pos = 1 To UBound(bytes) + 1
  11.        outstr = outstr & Right("0" & Hex(AscB(MidB(bytes, pos, 1))), 2)
  12.     Next pos
  13.     outstr = LCase(outstr)
  14.     StringToMD5Hex = outstr
  15.     Set enc = Nothing
  16. End Function
  17. Function StringToMD5Hex1(ByVal temp_sign As String) As String
  18.     Dim md As Object
  19.     Set md = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
  20.     Dim bytes() As Byte
  21.     bytes = md.ComputeHash_2(CreateObject("System.Text.UTF8Encoding").GetBytes_4(temp_sign))
  22.     Dim sign As String
  23.     sign = ""
  24.     For i = 1 To UBound(bytes) + 1
  25.         sign = sign & Right("0" & Hex(AscB(MidB(bytes, i, 1))), 2)
  26.     Next
  27.     md.Clear
  28.     Set md = Nothing
  29.     StringToMD5Hex1 = LCase(sign)
  30. End Function
  31. '''编码转换("我是学员","UTF-8","gbk")
  32. '''decode(encode("我是学员"),"gbk")
  33. '''decode(encode("我是学员","gbk"),"gbk")
  34. Function 编码转换(str, CharSet1, CharSet2)
  35.     编码转换 = ""
  36.     On Error GoTo err1
  37.     Dim ADODB_Stream
  38.     Set ADODB_Stream = CreateObject("ADODB.Stream")
  39.     Dim bytes()                                   As Byte
  40.     Dim gbk_txt                                   As String
  41.     With ADODB_Stream
  42.         .Charset = CharSet1
  43.         .Open
  44.         .WriteText str, 0
  45.         .Position = 0
  46.         .Type = 1
  47.         If CharSet1 = CharSet2 Then
  48.             .Position = 0
  49.         Else
  50.             .Position = 3 ''无 bom
  51.         End If
  52.         bytes = .Read()
  53.         .Close
  54.         .Open
  55.         .Type = 1
  56.         .Write bytes
  57.         .Position = 0
  58.         .Type = 2
  59.         .Charset = CharSet2
  60.         编码转换 = .ReadText()
  61.         .Close
  62.      End With
  63. err1:
  64.     Set ADODB_Stream = Nothing
  65. End Function
  66. Function encode(ss, Optional ByVal CharSet1 As String = "UTF-8") As Byte()
  67.     On Error GoTo err1
  68.     Dim ADODB_Stream
  69.     Set ADODB_Stream = CreateObject("ADODB.Stream")
  70.     With ADODB_Stream
  71.         .Charset = CharSet1
  72.         .Open
  73.         .WriteText ss, 0
  74.         .Position = 0
  75.         .Type = 1
  76.         If CharSet1 <> "UTF-8" Then
  77.             .Position = 0
  78.         Else
  79.             .Position = 3 ''无 bom
  80.         End If
  81.         encode = .Read()
  82.         .Close
  83.      End With
  84. err1:
  85.     Set ADODB_Stream = Nothing
  86. End Function
  87. Function decode(bytes, Optional ByVal CharSet2 As String = "UTF-8") As String
  88.     decode = ""
  89.     On Error GoTo err1
  90.     Dim ADODB_Stream
  91.     Set ADODB_Stream = CreateObject("ADODB.Stream")
  92.     With ADODB_Stream
  93.         .Open
  94.         .Type = 1
  95.         .Write bytes
  96.         .Position = 0
  97.         .Type = 2
  98.         .Charset = CharSet2
  99.         decode = .ReadText()
  100.         .Close
  101.      End With
  102. err1:
  103.     Set ADODB_Stream = Nothing
  104. End Function

1.png
3 人给楼主打赏
1010阅读
3回复 倒序

liuxin12 Lv.2 2楼

VBA还能用MD5呀!大佬主要用于什么用途

tdpzby Lv.2 3楼

引用: liuxin12 发表于 2024-5-16 01:03
VBA还能用MD5呀!大佬主要用于什么用途

好多网站的的请求都要用到

perfect131 楼主 4楼

引用: liuxin12 发表于 2024-5-16 01:03
VBA还能用MD5呀!大佬主要用于什么用途

MD5 一般用于
验证         比如某些网页 先用MD5加密一些数据 然后跟客户请求的MD5做比较从而判断 是不是正常请求
唯一值      比如 wps的嵌图 后返回一个MD5 作为图片的唯一ID

已显示全部内容