Page 1 of 1

AES Encryption [solved]

Posted: Fri Oct 21, 2011 2:15 pm
by Primus
Hi,
I'm tinkering with some encryption stuff that doesn't exactly work at the moment, and I just can't figure out what the problem is.

Here's what should happen: Program 1 should write an AES-encrypted file, Program 2 should decrypt that file.

Program 1:

Code: Select all

Structure Vector_struc
  Byte.a[32]
EndStructure

Filepath$="C:\test.file"
Password$="MyAwesomePassword"

a$=""
a$=a$+"I am a string "
a$=a$+"-------------------------------------------------------------------------------------------"
a$=a$+"-------------------------------------------------------------------------------------------"
a$=a$+"-------------------------------------------------------------------------------------------"
a$=a$+"-------------------------------------------------------------------------------------------"
String$=a$

*vector.Vector_struc
VectorSize=32
bit=256
*Vector= AllocateMemory(VectorSize)
RandomSeed(562642)
For i=0 To VectorSize-1
  *Vector\Byte[i] = Random(255)
Next

File=CreateFile(#PB_Any, Filepath$)
*AESEncoded = AllocateMemory(Len(String$))
AESEncoder(@String$,*AESEncoded,Len(String$),@Password$,Bit,*Vector)
WriteData(File,*AESEncoded,Len(String$))
CloseFile(File)
FreeMemory(*AESEncoded)
And Program 2:

Code: Select all


Structure Vector_struc
  Byte.a[32]
EndStructure

Filepath$="C:\test.file"
Password$="MyAwesomePassword"

*vector.Vector_struc
VectorSize=32
bit=256
*Vector= AllocateMemory(VectorSize)
RandomSeed(562642)
For i=0 To VectorSize-1
  *Vector\Byte[i] = Random(255)
Next

File=ReadFile(#PB_Any, Filepath$) 
Size=Lof(File)
*AESEncoded=AllocateMemory(Size)
ReadData(File,*AESEncoded,Size)
CloseFile(File)

*AESDecoded = AllocateMemory(Size)
AESDecoder(*AESEncoded,*AESDecoded,Size,@Password$,Bit,*Vector)
Result$ = PeekS(*AESDecoded,Size)

FreeMemory(*AESEncoded)
FreeMemory(*AESDecoded)

Debug Result$
Thank you for your support :wink:

Re: AES Encryption

Posted: Fri Oct 21, 2011 5:22 pm
by infratec
Hi,

if you use 256 bits, your key has to be 32bytes long. (Also written in the help)

So:

Code: Select all

Password$="MyAwesomePassword"
Password$ + Space(32 - Len(Password$))
(In both programs)
And it works.

Bernd

Re: AES Encryption

Posted: Fri Oct 21, 2011 5:37 pm
by Primus
Fantastic, thank you so much. I'm pretty sure this is the explaination for many disagreements I had with AES :D