急!!!请教关于加密的API-advapi32.dll

labelliu 2003-10-21 03:54:10
我使用advapi32.dll提供的函数(没找到文档)加密数据(每次长为8的字符串),解密输出后的字符串经常有乱码(如“="Micros”,加密后是“?ec;7k ”,解密后则成了“?#o;~38葿”)。我想是Unicode或DBCS方面出的问题,不知如何解决,或有更友好的API没有?
请教高手指南,多谢!

先把类代码附上,这是FoxAPI中提供的代码示例:
------------------------------------
类的声明
----
Option Explicit
Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (phProv As Long, pszContainer As String, pszProvider As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptCreateHash Lib "advapi32.dll" (ByVal hProv As Long, ByVal Algid As Long, ByVal hKey As Long, ByVal dwFlags As Long, phHash As Long) As Long
Private Declare Function CryptDeriveKey Lib "advapi32.dll" (ByVal hProv As Long, ByVal Algid As Long, ByVal hBaseData As Long, ByVal dwFlags As Long, phKey As Long) As Long
Private Declare Function CryptDestroyHash Lib "advapi32.dll" (ByVal hHash As Long) As Long
Private Declare Function CryptDestroyKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function CryptEncrypt Lib "advapi32.dll" (ByVal hKey As Long, ByVal hHash As Long, ByVal Final As Long, ByVal dwFlags As Long, ByVal pbData As String, pdwDataLen As Long, ByVal dwBufLen As Long) As Long
Private Declare Function CryptDecrypt Lib "advapi32.dll" (ByVal hKey As Long, ByVal hHash As Long, ByVal Final As Long, ByVal dwFlags As Long, ByVal pbData As String, pdwDataLen As Long) As Long
Private Declare Function CryptExportKey Lib "advapi32.dll" (ByVal hKey As Long, ByVal hExpKey As Long, ByVal dwBlobType As Long, ByVal dwFlags As Long, ByVal pbData As String, pdwDataLen As Long) As Long
Private Declare Function CryptGenKey Lib "advapi32.dll" (ByVal hProv As Long, ByVal Algid As Long, ByVal dwFlags As Long, phKey As Long) As Long
Private Declare Function CryptGetProvParam Lib "advapi32.dll" (ByVal hProv As Long, ByVal dwParam As Long, pbData As Any, pdwDataLen As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptGetUserKey Lib "advapi32.dll" (ByVal hProv As Long, ByVal dwKeySpec As Long, phUserKey As Long) As Long
Private Declare Function CryptHashData Lib "advapi32.dll" (ByVal hHash As Long, ByVal pbData As String, ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByVal hProv As Long, ByVal dwFlags As Long) As Long
Private Declare Function CryptSignHash Lib "advapi32.dll" Alias "CryptSignHashA" (ByVal hHash As Long, ByVal dwKeySpec As Long, ByVal sDescription As String, ByVal dwFlags As Long, ByVal pbSignature As String, pdwSigLen As Long) As Long
Private Declare Function CryptVerifySignature Lib "advapi32.dll" Alias "CryptVerifySignatureA" (ByVal hHash As Long, ByVal pbSignature As String, ByVal dwSigLen As Long, ByVal hPubKey As Long, ByVal sDescription As String, ByVal dwFlags As Long) As Long

'API error function
Private Declare Function GetLastError Lib "kernel32" () As Long

'API memory functions
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Sub CpyMemValAdrFromRefAdr Lib "kernel32" Alias "RtlMoveMemory" (ByVal hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Sub CpyMemRefAdrFromValAdr Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, ByVal hpvSource As Any, ByVal cbCopy As Long)

'constants for API memory functions
Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_ZEROINIT = &H40
Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)

'constants for Cryptography API functions
Private Const MS_DEF_PROV = "Microsoft Base Cryptographic Provider v1.0"
Private Const PROV_RSA_FULL = 1
Private Const CRYPT_NEWKEYSET = &H8
Private Const PP_CONTAINER = 6
Private Const AT_KEYEXCHANGE = 1
Private Const AT_SIGNATURE = 2

Private Const SIMPLEBLOB = 1

Private Const ALG_CLASS_DATA_ENCRYPT = 24576
Private Const ALG_CLASS_HASH = 32768
Private Const ALG_TYPE_ANY = 0
Private Const ALG_TYPE_BLOCK = 1536
Private Const ALG_TYPE_STREAM = 2048
Private Const ALG_SID_RC2 = 2
Private Const ALG_SID_RC4 = 1
Private Const ALG_SID_MD5 = 3

Private Const CALG_MD5 = ((ALG_CLASS_HASH Or ALG_TYPE_ANY) Or ALG_SID_MD5)
Private Const CALG_RC2 = ((ALG_CLASS_DATA_ENCRYPT Or ALG_TYPE_BLOCK) Or ALG_SID_RC2)
Private Const CALG_RC4 = ((ALG_CLASS_DATA_ENCRYPT Or ALG_TYPE_STREAM) Or ALG_SID_RC4)

'constants from WinErr.h
Private Const NTE_NO_KEY As Long = -2146893811 '0x8009000DL
Private Const NTE_BAD_SIGNATURE As Long = -2146893818

'clsCryptoFilterBox constants
Private Const CFB_BUSY = 0
Private Const CFB_READY = 1
Private Const CFB_VALID = 2

Private Const ENCRYPT_ALGORITHM = CALG_RC4
Private Const ENCRYPT_BLOCK_SIZE = 1

Private Const CRYPT_EXPORTABLE = 1

'private property buffers
Private sInBuffer As String
Private sOutBuffer As String
Private sPassword As String
...全文
170 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
rainstormmaster 2003-10-22
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Dim s As String
s = "高高兴兴abc"
s = Dcode(s, 56)
Debug.Print s
s = Ecode(s, 56)
Debug.Print s
End Sub
Private Function Dcode(ByVal str As String, ByVal key As Byte) As String '加密,输出为16进制表示的数字字串
Dim buff() As Byte
buff = StrConv(str, vbFromUnicode)
Dim i As Long
Dim s As String, temps As String
For i = 0 To UBound(buff)
buff(i) = buff(i) Xor key
temps = FStr(buff(i))
s = s + temps
Next
Dcode = s
End Function
Private Function FStr(ByVal mbyte As Byte) As String
Dim i As Long
Dim s As String
s = Hex(mbyte)
s = String(2 - Len(s), "0") + s
FStr = s
End Function
Private Function Ecode(ByVal str As String, ByVal key As Byte) As String '解密
Dim buff() As Byte, j As Long
j = Len(str)
If j Mod 2 = 1 Then
Ecode = ""
Exit Function
End If
Dim i As Long
Dim s As String, temps As String
For i = 1 To j Step 2
temps = Mid(str, i, 2)
ReDim Preserve buff((i - 1) / 2)
buff((i - 1) / 2) = Val("&H" & temps)
buff((i - 1) / 2) = buff((i - 1) / 2) Xor key
Next
Ecode = StrConv(buff, vbUnicode)
End Function
labelliu 2003-10-21
  • 打赏
  • 举报
回复
'这是测试用的代码
'---------

Private Sub EnCrypt_Click()
With m_cls_Cryptor
.Password = Text1.Text
.InBuffer = Text3.Text
.EnCrypt
Text4.Text = .OutBuffer
End With
End Sub

Private Sub DeCrypt_Click()
With m_cls_Cryptor
.Password = Text1.Text
.InBuffer = CStr(Text4.Text)
.DeCrypt
Text5.Text = .OutBuffer
End With
End Sub

根据以上,能否给出问题的关键?
labelliu 2003-10-21
  • 打赏
  • 举报
回复
'类初始化
'----
Private Function InitUser() As Long
Dim lHCryptprov As Long, lHCryptKey As Long, avProviderData(1000) As Byte
Dim lProviderDataAddress As Long, lProviderDataLen As Long, lDataSize As Long
Dim lResult As Long, sContainer As String, sProvider As String
Dim sUserName As String, lPoint As Long, lMemHandle As Long
Dim lReturn As Long, sBuffer As String

On Error GoTo ErrInitUser
'prepare string buffers

sContainer = vbNullChar
sProvider = MS_DEF_PROV & vbNullChar

'Attempt to acquire a handle to the default key container.
If Not CBool(CryptAcquireContext(lHCryptprov, ByVal sContainer, ByVal sProvider, PROV_RSA_FULL, 0)) Then

'Create default key container.
If Not CBool(CryptAcquireContext(lHCryptprov, ByVal sContainer, ByVal sProvider, PROV_RSA_FULL, CRYPT_NEWKEYSET)) Then
MsgBox ("Error creating key container! " & CStr(GetLastError))
Exit Function
End If

'Get name of default key container.
lProviderDataLen = Len(avProviderData(0)) * (UBound(avProviderData) + 1)
If Not CBool(CryptGetProvParam(lHCryptprov, PP_CONTAINER, avProviderData(0), lProviderDataLen, 0)) Then
MsgBox ("Error getting user name! " & CStr(GetLastError))
avProviderData(0) = 0
End If

'Get sUserName from avProviderData()
lPoint = LBound(avProviderData)
While lPoint <= UBound(avProviderData)
If avProviderData(lPoint) <> 0 Then
sUserName = sUserName & Chr$(avProviderData(lPoint))
Else
lPoint = UBound(avProviderData)
End If
lPoint = lPoint + 1
Wend

MsgBox ("Create key container " & sUserName)

End If

'Attempt to get handle to signature key
If Not CBool(CryptGetUserKey(lHCryptprov, AT_SIGNATURE, lHCryptKey)) Then
If GetLastError = NTE_NO_KEY Then
MsgBox ("Create key exchange key pair")
If Not CBool(CryptGenKey(lHCryptprov, AT_SIGNATURE, 0, lHCryptKey)) Then
MsgBox ("Error during CryptGenKey! " & CStr(GetLastError))
Exit Function
Else
lResult = CryptDestroyKey(lHCryptprov)
End If
Else
MsgBox ("Error during CryptGetUserKey! " & CStr(GetLastError))
Exit Function
End If
End If

'Attempt to get handle to exchange key
If Not CBool(CryptGetUserKey(lHCryptprov, AT_KEYEXCHANGE, lHCryptKey)) Then
If GetLastError = NTE_NO_KEY Then
MsgBox ("Create key exchange key pair")
If Not CBool(CryptGenKey(lHCryptprov, AT_KEYEXCHANGE, 0, lHCryptKey)) Then
MsgBox ("Error during CryptGenKey! " & CStr(GetLastError))
Exit Function
Else
lResult = CryptDestroyKey(lHCryptprov)
End If
Else
MsgBox ("Error during CryptGetUserKey! " & CStr(GetLastError))
Exit Function
End If
End If

'release handle to provider
lResult = CryptReleaseContext(lHCryptprov, 0)
InitUser = True

Exit Function

ErrInitUser:
MsgBox ("ErrInitUser " & Error$)
Resume

End Function
Private Sub Class_Initialize()
If InitUser = True Then
MsgBox ("InitUser OK")
Else
MsgBox ("InitUser failed")
End If
End Sub

'加密与解密函数
--------
Public Sub EnCrypt()
'Encrypt InBuffer into OutBuffer

Dim lHExchgKey As Long, lHCryptprov As Long, lHHash As Long, lHkey As Long
Dim lResult As Long, sContainer As String, sProvider As String, sCryptBuffer As String
Dim lCryptLength As Long, lCryptBufLen As Long

On Error GoTo ErrEncrypt

'switch Status property
lStatus = CFB_BUSY

'Get handle to the default provider
sContainer = vbNullChar
sProvider = vbNullChar
sProvider = MS_DEF_PROV & vbNullChar
If Not CBool(CryptAcquireContext(lHCryptprov, ByVal sContainer, ByVal sProvider, PROV_RSA_FULL, 0)) Then
MsgBox ("Error " & CStr(GetLastError) & " during CryptAcquireContext!")
GoTo Done
End If

'Create a hash object.
If Not CBool(CryptCreateHash(lHCryptprov, CALG_MD5, 0, 0, lHHash)) Then
MsgBox ("Error " & CStr(GetLastError) & " during CryptCreateHash!")
GoTo Done
End If

'Hash in the password data.
If Not CBool(CryptHashData(lHHash, sPassword, Len(sPassword), 0)) Then
MsgBox ("Error " & CStr(GetLastError) & " during CryptHashData!")
GoTo Done
End If

'Derive a session key from the hash object.
If Not CBool(CryptDeriveKey(lHCryptprov, ENCRYPT_ALGORITHM, lHHash, 0, lHkey)) Then
MsgBox ("Error " & CStr(GetLastError) & " during CryptDeriveKey!")
GoTo Done
End If

'Destroy the hash object.
CryptDestroyHash (lHHash)
lHHash = 0

'Prepare a string buffer for the CryptEncrypt function
lCryptLength = Len(sInBuffer)
lCryptBufLen = lCryptLength * 2
sCryptBuffer = String(lCryptBufLen, vbNullChar)
LSet sCryptBuffer = sInBuffer

'Encrypt data
If Not CBool(CryptEncrypt(lHkey, 0, 1, 0, sCryptBuffer, lCryptLength, lCryptBufLen)) Then
MsgBox ("bytes required:" & CStr(lCryptLength))
MsgBox ("Error " & CStr(GetLastError) & " during CryptEncrypt!")
'GoTo Done
End If

sOutBuffer = Mid$(sCryptBuffer, 1, lCryptLength)

Done:

'Destroy session key.
If (lHkey) Then lResult = CryptDestroyKey(lHkey)

'Release key exchange key handle.
If lHExchgKey Then CryptDestroyKey (lHExchgKey)

'Destroy hash object.
If lHHash Then CryptDestroyHash (lHHash)

'Release provider handle.
If lHCryptprov Then lResult = CryptReleaseContext(lHCryptprov, 0)

'switch Status property
lStatus = CFB_READY

Exit Sub

ErrEncrypt:

MsgBox ("ErrEncrypt " & Error$)
Resume

End Sub



Public Property Let InBuffer(vNewValue As String)
sInBuffer = vNewValue
End Property
Public Property Get OutBuffer() As String
OutBuffer = sOutBuffer
End Property

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