 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
markreflex
Joined: 12 Feb 2008 Posts: 50
|
Posted: Wed May 21, 2008 4:25 am Post subject: output encryption so 3rd party util can decrypt it |
|
|
Is is possible to encrypt some text by autohotkey then write it to a file or the clipboard and a 3rd party program can read it and decrypt either immediately after or at a later date?
This way the text is not written unencrypted to disk first causing it to be read if the file failed to be deleted plus wasted time being encrypted by another software. It can be already encrypted by autohotkey. What encryptions can be encrypted within Autohotkey so that the encryption is compatible with a 3rd party software and can be decrypted? If the encryption is a common one but secure then i can have more choice on a suitable software.
Last edited by markreflex on Wed May 21, 2008 4:33 am; edited 1 time in total |
|
| Back to top |
|
 |
Guest
|
Posted: Wed May 21, 2008 4:33 am Post subject: |
|
|
there are AES, MD5, SHA, Blowfish..etc in forum
which are popular algorithms. just search and pick one |
|
| Back to top |
|
 |
markreflex
Joined: 12 Feb 2008 Posts: 50
|
Posted: Wed May 21, 2008 4:53 am Post subject: |
|
|
| yes i see some, but i need the encryption to be compatible with another software. in case you thinking i can have another seperate autohotkey script decrypt it, i can't it has to be another 3rd party software that can decrypt it. i seen some TEA encryptions but i don't know if they been modified to there own end and will be compatible with other softwares. is there something workable? |
|
| Back to top |
|
 |
markreflex
Joined: 12 Feb 2008 Posts: 50
|
Posted: Wed May 21, 2008 11:56 am Post subject: |
|
|
found a TEA encryption here. i encrypt but the 128bit key doesn't decrypt with any TEA encryption software. http://www.autohotkey.com/forum/viewtopic.php?t=4391
found a BLOWFISH dll and thought it might be the answer yet it has a virus. look at AES nothing useable. the story goes on. seem all half finished projects yet i thought encryption was popular. |
|
| Back to top |
|
 |
