Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: CryptAcquireContext : Gets a handle to a particular key container within a particular CSP
;Crypto API Functions ;Guido 12/01 ;Further hacked by MW oct-10-2002. Code may be in bad shape ;tried to fix CryptEncryptStr and CryptDecriptStr ;as *sometimes* there are zeros in the buffer data ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;CryptAcquireContext : Gets a handle to a particular key container within a ; ;particular CSP. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;pszContainer : The key container name. ; ;pszProvider : The provider name. ; ;dwProvType : The type of provider to acquire. ; ;dwFlags : Normally set to zero. ; ;Returns : A handle to a provider. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; #DefineFunction CryptAcquireContext(pszContainer, pszProvider, dwProvType, dwFlags) sDLLName = StrCat(DirWindows(1), "advapi32.dll") phProv = BinaryAlloc(100) xx = DLLCall(sDLLName, long:"CryptAcquireContextA", lpbinary:phProv, lpstr:pszContainer, lpstr:pszProvider, long:dwProvType, long:dwFlags) BinaryEodSet(phProv, 100) hprovider = BinaryPeek4(phProv, 0) BinaryFree(phProv) Return hprovider #EndFunction ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;CryptCreateHash : Initializes the hashing of a stream of data. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;hProv : A handle to the CSP to use. ; ;Algid : An algorithm identifier of the hash algorithm to use. ; ;hKey : Key for the hash if needed. ; ;dwFlags : Reserved, should always be zero. ; ;Returns : Handle to the new hash object. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; #DefineFunction CryptCreateHash(hProv, Algid, hKey, dwFlags) phHash = BinaryAlloc(100) sDLLName = StrCat(DirWindows(1), "advapi32.dll") xx = DLLCall(sDLLName, long:"CryptCreateHash", long:hProv, long:Algid, long:hKey, long:dwFlags, lpbinary:phhash) BinaryEodSet(phHash, 100) hhash = BinaryPeek4(phhash, 0) BinaryFree(phHash) Return hhash #EndFunction ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;CryptHashData : Computes the cryptographic hash on a stream of data. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;hHash : A handle to the hash object. ; ;pbData : Data to be hashed. ; ;dwDataLen : The number of bytes of data to be hashed. ; ;dwFlags : The flag values. ; ;Returns : If the function succeeds, the return value is nonzero. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; #DefineFunction CryptHashData(hHash, pbData, dwDataLen, dwFlags) sDLLName = StrCat(DirWindows(1), "advapi32.dll") xx = DLLCall(sDLLName, long:"CryptHashData", long:hHash, lpstr:pbData, long:dwDataLen, long:dwFlags) Return xx #EndFunction ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;CryptDeriveKey : Generates cryptographic keys derived from base data. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;hprov : A handle to the application’s CSP. ; ;Algid : The identifier for the algorithm for which the key is to be generated.; ;hBaseData: A handle to a hash object. ; ;dwFlags : The flags specifying the type of key generated. ; ;Returns : A handle of the newly generated key. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; #DefineFunction CryptDeriveKey(hprov, Algid, hBaseData, dwFlags) phKey = binaryalloc(100) sDLLName = StrCat(DirWindows(1), "advapi32.dll") xx = DLLCall(sDLLName, long:"CryptDeriveKey", long:hProv, long:Algid, long:hBaseData, long:dwFlags, lpbinary:phKey) binaryeodset(phKey, 100) hkey = BinaryPeek4(phKey, 0) binaryfree(phkey) Return hkey #EndFunction ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;CryptEncryptStr : Encrypts a string. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;hKey : A handle to the key to use for the encryption. ; ;hHash : A handle to a hash object. ; ;Final : Specifies whether this is the last section in a series being decrypted.; ;dwFlags : Reserved, should always be zero. ; ;Data : String to be encrypted. ; ;DataLen : Number of bytes to be encrypted. ; ;dwBufLen: Size of the buffer that will hold the string. ; ;Returns : The encrypted string. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; #DefineFunction CryptEncryptStr(hKey, hHash, Final, dwFlags, Data, DataLen, dwBufLen) sDLLName = StrCat(DirWindows(1), "advapi32.dll") pdwDataLen = BinaryAlloc(4) pbData = binaryalloc(dwBufLen) BinaryEodSet(pbData, dwBufLen) BinaryEodSet(pdwDataLen, 4) BinaryPokeStr(pbData, 0, data) BinaryPoke4(pdwDataLen, 0, DataLen) xx = DLLCall(sDLLName, long:"CryptEncrypt", long:hKey, long:hHash, long:Final, long:dwFlags, lpbinary:pbData, lpbinary:pdwDataLen, long:dwBufLen) pbindex=BinaryPeek4(pdwDatalen,0) -1 retdata="" for xx=0 to pbindex byte=BinaryPeek(pbData,xx) bytea=num2char((byte >> 4) + 65) byteb=num2char((byte & 15) + 65) retdata=strcat(retdata,bytea,byteb) next BinaryFree(pbData) BinaryFree(pdwDataLen) return retdata #EndFunction ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;CryptDecryptStr : Decrypts a string. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;hKey : A handle to the key to use for the decryption. ; ;hHash : A handle to a hash object. ; ;Final : Specifies whether this is the last section in a series being decrypted.; ;dwFlags : Reserved, should always be zero. ; ;Data : String to be decrypted. ; ;DataLen : Number of bytes to be decrypted. ; ;dwBufLen: Size of the buffer that will hold the string. ; ;Returns : The decrypted string. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; #DefineFunction CryptDecryptStr(hKey, hHash, Final, dwFlags, Data, DataLen, dwBufLen) sDLLName = StrCat(DirWindows(1), "advapi32.dll") pbData = BinaryAlloc(dwBufLen) for xx=1 to DataLen by 2 bytea=Strsub(data,xx,1) byteb=Strsub(data,xx+1,1) bytea=(Char2num(bytea)-65) << 4 byteb=Char2num(byteb)-65 BinaryPoke(pbData,(xx-1)/2,bytea|byteb) next pdwDataLen = BinaryAlloc(100) ;BinaryPokeStr(pbData, 0, data) BinaryPoke4(pdwDataLen, 0, DataLen) xx = DLLCall(sDLLName, long:"CryptDecrypt", long:hKey, long:hHash, long:Final, long:dwFlags, lpbinary:pbData, lpbinary:pdwDataLen) BinaryEodSet(pbData, BinaryEodGet(pbData)) BinaryEodSet(pdwDataLen, 100) daData=BinaryPeekStr(pbData, 0, BinaryEodGet(pbData)) BinaryFree(pbData) BinaryFree(pdwDataLen) Return DaData #EndFunction ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;CryptEncrypt : Encrypts a buffer. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;hKey : A handle to the key to use for the encryption. ; ;hHash : A handle to a hash object. ; ;Final : Specifies whether this is the last section in a series being decrypted.; ;dwFlags : Reserved, should always be zero. ; ;pbData : Handle to the buffer to encrypt. ; ;DataLen : Number of bytes to be encrypted. ; ;dwBufLen: Size of the buffer to encrypt. ; ;Returns : If the function succeeds, the return value is nonzero. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; #DefineFunction CryptEncrypt(hKey, hHash, Final, dwFlags, pbData, DataLen, dwBufLen) sDLLName = StrCat(DirWindows(1), "advapi32.dll") pdwDataLen = BinaryAlloc(100) BinaryPoke4(pdwDataLen, 0, DataLen) xx = DLLCall(sDLLName, long:"CryptEncrypt", long:hKey, long:hHash, long:Final, long:dwFlags, lpbinary:pbData, lpbinary:pdwDataLen, long:dwBufLen) BinaryEodSet(pbData, binaryeodget(pbData)) BinaryEodSet(pdwDataLen, 100) BinaryFree(pdwDataLen) Return xx #EndFunction ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;CryptDecrypt : Decrypts a buffer. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;hKey : A handle to the key to use for the decryption. ; ;hHash : A handle to a hash object. ; ;Final : Specifies whether this is the last section in a series being decrypted.; ;dwFlags : Reserved, should always be zero. ; ;pbData : Handle of the buffer to be decrypted. ; ;DataLen : Number of bytes to be decrypted. ; ;Returns : If the function succeeds, the return value is nonzero. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; #DefineFunction CryptDecrypt(hKey, hHash, Final, dwFlags, pbData, DataLen) sDLLName = StrCat(DirWindows(1), "advapi32.dll") pdwDataLen = BinaryAlloc(100) BinaryPoke4(pdwDataLen, 0, DataLen) xx = DLLCall(sDLLName, long:"CryptDecrypt", long:hKey, long:hHash, long:Final, long:dwFlags, lpbinary:pbData, lpbinary:pdwDataLen) BinaryEodSet(pbData, BinaryEodGet(pbData)) BinaryEodSet(pdwDataLen, 100) Return xx BinaryFree(pdwDataLen) #EndFunction ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;CryptDestroyHash : Destroys a hash object. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;hHash : A handle to the hash object to be destroyed. ; ;Returns : If the function succeeds, the return value is nonzero. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; #DefineFunction CryptDestroyHash(hHash) sDLLName = StrCat(DirWindows(1), "advapi32.dll") xx = DLLCall(sDLLName, long:"CryptDestroyHash", long:hHash) Return xx #EndFunction ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;CryptDestroyKey : Releases the handle referenced by the hKey parameter. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;hKey : A handle to the key to be destroyed. ; ;Returns : If the function succeeds, the return value is nonzero. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; #DefineFunction CryptDestroyKey(hKey) sDLLName = StrCat(DirWindows(1), "advapi32.dll") xx = DLLCall(sDLLName, long:"CryptDestroyKey", long:hKey) Return xx #EndFunction ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;CryptReleaseContext : Releases a handle to a CSP and a key container. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;hProv : A handle to the application’s CSP. ; ;dwFlags : Reserved, should always be zero. ; ;Returns : If the function succeeds, the return value is nonzero. ; ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; #DefineFunction CryptReleaseContext(hProv, dwFlags) sDLLName = StrCat(DirWindows(1), "advapi32.dll") xx = DLLCall(sDLLName, long:"CryptReleaseContext", long:hProv, long:dwFlags) Return xx #EndFunction ;END ;Constants PROV_RSA_FULL = 1 CALG_MD5 = 32771 CRYPT_EXPORTABLE = 1 CALG_RC4 = 26625 ;Get handle to user default provider hProv = CryptAcquireContext("", "", PROV_RSA_FULL, 0) ;Create hash object hHash = CryptCreateHash(hProv, CALG_MD5, 0, 0) ;Hash password string CryptHashData(hHash, "password sunil", strlen("password sunil"), 0) ;Create block cipher session key based on hash of the password hKey = CryptDeriveKey(hprov, CALG_RC4, hHash, CRYPT_EXPORTABLE) ;Encrypt string data = "sunil varma" encdata = CryptEncryptStr(hKey, 0, @true, 0, data, strlen(data), 20) message("",encdata) ;Decrypt string decdata = CryptDecryptStr(hKey, 0, @true, 0, encdata, strlen(encdata), 20) message("",decdata) ;Free resources CryptDestroyHash(hHash) CryptDestroyKey(hKey) CryptReleaseContext(hProv, 0) exit
My thoughts:
Article ID: W15447
File Created: 2003:05:29:12:50:32
Last Updated: 2003:05:29:12:50:32