AES ENCRYPTION

Just starting out? Need help? Post your questions and find answers here.
KIKI
Enthusiast
Enthusiast
Posts: 145
Joined: Thu Dec 28, 2006 11:49 am
Location: FRANCE

AES ENCRYPTION

Post by KIKI »

I want to encrypt a string whiche len is less than 16 character.
With the built-in library the encryption works only if the string is up to 16 character. How can i do for a string less than 16 characters

thanks in advance
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: AES ENCRYPTION

Post by PB »

Pad it with spaces for the extra chars, then remove that many when decrypting. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
KJ67
Enthusiast
Enthusiast
Posts: 218
Joined: Fri Jun 26, 2009 3:51 pm
Location: Westernmost tip of Norway

Re: AES ENCRYPTION

Post by KJ67 »

If you are just encoding a string, e.g. it is 0-terminated. You can just copy the string into a block of memory and where your sting fits with margin, the block then continues nicely with zeros. Reading it back will then also be very easy and it will be hard for somone to ues the length of the output (stored on disk?) to guess the original length.

Code: Select all

CompilerIf #PB_Compiler_Unicode
  CompilerError "Should be compiled in ascii mode"
CompilerEndIf

#CrBlock=768
Define *Memory = AllocateMemory(#CrBlock) 
Define *TPointer=*Memory
Define String$ = "Short"

If Len(String$)>=#CrBlock-1
  Debug "Buffer To small"
  CallDebugger
EndIf

CopyMemoryString(String$, @*TPointer)

Debug PeekS(*Memory)
*CipheredString   = AllocateMemory(#CrBlock)
*DecipheredString = AllocateMemory(#CrBlock) 

If AESEncoder(*Memory, *CipheredString, #CrBlock, ?Key, 128, ?InitializationVector)
  Debug "Ciphered: "+PeekS(*CipheredString)
  
  AESDecoder(*CipheredString, *DecipheredString, #CrBlock, ?Key, 128, ?InitializationVector)
  Debug "Deciphered: "+PeekS(*DecipheredString)
EndIf

DataSection
  Key:
  Data.b $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06
  
  InitializationVector:
  Data.b $3d, $af, $ba, $42, $9d, $9e, $b4, $30, $b4, $22, $da, $80, $2c, $9f, $ac, $41
EndDataSection
The best preparation for tomorrow is doing your best today.
Krisko
User
User
Posts: 22
Joined: Tue Jun 08, 2010 11:03 pm

Re: AES ENCRYPTION

Post by Krisko »

Ok I have a question If I encrypt some string and write it down in some txt file for example. After this how to decrypt it. When I try, I read the file and use the AESdecrypt but nothing's happen or the decrypted text is not the text that I've encrypted before this. :?
User avatar
KJ67
Enthusiast
Enthusiast
Posts: 218
Joined: Fri Jun 26, 2009 3:51 pm
Location: Westernmost tip of Norway

Re: AES ENCRYPTION

Post by KJ67 »

When you save the encrypted data, you better use WriteData() or if you really(!) want it in ascii, use Base64Encoder().
Try this, if not working post a snippet back.
The best preparation for tomorrow is doing your best today.
Post Reply