Ich hab mich mal daran versucht, die PureAES UserLibrary von Gnozal aus dem engl. Forum in ein Modul
zu verfrachten, da seit PB 5.0 keine Updates mehr von ihm kamen.
Es ist natürlich nicht sein Originaler Code, und es ist mein erster Gehversuch in Sachen AES...
Momentan besteht dieses Modul aus 2 Funktionen.
AES_Encrypt() verschlüsselt einen Speicherblock per AES 256 und
AES_Decrypt() entschlüsselt ihn wieder.
Verbesserungsvorschläge und/oder Kritik ? Immer her damit

Eventuell erweitere ich das ganze noch ... (oder wir alle zusammen!)
- V0.002
- Der MD5 - "EncryptionKey" - auf ASCII festgesetzt
- Die Berechnung des EncryptionKeys in Prozedur ausgelagert
Code: Alles auswählen
DeclareModule cmCipher
;:- common module Cipher
;:--------------------------------------------------------------------------
;:- Author : George Bisonte
;:- Date : 20. February 2014
;:- PB : 5.21
;:- OS : Windows/Linux/MacOS
;:- Use it at your own risk
;:--------------------------------------------------------------------------
;:- V0.002 : EncryptionKey fixed to ASCII
;:- V0.001 : Try to replace the PureAES Userlibrary
;:--------------------------------------------------------------------------
; Original PureAES (by gnozal) Syntax (compatibility)
Declare PureAES_Encrypt(*Buffer, Dummy, EncryptionKey.s)
Declare PureAES_Decrypt(*Buffer, Dummy, EncryptionKey.s)
; Needed Syntax
Declare AES_Encrypt(*Buffer, EncryptionKey.s)
Declare AES_Decrypt(*Buffer, EncryptionKey.s)
EndDeclareModule
Module cmCipher
;:--------------------------------------------------------------------------
;:- Author : George Bisonte
;:- Date : 20. February 2014
;:- PB : 5.21 x86/x64
;:- OS : Windows/Linux/MacOS
;:- Use it at your own risk
;:--------------------------------------------------------------------------
EnableExplicit
Procedure AES_AsciiKey(EncryptionKey.s)
Protected *Key = AllocateMemory(32)
If Not *Key : ProcedureReturn #False : EndIf
EncryptionKey = MD5Fingerprint(@EncryptionKey, StringByteLength(EncryptionKey, #PB_Ascii))
PokeS(*Key, EncryptionKey, 32, #PB_Ascii)
ProcedureReturn *Key
EndProcedure
Procedure AES_Encrypt(*Buffer, EncryptionKey.s)
Protected Result = #False
Protected *IVector, *Input, *Output, Size
Protected *Key = AES_AsciiKey(EncryptionKey)
If *Key
OpenCryptRandom()
If *Buffer
Size = MemorySize(*Buffer)
If Size < 32 : Size = 32 : EndIf
*Input = AllocateMemory(Size)
If *Input
CopyMemory(*Buffer, *Input, Size)
*IVector = AllocateMemory(32)
If *IVector
CryptRandomData(*IVector, 32)
*Output = AllocateMemory(Size + 32)
If *Output
CopyMemory(*IVector, *Output, 32)
If AESEncoder(*Input, *Output + 32, Size, *Key, 256, *IVector, #PB_Cipher_CBC)
Result = *Output
Else
FreeMemory(*Output)
EndIf
EndIf
FreeMemory(*IVector)
EndIf
FreeMemory(*Input)
EndIf
EndIf
CloseCryptRandom()
FreeMemory(*Key)
EndIf
ProcedureReturn Result
EndProcedure
Procedure AES_Decrypt(*Buffer, EncryptionKey.s)
Protected Result = #False
Protected Size, *IVector, *Output
Protected *Key = AES_AsciiKey(EncryptionKey)
If *Key
If *Buffer
Size = MemorySize(*Buffer)
*IVector = AllocateMemory(32)
If *IVector
CopyMemory(*Buffer, *IVector, 32)
*Output = AllocateMemory(Size - 32)
If *Output
If AESDecoder(*Buffer + 32, *Output, Size - 32, *Key, 256, *IVector, #PB_Cipher_CBC )
Result = *Output
Else
FreeMemory(*Output)
EndIf
EndIf
FreeMemory(*IVector)
EndIf
EndIf
FreeMemory(*Key)
EndIf
ProcedureReturn Result
EndProcedure
Procedure PureAES_Encrypt(*Buffer, Dummy, EncryptionKey.s)
ProcedureReturn AES_Encrypt(*Buffer, EncryptionKey)
EndProcedure
Procedure PureAES_Decrypt(*Buffer, Dummy, EncryptionKey.s)
ProcedureReturn AES_Decrypt(*Buffer, EncryptionKey)
EndProcedure
EndModule
CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
Define InFile.s, OutFile.s
Define AESKey.s = "MyKey" ; <- The Key to encode and decode the data
Procedure AESCrypt(SourceFile.s, DestinationFile.s, Key.s, Mode = #False)
Protected fHnd, *File, *Memory, Result = #False
fHnd = ReadFile(#PB_Any, SourceFile) ; <- Read
If fHnd
*File = AllocateMemory(Lof(fHnd))
If *File
ReadData(fHnd, *File, Lof(fHnd))
EndIf
CloseFile(fHnd)
EndIf
If *File
If Mode = #False
*Memory = cmCipher::AES_Encrypt(*File, Key)
Else
*Memory = cmCipher::AES_Decrypt(*File, Key)
EndIf
FreeMemory(*File)
EndIf
If *Memory
fHnd = CreateFile(#PB_Any, DestinationFile)
If fHnd
WriteData(fHnd, *Memory, MemorySize(*Memory))
CloseFile(fHnd)
Result = #True
EndIf
FreeMemory(*Memory)
EndIf
ProcedureReturn Result
EndProcedure
InFile = OpenFileRequester("Encode : Select an inputfile", "", "", 0)
If InFile <> ""
OutFile = OpenFileRequester("Encode : Select an outputfile", "", "", 0)
If OutFile <> ""
AESCrypt(InFile, OutFile, AESKey, 0)
EndIf
EndIf
InFile = OpenFileRequester("Decode : Select an inputfile", "", "", 0)
If InFile <> ""
OutFile = OpenFileRequester("Decode : Select an outputfile", "", "", 0)
If OutFile <> ""
AESCrypt(InFile, OutFile, AESKey, 1)
EndIf
EndIf
CompilerEndIf