Page 1 of 1

AES ENCRYPTION

Posted: Sun Aug 08, 2010 1:26 pm
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

Re: AES ENCRYPTION

Posted: Sun Aug 08, 2010 1:38 pm
by PB
Pad it with spaces for the extra chars, then remove that many when decrypting. ;)

Re: AES ENCRYPTION

Posted: Sun Aug 08, 2010 2:43 pm
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

Re: AES ENCRYPTION

Posted: Sun Aug 08, 2010 10:04 pm
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. :?

Re: AES ENCRYPTION

Posted: Sun Aug 08, 2010 10:11 pm
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.