Crypting with the AES lib?
Posted: Sun Oct 02, 2005 1:42 am
Hello,
Well, after 4 or so hours scortching the forum looking for a way to encrypt and decrypt with Sec's AES lib, I came out with this code:
I successfully used it to encrypt a file, but I have not yet been able to decrypt the file. Any ideas on what is going wrong?
This is the code I am using to call it:
(file$ = "C:\prog.exe", key$="TheKey")
Well, after 4 or so hours scortching the forum looking for a way to encrypt and decrypt with Sec's AES lib, I came out with this code:
Code: Select all
Procedure AES_Crypt_File(Mode, InFilename.s, OutFileName.s, key.s, FileReadBufferSize)
;Mode 0 = Encrypt
;Mode 1 = Decrypt
If FileReadBufferSize % 16 = 0
#OutFile = 0
#InFile = 1
If Mode = 0
packed_filename.s = InFilename + ".pack"
res = CreatePack(packed_filename)
If res <> 0
res = AddPackFile(InFilename , 9)
ClosePack()
Else
MessageRequester(#title,"Error!")
ProcedureReturn
EndIf
InFilename = packed_filename
EndIf
FileIn=OpenFile(#InFile,InFilename.s)
If FileIn<>0
*KeyBuffer = AllocateMemory(32)
key = key + key
j=-1
For i=1 To Len(key) Step 2
j=j+1
PokeB(*KeyBuffer+j,hex2dec(Mid(key,i,2)))
Next i
AES256Init(*KeyBuffer)
*InputBuffer = AllocateMemory(FileReadBufferSize)
*OutputBuffer = AllocateMemory(FileReadBufferSize)
*PlainBlock = AllocateMemory(16)
*EncryptionBlock = AllocateMemory(16)
FileLength = FileSize(InFilename)
BlocksToRead = Int(FileLength/FileReadBufferSize)
Result=CreateFile(#OutFile,OutFileName)
If Result
For Offset = 0 To BlocksToRead
If Offset<BlocksToRead
BlockSize = FileReadBufferSize
Else
BlockSize = FileLength-Offset*FileReadBufferSize
EndIf
UseFile(1)
FileSeek(Offset*FileReadBufferSize)
Result=ReadData(*InputBuffer,BlockSize)
If BlockSize<>FileReadBufferSize And Mode = 0
PadBytes = (FileReadBufferSize - BlockSize) % 16
For i=0 To PadBytes-1
PokeB(*InputBuffer+BlockSize+i,PadBytes)
Next i
BlockSize = BlockSize + PadBytes
EndIf
UseFile(0)
For EncryptionBlockOffset = 0 To FileReadBufferSize / 16
CopyMemory(*InputBuffer+EncryptionBlockOffset*16, *PlainBlock , 16)
If Mode = 0
*EncryptionBlock = AES256Encrypt(*PlainBlock)
ElseIf Mode = 1
*EncryptionBlock = AES256Decrypt(*PlainBlock)
EndIf
CopyMemory(*EncryptionBlock,*OutputBuffer+EncryptionBlockOffset*16,16)
Next EncryptionBlockOffset
FileSeek(Offset*FileReadBufferSize)
If Mode=1 And ((Offset = BlocksToRead) Or (FileLength % FileReadBufferSize = 0))
PaddedBytes=PeekB(*OutputBuffer+BlockSize-1) & $FF
For i=1 To PaddedBytes
If PeekB(*OutputBuffer+BlockSize-i)<>PaddedBytes
Break
EndIf
Next i
If i>PaddedBytes And PaddedBytes<16
BlockSize=BlockSize-PaddedBytes
EndIf
EndIf
WriteData(*OutputBuffer,BlockSize)
Next Offset
CloseFile(#InFile)
CloseFile(#OutFile)
If Mode = 1
packed_filename.s = OutFileName
res = OpenPack(packed_filename)
If res <> 0
*addr = NextPackFile()
size = PackFileSize()
File = OpenFile(#InFile, packed_filename + ".dec")
WriteData(*addr, size)
CloseFile(#InFile)
ClosePack()
Else
MessageRequester(#title,"Error!")
ProcedureReturn
EndIf
EndIf
EndIf
FreeMemory(-1)
EndIf
Else
Error = 1
EndIf
EndProcedure
This is the code I am using to call it:
Code: Select all
AES_Crypt_File(type.l,file$,file$,key$, 8192*2)