
For now I am using the v2 of Sec's lib, and I'm happy with it ^^
next point : 1 000 000 000 000 000 loops in 5ms

edit : still looking at the SHA functions family... I think it's far easier to do than AES ^^
Done that.newbie wrote:I think we will let you optimize your lib and test yourself
/* Description of what tables are tested:
The provided implementations each use a different set of tables
- Java implementation: uses no tables
- reference C implementation: uses Logtable, Alogtable, S, Si, rcon
- fast C implementation: uses Logtable, Alogtable, rcon
and additionally, T1, T2, T3, T4, T5, T6, T7, T8
and (for the inverse key schedule only) U1, U2, U3, U4.
All these tables are tested.
=========================
FILENAME: "ecb_tbl.txt"
Electronic Codebook (ECB) Mode
Tables Known Answer Tests
Algorithm Name: Rijndael
Principal Submitter: Joan Daemen
==========
KEYSIZE=128
I=1
KEY=00010203050607080A0B0C0D0F101112
PT=506812A45F08C889B97F5980038B8359
CT=D8F532538289EF7D06B506A4FD5BE9C9
Padding is no problem (from the idea), but I learnt that there are several methods used with AES and each of it has it pros and cons. Maybe best is to add all (most) padding methods so users can choose.newbie wrote:Hey Max,
file encryption was the next part I wanted to try myself, but I see you are already far, I'll let you do it so ^^
What is your problem with padding ?
Just encrypt data until the last block, and only pad the last block with $0 bytes to do a 128 bits block ? is there a problem ?
Apart of that, I announce that I done a small page to merge all of the known purebasic encryption/hash algorithm :
PB crypto ressource page :
http://perso.wanadoo.fr/jugesoftware/pu ... crypto.htm
P.S : I'm tired now, but I would like to play with files tomorrow, don't give up Max
EDIT : would be fine to use the same Lib to be "unified" and that for everyone could use your code. If it works, I will need it, and it would be annoying to convert Sec code to Max lib and Max code to Sec lib
EDIT 2 : if you do a nice file encryption/decryption code, I could add it to the pageand if you look carefully at it you will see that SHA is waiting for someone lol
Let me try it first my the Lib I useMax.² wrote:Dunno if you saw it, but I updated the file crypt procedure some posts ago.newbie wrote:Thanks Max.² for the correction about the page, it is done
This is the latest version I am using; since yesterday, no matter which file type and file size, it worked correctly. Only thing I changed was key handling, but that shouldn't have an effect on the result.newbie wrote:Let me try it first my the Lib I useMax.² wrote:Dunno if you saw it, but I updated the file crypt procedure some posts ago.newbie wrote:Thanks Max.² for the correction about the page, it is done
EDIT : I tested your code Max with your last Lib (from the link within the code) and it is not correct unfortunaly :-/
When encrypting a 3Ko executable, the final decrypted is 2.98Ko, and is not runable (Windows gives an error).
I just changed the target file, and input/output files, do I need to change anything else ?
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
FileIn=OpenFile(#InFile,InFilename.s)
If FileIn<>0
;Key transformation
*KeyBuffer = AllocateMemory(16)
j=-1
For i=1 To Len(Key.s) Step 2
j=j+1
PokeB(*KeyBuffer+j,Hex2Dec(Mid(Key,i,2)))
Next i
KeyLengthinBits = Len(Key)*4
AES_Gen_Tabs()
If Mode = 0
Ecx.s = Space(255)
AES_Encrypt_Key(*KeyBuffer, KeyLengthinBits, @Ecx)
ElseIf Mode = 1
Dcx.s = Space(255)
AES_Decrypt_Key(*KeyBuffer, KeyLengthinBits, @Dcx)
EndIf
*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
AES_Encrypt(*PlainBlock,*EncryptionBlock,@Ecx)
ElseIf Mode = 1
AES_Decrypt(*PlainBlock,*EncryptionBlock,@Dcx)
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)
EndIf
FreeMemory(-1)
EndIf
Else
Error = 1
EndIf
EndProcedure
start = GetTickCount_()
Key.s = "00010203050607080A0B0C0D0F101112"
InFile.s = "c:\_2.mpg"
EncryptFile.s = "c:\_Encrypt.enc"
DecryptFile.s = "c:\_Decrypt.exe"
AES_Crypt_File(0,InFile.s,EncryptFile.s,Key.s,8192*2)
AES_Crypt_File(1,EncryptFile.s,DecryptFile.s,Key.s,8192*2)
Debug "Milliseconds: "+Str(GetTickCount_()-start)
If MD5FileFingerprint(InFile) = MD5FileFingerprint(DecryptFile)
Debug "ok"
Else
Debug "error"
EndIf
Code: Select all
InFile.s = "c:\toto.exe"
EncryptFile.s = "c:\toto.enc"
DecryptFile.s = "c:\toto_dec.exe"
Probably still a padding problem; just updated the posting again. The lib is fine. I am currently encrypting/decrypting any file on my harddisk to find file situations which may cause troubles and fixing situation one by one.newbie wrote:Sorry but it still give me an error :-/
toto.exe = 3Ko (3072 bytes)Code: Select all
InFile.s = "c:\toto.exe" EncryptFile.s = "c:\toto.enc" DecryptFile.s = "c:\toto_dec.exe"
toto.enc = 3Ko (3072 bytes)
toto_dec.exe = 2.98Ko (3057 bytes) and not a valid Win32 executable
I have tried with C:\Windows\system32\alg.exe (that I moved to C:\ first) and same, the final executable file is smaller than the original file, so there is a problem in th decryption or unpadding procedure.
EDIT : i am using the Lib at this link :
http://www.host4scripts.de/pub/AESLIB.zip