greynite
Joined: 17 May 2008 Posts: 11
|
Posted: Wed May 21, 2008 9:42 pm Post subject: |
|
|
What encryption algoritms does your 3rd party program support?
3DES might be the best choice -- very mature (so you're not likely to get stung by any specific implementation), reasonably secure, and very widely supported. Finding AHK support may be the trick.... |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 1397
|
Posted: Thu May 22, 2008 1:10 am Post subject: |
|
|
| The problem in handling the raw data is that the encrypted data can contain embedded NULLs even when the original data consist of plain texts. How would you pass the data between different apps then? Converting them to HEX string etc? That's why preferring to use files. |
|
| Back to top |
|
 |
Guest
|
Posted: Thu May 22, 2008 9:30 am Post subject: |
|
|
| markreflex wrote: | | seem all half finished projects | seem you a half started newbie. sorry can't resisit it. |
|
| Back to top |
|
 |
markreflex
Joined: 12 Feb 2008 Posts: 50
|
Posted: Thu May 22, 2008 2:09 pm Post subject: |
|
|
| greynite wrote: | What encryption algoritms does your 3rd party program support?
3DES might be the best choice -- very mature (so you're not likely to get stung by any specific implementation), reasonably secure, and very widely supported. Finding AHK support may be the trick.... |
There is alot of cmd line tiny utils that use TEA, BLOWFISH or Tiny IDEA. I was hoping any generic existing algorithms used in AHK maybe compatible with generic encryption cmd utils. Hoping was the word. I am not capable of writing my encryption script. I didn't want to write the data to a file as that can then be easily read. What's the point of writing the data to a file to be encrypted if that first written file can be read unencrypted??? I thought about stdin or clipboard even then its a gamble.
| Sean wrote: | | The problem in handling the raw data is that the encrypted data can contain embedded NULLs even when the original data consist of plain texts. How would you pass the data between different apps then? Converting them to HEX string etc? That's why preferring to use files. |
Still surely the output from AHK can be made compatible with a 3rd party util in someway however means. Could convert to hex string and then to unicode,ascii to be readable. You can use redirection and piping in a batch file to send to stdin. can dump stdin to a file.
| Anonymous wrote: | | markreflex wrote: | | seem all half finished projects | seem you a half started newbie. sorry can't resisit it. |
No disrespect to those who can do. I was speaking out my frustration not finding anything juicy and workable after search.
It would be great if AHK had some built in encryption such as RC4 or even simpler encryption that is universal. You can then use FileAppend with the encryption flag or am i dreaming.
FileAppend, hello, data.txt, encryptRC4 |
|
| Back to top |
|
 |
Guest
|
Posted: Thu May 22, 2008 3:07 pm Post subject: |
|
|
| markreflex wrote: | | Sean wrote: | | The problem in handling the raw data is that the encrypted data can contain embedded NULLs even when the original data consist of plain texts. How would you pass the data between different apps then? Converting them to HEX string etc? That's why preferring to use files. |
Still surely the output from AHK can be made compatible with a 3rd party util in someway however means. Could convert to hex string and then to unicode,ascii to be readable. You can use redirection and piping in a batch file to send to stdin. can dump stdin to a file. |
Oops, I think I posted in wrong thread, was supposed to post here.
Anyway, if you're not against using a file, you have all needed means to do it. I copied some code from here.
And I'll use File.ahk which can be downloaded from:
http://www.autohotkey.net/~Sean/Lib/File.zip
| Code: | sTextOriginl := "YOUR_TEXT"
sPassword := "YOUR_PASSWORD"
sFileEncrypt := "PATH_OF_FILE_TO_SAVE_ENCRYPTED_DATA"
SID := 256 ; 256bit AES
nSize := Text_AES(sTextEncrypt, &sTextOriginl, StrLen(sTextOriginl), sPassword, SID, True) ; Encryption
File_ReadMemory(sFileEncrypt, &sTextEncrypt, nSize)
Return
Text_AES(ByRef sResult, pData, nSize, sPassword, SID = 256, bEncrypt = True)
{
VarSetCapacity(sResult, bEncrypt ? nSize+16 : nSize)
DllCall("RtlMoveMemory", "Uint", &sResult, "Uint", pData, "Uint", nSize)
nSize := Crypt_AES(&sResult, nSize, sPassword, SID, bEncrypt)
NumPut(0, sResult, nSize, "char"), VarSetCapacity(sResult,-1)
Return nSize
}
|
|
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 1397
|
Posted: Thu May 22, 2008 3:08 pm Post subject: |
|
|
| Sorry, forgot to login again. The above post was by me. |
|
| Back to top |
|
 |
markreflex
Joined: 12 Feb 2008 Posts: 50
|
Posted: Thu May 22, 2008 3:41 pm Post subject: |
|
|
| Sean wrote: | | Sorry, forgot to login again. The above post was by me. |
you forgot to turn on encryption lol. right thanks. |
|
| Back to top |
|
 |
markreflex
Joined: 12 Feb 2008 Posts: 50
|
Posted: Thu May 22, 2008 3:42 pm Post subject: |
|
|
ok i put the whole lot together so all the functions are seen and outputs file.txt with Hello so not getting encrypted. i am on XP so the dll is there.
| Code: |
sTextOriginl := "Hello"
sPassword := "password"
sFileEncrypt := "file.txt"
SID := 256 ; 256bit AES
nSize := Text_AES(sTextEncrypt, &sTextOriginl, StrLen(sTextOriginl), sPassword, SID, True)
; Encryption
File_ReadMemory(sFileEncrypt, &sTextEncrypt, nSize)
Return
Text_AES(ByRef sResult, pData, nSize, sPassword, SID = 256, bEncrypt = True)
{
VarSetCapacity(sResult, bEncrypt ? nSize+16 : nSize)
DllCall("RtlMoveMemory", "Uint", &sResult, "Uint", pData, "Uint", nSize)
nSize := Crypt_AES(&sResult, nSize, sPassword, SID, bEncrypt)
NumPut(0, sResult, nSize, "char"), VarSetCapacity(sResult,-1)
Return nSize
}
File_Hash(sFile, SID = "CRC32")
{
nSize:= File_WriteMemory(sFile, sBuffer)
Return Crypt_Hash(&sBuffer, nSize, SID)
}
File_AES(sFileFr, sFileTo, sPassword, SID = 256, bEncrypt = True)
{
If !(1 + hFileFr := File_CreateFile(sFileFr, 3, 0x80000000, 1))
Return "File not found!"
nSize := File_GetFileSize(hFileFr), VarSetCapacity(sBuffer, nSize + (bEncrypt ? 16 : 0))
File_ReadFile(hFileFr,&sBuffer,nSize), File_CloseHandle(hFileFr)
If !(1 + hFileTo := File_CreateFile(sFileTo, 2, 0x40000000, 1))
Return "File not created/opened!"
nSize := Crypt_AES(&sBuffer, nSize, sPassword, SID, bEncrypt)
File_WriteFile(hFileTo,&sBuffer,nSize), File_CloseHandle(hFileTo)
Return nSize
}
File_ReadMemory(sFile, pBuffer, nSize = 512, bAppend = False)
{
If !(1 + hFile := File_CreateFile(sFile, 4, 0x40000000, 1))
Return "File not created/opened!"
File_SetFilePointer(hFile, bAppend ? 2 : 0)
nSize := File_WriteFile(hFile, pBuffer, nSize)
File_SetEndOfFile(hFile)
File_CloseHandle(hFile)
Return nSize
}
File_WriteMemory(sFile, ByRef sBuffer, nSize = 0)
{
If !(1 + hFile := File_CreateFile(sFile, 3, 0x80000000, 1))
Return "File not found!"
VarSetCapacity(sBuffer, nSize += nSize ? 0 : File_GetFileSize(hFile))
nSize := File_ReadFile(hFile, &sBuffer, nSize)
NumPut(0, sBuffer, nSize, "char")
VarSetCapacity(sBuffer, -1)
File_CloseHandle(hFile)
Return nSize
}
File_CreateFile(sFile, nCreate = 3, nAccess = 0x1F01FF, nShare = 3, bFolder = False)
{
/*
CREATE_NEW = 1
CREATE_ALWAYS = 2
OPEN_EXISTING = 3
OPEN_ALWAYS = 4
GENERIC_READ = 0x80000000
GENERIC_WRITE = 0x40000000
GENERIC_EXECUTE = 0x20000000
GENERIC_ALL = 0x10000000
FILE_SHARE_READ = 1
FILE_SHARE_WRITE = 2
FILE_SHARE_DELETE = 4
*/
Return DllCall("CreateFile", "Uint", &sFile, "Uint", nAccess, "Uint", nShare,
"Uint", 0, "Uint", nCreate, "Uint", bFolder ? 0x02000000 : 0, "Uint", 0)
}
File_DeleteFile(sFile)
{
Return DllCall("DeleteFile", "Uint", &sFile)
}
File_ReadFile(hFile, pBuffer, nSize = 1024)
{
DllCall("ReadFile", "Uint", hFile, "Uint", pBuffer, "Uint", nSize, "UintP", nSize,
"Uint", 0)
Return nSize
}
File_WriteFile(hFile, pBuffer, nSize = 1024)
{
DllCall("WriteFile", "Uint", hFile, "Uint", pBuffer, "Uint", nSize, "UintP", nSize,
"Uint", 0)
Return nSize
}
File_GetFileSize(hFile)
{
DllCall("GetFileSizeEx", "Uint", hFile, "int64P", nSize)
Return nSize
}
File_SetEndOfFile(hFile)
{
Return DllCall("SetEndOfFile", "Uint", hFile)
}
File_SetFilePointer(hFile, nPos = 0, nMove = 0)
{
/*
FILE_BEGIN = 0
FILE_CURRENT = 1
FILE_END = 2
*/
Return DllCall("SetFilePointerEx", "Uint", hFile, "int64", nMove, "Uint", 0, "Uint",
nPos)
}
File_CloseHandle(Handle)
{
Return DllCall("CloseHandle", "Uint", Handle)
}
File_InternetOpen(sAgent = "AutoHotkey", nType = 4)
{
If Not DllCall("GetModuleHandle", "str", "wininet")
DllCall("LoadLibrary" , "str", "wininet")
Return DllCall("wininet\InternetOpenA", "str", sAgent, "Uint", nType, "Uint", 0,
"Uint", 0, "Uint", 0)
}
File_InternetOpenUrl(hInet, sUrl, nFlags = 0, pHeaders = 0)
{
Return DllCall("wininet\InternetOpenUrlA", "Uint", hInet, "Uint", &sUrl, "Uint",
pHeaders, "Uint", -1, "Uint", nFlags | 0x80000000, "Uint", 0) ; INTERNET_FLAG_RELOAD =
0x80000000
}
File_InternetReadFile(hFile, pBuffer, nSize = 1024)
{
DllCall("wininet\InternetReadFile", "Uint", hFile, "Uint", pBuffer, "Uint", nSize,
"UintP", nSize)
Return nSize
}
File_InternetWriteFile(hFile, pBuffer, nSize = 1024)
{
DllCall("wininet\InternetWriteFile", "Uint", hFile, "Uint", pBuffer, "Uint", nSize,
"UintP", nSize)
Return nSize
}
File_InternetSetFilePointer(hFile, nPos = 0, nMove = 0)
{
Return DllCall("wininet\InternetSetFilePointer", "Uint", hFile, "Uint", nMove,
"Uint", 0, "Uint", nPos, "Uint", 0)
}
File_InternetCloseHandle(Handle)
{
Return DllCall("wininet\InternetCloseHandle", "Uint", Handle)
}
Crypt_Hash(pData, nSize, SID = "CRC32", nInitial = 0)
{
CALG_SHA := CALG_SHA1 := 1 + CALG_MD5 := 0x8003
If Not CALG_%SID%
{
FormatI := A_FormatInteger
SetFormat, Integer, H
sHash := DllCall("ntdll\RtlComputeCrc32", "Uint", nInitial, "Uint", pData,
"Uint", nSize, "Uint")
SetFormat, Integer, %FormatI%
StringUpper, sHash, sHash
StringReplace, sHash, sHash, X, 000000
Return SubStr(sHash,-7)
}
DllCall("advapi32\CryptAcquireContextA", "UintP", hProv, "Uint", 0, "Uint", 0,
"Uint", 1, "Uint", 0)
DllCall("advapi32\CryptCreateHash", "Uint", hProv, "Uint", CALG_%SID%, "Uint", 0,
"Uint", 0, "UintP", hHash)
DllCall("advapi32\CryptHashData", "Uint", hHash, "Uint", pData, "Uint", nSize,
"Uint", 0)
DllCall("advapi32\CryptGetHashParam", "Uint", hHash, "Uint", 2, "Uint", 0, "UintP",
nSize, "Uint", 0)
VarSetCapacity(HashVal, nSize, 0)
DllCall("advapi32\CryptGetHashParam", "Uint", hHash, "Uint", 2, "Uint", &HashVal,
"UintP", nSize, "Uint", 0)
DllCall("advapi32\CryptDestroyHash", "Uint", hHash)
DllCall("advapi32\CryptReleaseContext", "Uint", hProv, "Uint", 0)
FormatI := A_FormatInteger
SetFormat, Integer, H
Loop, %nSize%
sHash .= SubStr(*(&HashVal + A_Index - 1), -1)
SetFormat, Integer, %FormatI%
StringReplace, sHash, sHash, x, 0, All
StringUpper, sHash, sHash
Return sHash
}
; Require Windows XP or Higher!
Crypt_AES(pData, nSize, sPassword, SID = 256, bEncrypt = True)
{
CALG_AES_256 := 1 + CALG_AES_192 := 1 + CALG_AES_128 := 0x660E
CALG_SHA := CALG_SHA1 := 1 + CALG_MD5 := 0x8003
DllCall("advapi32\CryptAcquireContextA", "UintP", hProv, "Uint", 0, "Uint", 0,
"Uint", 24, "Uint", 0)
DllCall("advapi32\CryptCreateHash", "Uint", hProv, "Uint", CALG_SHA1, "Uint", 0,
"Uint", 0, "UintP", hHash)
DllCall("advapi32\CryptHashData", "Uint", hHash, "Uint", &sPassword, "Uint",
StrLen(sPassword), "Uint", 0)
DllCall("advapi32\CryptDeriveKey", "Uint", hProv, "Uint", CALG_AES_%SID%, "Uint",
hHash, "Uint", SID<<16, "UintP", hKey)
DllCall("advapi32\CryptDestroyHash", "Uint", hHash)
If bEncrypt
DllCall("advapi32\CryptEncrypt", "Uint", hKey, "Uint", 0, "Uint", True,
"Uint", 0, "Uint", pData, "UintP", nSize, "Uint", nSize+16)
Else DllCall("advapi32\CryptDecrypt", "Uint", hKey, "Uint", 0, "Uint", True,
"Uint", 0, "Uint", pData, "UintP", nSize)
DllCall("advapi32\CryptDestroyKey", "Uint", hKey)
DllCall("advapi32\CryptReleaseContext", "Uint", hProv, "Uint", 0)
Return nSize
}
|
Last edited by markreflex on Wed May 28, 2008 2:39 pm; edited 1 time in total |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 1397
|
Posted: Thu May 22, 2008 11:30 pm Post subject: |
|
|
| markreflex wrote: | | ok i put the whole lot together so all the functions are seen and outputs file.txt with Hello so not getting encrypted. i am on XP so the dll is there. |
I recently fresh-installed XPSP3, and the AES/Hash scripts stopped working. It was due to the last parameter of CryptAcquireContext. I just updated Crypt.ahk accordingly, so you may try again after re-downloading File.zip. |
|
| Back to top |
|
 |
markreflex
Joined: 12 Feb 2008 Posts: 50
|
Posted: Mon May 26, 2008 8:51 pm Post subject: |
|
|
Sean, Thanks but I now get this as my output:
K7
It is not useable. The characters do not copy and paste. Should there be big spaces in there? |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 1397
|
Posted: Tue May 27, 2008 1:31 am Post subject: |
|
|
| markreflex wrote: | | It is not useable. The characters do not copy and paste. Should there be big spaces in there? |
They can be non-printable characters. The encrypted data can contain any ASCII codes in the range of 0 - 255 even if the original data is a plain text, and there is no guarantee that they will remain unchanged when copied into clipboard. So, it's not such a good idea to handle the encrypted data in raw.
Doesn't your target application accept a file as the encrypted data? IMO, files are the safest means to pass encrypted data between apps. If you don't like to use files, then you may use/convert to hex strings as the next choice, however, your target application should be prepared to handle the hex string as the encrypted data. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|