码迷,mamicode.com
首页 > 编程语言 > 详细

EXCEL VBA计算带汉字的字符串的MD5值(VBA转UTF-8保存)

时间:2020-02-25 16:09:54      阅读:385      评论:0      收藏:0      [点我收藏+]

标签:open   cert   cmd   cer   输出   copy   adt   win   util   

操作思路:
1.使用 ADODB.stream 把字符串转换为UFT-8的编码集存储到文件 AA
2.使用调用windows命令行CertUtil -hashfile c:\11\AA md5 对文件计算MD5值
3.回显结果直接输出到EXCEL
4.字符串截取第二行,去空格,输出MD5值

VBA代码:

strBody = "VBA变量字符串转换为UFT-8后MD5加密"

‘MD5计算
Dim stream As New ADODB.stream
stream.Open
stream.Type = adTypeText
stream.Charset = "utf-8"
stream.WriteText strBody
‘ Try to Remove UTF-8 BOM
stream.Position = 3 ‘skip BOM
Dim BinaryStream As New ADODB.stream
BinaryStream.Type = adTypeBinary
BinaryStream.Mode = adModeReadWrite
BinaryStream.Open
‘ Strips BOM (first 3 bytes)
stream.CopyTo BinaryStream
stream.Flush
stream.Close
str8 = BinaryStream.Read
BinaryStream.SaveToFile "C:\11\AA", adSaveCreateOverWrite
BinaryStream.Flush
BinaryStream.Close
Dim WSH, wExec, sCmd As String
Set WSH = CreateObject("WScript.Shell")
Set wExec = WSH.Exec("CertUtil -hashfile c:\11\AA md5")
Ar = Split(wExec.StdOut.ReadAll, vbCrLf)
N = UBound(Ar)
Sign = Replace(Ar(1), Space(1), "")
Set wExec = Nothing
Set WSH = Nothing
Range("A12") = strBody

EXCEL VBA计算带汉字的字符串的MD5值(VBA转UTF-8保存)

标签:open   cert   cmd   cer   输出   copy   adt   win   util   

原文地址:https://blog.51cto.com/mflag/2473469

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!