Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
;Crypto API Functions ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; ;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 #DefineFunction udfEncrypt(mString, mHack) ;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, mHack, StrLen(mHack), 0) ;Create block cipher session key based on hash of the password hKey = CryptDeriveKey(hprov, CALG_RC4, hHash, CRYPT_EXPORTABLE) ;Encrypt string mEncData = CryptEncryptStr(hKey, 0, @TRUE, 0, mString, StrLen(mString), 20) ;Free resources CryptDestroyKey(hKey) CryptDestroyHash(hHash) CryptReleaseContext(hProv, 0) Return mEncData #EndFunction #DefineFunction udfDecrypt(mString,mHack) ;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, mHack, StrLen(mHack), 0) ;Create block cipher session key based on hash of the password hKey = CryptDeriveKey(hprov, CALG_RC4, hHash, CRYPT_EXPORTABLE) ;Decrypt string mDecData = CryptDecryptStr(hKey, 0, @TRUE, 0, mString, StrLen(mString), 20) ;Free resources CryptDestroyKey(hKey) CryptDestroyHash(hHash) CryptReleaseContext(hProv, 0) Return mDecData #EndFunction
KeyString = AskLine('CryptoAPI', 'Enter the Key String', '') pwd = askpassword('CriptoAPI', 'Please enter the password to encript') mencript = udfEncrypt(pwd,KeyString) x = AskLine('CryptoAPI', 'Here is your password - copy for safe keeping', mencript) pwd2 = udfDecrypt(mencript, KeyString) message("decrypted password", pwd2)
Article ID: W15928
File Created: 2004:03:30:15:41:44
Last Updated: 2004:03:30:15:41:44