Sec :
About the compresssion, since my prog sends files via the network, it is very usefull, nice to see a built-in packer in PB.
About your AESV3, I have some web space, send me the file !

Again, what are the modifications ?
And about SHA... can't wait for it !
EDIT : the AES ecnryption file updated :
Code: Select all
;http://www.host4scripts.de/pub/AESLIB.zip ; use just _1_ of the libs
;to make life easier, FileReadBufferSize needs to be a multiplier of 16
;Key is meant hexademical. A common way to generate a 16 byte key out of a password is to use MD5Fingerprint
;Procedure by PB / english forum
Procedure.l hex2dec(h$)
h$=UCase(h$)
For r=1 To Len(h$)
d<<4 : a$=Mid(h$,r,1)
If Asc(a$)>60
d+Asc(a$)-55
Else
d+Asc(a$)-48
EndIf
Next
ProcedureReturn d
EndProcedure
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
; 9 = MAX compression
res = AddPackFile(InFilename , 9)
ClosePack()
Else
Debug "error while packing"
ProcedureReturn
EndIf
InFilename = packed_filename
EndIf
FileIn=OpenFile(#InFile,InFilename.s)
If FileIn<>0
;Key transformation
*KeyBuffer = AllocateMemory(32)
key = key + key ; key is a MD5 128 bits key, we need 256 bits for AES 256, it's a workaround until we have SHA-256
Debug "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
; get data from input file
UseFile(1)
FileSeek(Offset*FileReadBufferSize)
Result=ReadData(*InputBuffer,BlockSize)
;Pad (make multiple of 16bytes) if needed
If BlockSize<>FileReadBufferSize And Mode = 0
Debug "Pad!"
PadBytes = (FileReadBufferSize - BlockSize) % 16
Debug "Need to pad with "+Str(PadBytes)+" bytes"
For i=0 To PadBytes-1
;Using pad method RFC2630
PokeB(*InputBuffer+BlockSize+i,PadBytes)
Next i
BlockSize = BlockSize + PadBytes
EndIf
; write data to outputfile
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)
;Strip padded bytes
If Mode=1 And ((Offset = BlocksToRead) Or (FileLength % FileReadBufferSize = 0))
Debug "Unpad!"
PaddedBytes=PeekB(*OutputBuffer+BlockSize-1) & $FF
Debug "Supposed # of padded bytes: "+Str(PaddedBytes)
For i=1 To PaddedBytes
If PeekB(*OutputBuffer+BlockSize-i)<>PaddedBytes
Debug "False alarm. No unpadding needed."
Break
EndIf
Next i
If i>PaddedBytes And PaddedBytes<16
BlockSize=BlockSize-PaddedBytes
Debug "Decreasing Block by "+Str(PaddedBytes)+" to get rid of padded bytes"
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
Debug "error while unpacking"
ProcedureReturn
EndIf
EndIf
EndIf
FreeMemory(-1)
EndIf
Else
Error = 1
EndIf
EndProcedure
start = GetTickCount_()
key.s = MD5Fingerprint("toto", 4)
InFile.s = "c:\alg.exe"
EncryptFile.s = "c:\alg.enc"
DecryptFile.s = "c:\alg.dec.exe"
AES_Crypt_File(0, InFile, EncryptFile, key, 8192*2)
AES_Crypt_File(1, EncryptFile, DecryptFile, key, 8192*2)
DeleteFile(DecryptFile)
RenameFile(DecryptFile + ".dec", DecryptFile)
Debug "Milliseconds: "+Str(GetTickCount_()-start)
If MD5FileFingerprint(InFile) = MD5FileFingerprint(DecryptFile)
Debug "ok"
Else
Debug "error"
EndIf
I fill the key in the good way know, and packing+encryption/decryption+unpacking is working fine
