#INCLUDE ONCE "BCrypt.inc"
MACRO ALG_ID = DWORD
'//-----------------------------------------------------------------------
'// 生成AES密码 32位
'//-----------------------------------------------------------------------
FUNCTION AES_ComputeHash(BYVAL buf AS STRING, BYVAL pass AS STRING, BYVAL Algid AS ALG_ID) AS STRING
LOCAL i AS LONG
LOCAL hCryptProv AS DWORD
LOCAL hCryptHash AS DWORD
LOCAL hCryptKey AS DWORD
LOCAL bRet AS LONG
LOCAL pData AS BYTE PTR
LOCAL aes AS STRING
LOCAL dwDataLength, dwBufferLength, dwKeyLength AS DWORD
'// 获得指定CSP的密钥容器的句柄
bRet = CryptAcquireContext(hCryptProv, BYVAL %NULL, BYVAL %NULL, %PROV_RSA_AES, %CRYPT_VERIFYCONTEXT)
IF(bRet<>1) THEN
FUNCTION = ""
EXIT FUNCTION
END IF
'//创建一个HASH对象, 指定HASH算法
bRet = CryptCreateHash(hCryptProv, %CALG_MD5, BYVAL %NULL, 0, hCryptHash)
IF(bRet<>1) THEN
FUNCTION = ""
EXIT FUNCTION
END IF
'//计算HASH数据
bRet = CryptHashData(hCryptHash, BYVAL STRPTR(pass), LEN(pass), 0)
IF(bRet<>1) THEN
FUNCTION = ""
EXIT FUNCTION
END IF
'//生成加密hCryptKey
SELECT CASE Algid
CASE %CALG_AES_128
bRet = CryptDeriveKey(hCryptProv, %CALG_AES_128, hCryptHash, %CRYPT_EXPORTABLE, hCryptKey) '16 * 8
IF bRet>0 THEN MSGBOX "128"
CASE %CALG_AES_192
bRet = CryptDeriveKey(hCryptProv, %CALG_AES_192, hCryptHash, %CRYPT_EXPORTABLE, hCryptKey) '24 * 8
IF bRet>0 THEN MSGBOX "192"
CASE %CALG_AES_256
bRet = CryptDeriveKey(hCryptProv, %CALG_AES_256, hCryptHash, %CRYPT_EXPORTABLE, hCryptKey) '32 * 8
IF bRet>0 THEN MSGBOX "256"
END SELECT
pData = STRPTR(buf)
dwDataLength = LEN(buf)+1
dwBufferLength = %MAX_PATH
bRet = CryptEncrypt(hCryptKey, BYVAL %NULL, %TRUE, 0, pData, dwDataLength, dwBufferLength)
'bRet = CryptDecrypt(hCryptKey, BYVAL %NULL, %TRUE, 0, pData, BYREF dwDataLength)
FOR i=0 TO dwDataLength-1
aes = aes + CHR$(@pData[i])
NEXT i
IF(hCryptKey>0) THEN CryptDestroyKey(hCryptKey)
IF(hCryptHash>0) THEN CryptDestroyHash(hCryptHash)
IF(hCryptProv>0) THEN CryptReleaseContext(hCryptProv, 0)
FUNCTION = aes
END FUNCTION
PowerBasic 生成AES密码 32位
于 2022-09-24 12:18:10 首次发布