Posted: Mon Aug 09, 2004 8:36 pm
Have to reply....newbie wrote:May be now someone can create an AES (Rindjael) code in PB ?
http://www.purebasic.com
https://www.purebasic.fr/english/
Have to reply....newbie wrote:May be now someone can create an AES (Rindjael) code in PB ?
AES128Decrypt(), AES128Encrypt()
AES192Decrypt(), AES192Encrypt()
AES256Decrypt(), AES256Encrypt()
Not I, I am pretty much 100% against DLL use for PB. That is why I pretty much ignore this thread, it talks about using a DLL in PB instead of a PB lib.newbie wrote:can't wait for it, and I'm sure Shannara too
Code: Select all
;Test from fips-197.pdf
;PLAINTEXT: 00112233445566778899aabbccddeeff
;KEY: 000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f
Procedure.s bytetohex(bb.l)
hexnum.s = "0123456789abcdef"
bb = bb & $FF
ProcedureReturn PeekS(@hexnum+ (bb>>4),1) + PeekS(@hexnum+ (bb & $0F),1)
EndProcedure
plain.s=Space(16)
PokeB(@plain,0)
For i = 1 To 15
PokeB(@plain+i, PeekB(@plain+i-1)+$11)
Next i
key.s = Space(32)
For i = 0 To 31
PokeB(@key+i,i)
Next i
AES256Encrypt(@plain,@key) ; notice: plain/cipher is at same place
Debug "#####ciphertext#####"
For i = 0 To 15
Debug bytetohex(PeekB(@plain+i))
Next i
AES256Decrypt(@plain,@key) ; notice: plain/cipher is at same place
Debug "#####plaintext#####"
For i = 0 To 15
Debug bytetohex(PeekB(@plain+i))
Next i
;other functions included
;AES128/192Encrypt()
;AES128/192Decrypt()Code: Select all
plain.s = "I am a fan of SEC now :-)"
key.s = MD5Fingerprint("toto", 4)
Debug "clear = " + plain
Debug "len(clear) = " + Str(Len(plain))
Debug "key = " + key
;-encryption
For a = 0 To Len(plain) Step 16
AES256Encrypt(@plain + a, @key)
Next a
Debug "encrypted = " + plain
;- decryption
For a = 0 To Len(plain) Step 16
AES256Decrypt(@plain + a, @key)
Next a
Debug "clear = " + plain
Code: Select all
FunctionName || Key Length || BlockSize
AES128* 128 bit 128 bit
AES192* 192 bit 128 bit
AES256* 256 bit 128 bit
-----
128 bit = 16 byte
192 bit = 24 byte
256 bit = 32 byteCode: Select all
plain.s = "I am a fan of SEC now :-) lololo"
key.s = MD5Fingerprint("toto", 4)
Debug "clear = " + plain
Debug "len(clear) = " + Str(Len(plain))
Debug "key = " + key
;-encryption
oldlen = Len(plain)
For a = 0 To oldlen Step 16
AES256Encrypt(@plain + a, @key)
Next a
Debug "encrypted = " + plain
;- decryption
For a = 0 To oldlen Step 16
AES256Decrypt(@plain + a, @key)
Next a
Debug "clear = " + plain
Code: Select all
Procedure AESEncrypt(*Buffer, *Key)
For a = 0 To Len(PeekS(*Buffer)) Step 16
AES256Encrypt(*Buffer + a, *Key)
Next a
ProcedureReturn *Buffer
EndProcedure
Procedure AESDecrypt(*Buffer, *Key)
For a = 0 To Len(PeekS(*Buffer)) Step 16
AES256Decrypt(*Buffer + a, *Key)
Next a
ProcedureReturn *Buffer
EndProcedure
plain.s = "I am a fan of SEC now :-) lololoLOL" ; our plain text
key.s = MD5Fingerprint("toto", 4) ; creating an AES key of 256 bits
Debug "clear = " + plain
Debug "len(clear) = " + Str(Len(plain))
Debug "key = " + key
length_required.l = Len(plain) ; length required for the buffer
Debug "length_required = " + Str(length_required)
*cypher_text = AllocateMemory(length_required) ; creating the buffer to the right input/output size
PokeS(*cypher_text, plain, length_required) ; copying our plain text into the buffer
Debug "LEN *cypher_text = " + Str(Len(PeekS(*cypher_text)))
;-encryption
*cypher_text = AESEncrypt(*cypher_text, @key) ; encrypting our plain text
Debug "encrypted = " + PeekS(*cypher_text, length_required)
Debug "LEN encrypted = " + Str(Len(PeekS(*cypher_text, length_required)))
;- decryption
;let's assume that this is a break in the code, like for instance an encrypted file that we did not encrypt
;or any encrypted data for which we do not know the size of the plain text
;*cypher_text would be @plain_text$ for instance
*plain_text = AESDecrypt(*cypher_text, @key) ; decrypting the cypher text
Debug "clear = " + PeekS(*plain_text);, length_required)
Debug "LEN clear = " + Str(Len(PeekS(*plain_text)))