question about encoding/decoding a text file

Just starting out? Need help? Post your questions and find answers here.
Lonely_Star77
User
User
Posts: 26
Joined: Sat Feb 20, 2021 3:39 pm

question about encoding/decoding a text file

Post by Lonely_Star77 »

hello.

today i didn't find something interesting to code... then i looked into PB help docs and found out about "ciphers" lib in PB so i used the example in AES-ENCODER to write this simple code that runs on console... you enter a sentence and the code encrypt and decrypt it with AES - i was wondering if it would be possible to code a program that reads a whole text file content and then encrypt it and overwrite it back to that text file making it completely encrypted then with same program you can do the opposite and read the whole encrypted text file and decrypt it and overwrite it with the decrypted text? i don't know who to read/write text files yet in FB and i'm sure someone has already written a program that does what i just described...

anyway here is my little code demo:

Code: Select all

Define.s plain_text, code

Procedure aes_encoder(plain_text.s)
     
  StringMemorySize = StringByteLength(plain_text) + SizeOf(Character) ; Space for the string and its null terminating character
  *CipheredString = AllocateMemory(StringMemorySize)   
  *DecipheredString = AllocateMemory(StringMemorySize) 
  
  If AESEncoder(@plain_text, *CipheredString, StringByteLength(plain_text), ?Key, 128, ?InitializationVector)
    Print("Ciphered: "+PeekS(*CipheredString)) ; warning, it will stop on the first null byte, only for demo purpose
    
    AESDecoder(*CipheredString, *DecipheredString, StringByteLength(plain_text), ?Key, 128, ?InitializationVector)
    Print("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
EndProcedure

OpenConsole()
Print("please enter text to be encoded: ")
plain_text = Input()
aes_encoder(plain_text)

Input()

CloseConsole()
Lonely_Star77
User
User
Posts: 26
Joined: Sat Feb 20, 2021 3:39 pm

Re: question about encoding/decoding a text file

Post by Lonely_Star77 »

well i did my best...

i wrote a code that reads a text file into a string variable and encode it then write it to another text file as encrypted and it worked i think i got an encrypted text in the file

then i decided to write another code to read the encoded text file and decode it and write the result in another file this time with no luck i got an empty text file and i do not know why...

here is the test text file:
file test3.txt:

Code: Select all

this is a test text file To test decoding encoding text file

rrrrrbbbbbbb77777777
here is the encoding code:

Code: Select all

Define.s plain_text, text2

Procedure.s aes_encoder(plain_text.s)
     
  StringMemorySize = StringByteLength(plain_text) + SizeOf(Character) ; Space for the string and its null terminating character
  *CipheredString = AllocateMemory(StringMemorySize)   
  *DecipheredString = AllocateMemory(StringMemorySize) 
  
  If AESEncoder(@plain_text, *CipheredString, StringByteLength(plain_text), ?Key, 128, ?InitializationVector)
;     Print("Ciphered: "+PeekS(*CipheredString)) ; warning, it will stop on the first null byte, only for demo purpose
    
;     AESDecoder(*CipheredString, *DecipheredString, StringByteLength(plain_text), ?Key, 128, ?InitializationVector)
;     Print("Deciphered: "+PeekS(*DecipheredString))
  EndIf
  ProcedureReturn PeekS(*CipheredString)
  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
EndProcedure

Procedure.s FileToVar(File$)
  ReadFile(0, File$)
  While Not Eof(0)   
    VarString$ + ReadString(0) + #CRLF$
  Wend
  CloseFile(0)
  ProcedureReturn VarString$
EndProcedure

Procedure writetofile(file.s, text2.s)
  *MemoryID = AllocateMemory(1000)       ; allocating a memory block
  If *MemoryID
    PokeS(*MemoryID, text2)   ; we write a string into the memory block
  EndIf
  If CreateFile(0, file)
  WriteData(0, *MemoryID, SizeOf(Character)*Len(text2))          ; write the text from the memory block into the file
    CloseFile(0)                         ; close the previously opened file and so store the written data
  Else
    Debug "may not create the file!"
  EndIf
EndProcedure  

plain_text = FileToVar("test3.txt")

text2 = aes_encoder(plain_text)

writetofile("test4.txt", text2)
here is the result
file test4.txt:

Code: Select all

¢ו(_־€@E—גְֱ¬D<uŠט ¥•qֹֽ£d"Žׂף(x;o%ף^‚Œ[*a<VDZ½פƒ]™םy6‎A(׀כ]ֲ”U`Fו7₪j›¬u¾$ט*;[_רDY₪“ּײ׳ֳ<ר}׳CŠפלfסfb lק—Xm״ב.}Y/cY דe₪לPXJ·›jD[i¨qygֺָֻ¦>ƒJVqtoֱYםh_ץקr
here is the decoding code:

Code: Select all

Define.s plain_text, text2

Procedure.s aes_encoder(plain_text.s)
     
  StringMemorySize = StringByteLength(plain_text) + SizeOf(Character) ; Space for the string and its null terminating character
  *CipheredString = AllocateMemory(StringMemorySize)   
  *DecipheredString = AllocateMemory(StringMemorySize) 
  
;   If AESEncoder(@plain_text, *CipheredString, StringByteLength(plain_text), ?Key, 128, ?InitializationVector)
;     Print("Ciphered: "+PeekS(*CipheredString)) ; warning, it will stop on the first null byte, only for demo purpose
    
    If AESDecoder(*CipheredString, *DecipheredString, StringByteLength(plain_text), ?Key, 128, ?InitializationVector)
;     Print("Deciphered: "+PeekS(*DecipheredString))
  EndIf
  ProcedureReturn PeekS(*CipheredString)
  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
EndProcedure

Procedure.s FileToVar(File$)
  ReadFile(0, File$)
  While Not Eof(0)   
    VarString$ + ReadString(0) + #CRLF$
  Wend
  CloseFile(0)
  ProcedureReturn VarString$
EndProcedure

Procedure writetofile(file.s, text2.s)
  *MemoryID = AllocateMemory(1000)       ; allocating a memory block
  If *MemoryID
    PokeS(*MemoryID, text2)   ; we write a string into the memory block
  EndIf
  If CreateFile(0, file)
  WriteData(0, *MemoryID, SizeOf(Character)*Len(text2))          ; write the text from the memory block into the file
    CloseFile(0)                         ; close the previously opened file and so store the written data
  Else
    Debug "may not create the file!"
  EndIf
EndProcedure  

plain_text = FileToVar("test4.txt")

text2 = aes_encoder(plain_text)

writetofile("test5.txt", text2)
and the result is an empty test5.txt file...
i wonder why? and what am i doing wrong?
infratec
Always Here
Always Here
Posts: 7834
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: question about encoding/decoding a text file

Post by infratec »

Look at the help for ReadData().
There you will find a hint to a code example (at Lof())

Once the file is in the buffer, use the AESEncoder() and use
WriteData() to write it to a file.
infratec
Always Here
Always Here
Posts: 7834
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: question about encoding/decoding a text file

Post by infratec »

Code: Select all

EnableExplicit


Define Filename$, InFile, *InFile, *OutFile, OutFile


Filename$ = OpenFileRequester("Chhose a file for encryption", "", "ALL|*.*", 0)
If Filename$
  
  InFile = ReadFile(#PB_Any, Filename$)
  If InFile
    *InFile = AllocateMemory(Lof(InFile), #PB_Memory_NoClear)
    If *InFile
      
      If ReadData(InFile, *InFile, MemorySize(*InFile)) = MemorySize(*InFile)
        
        *OutFile = AllocateMemory(MemorySize(*InFile), #PB_Memory_NoClear)
        If *OutFile
          
          If AESEncoder(*InFile, *OutFile, MemorySize(*InFile), ?Key, 128, ?InitializationVector, #PB_Cipher_CBC)
            OutFile = CreateFile(#PB_Any, Filename$ + ".AES")
            If OutFile
              WriteData(OutFile, *OutFile, MemorySize(*OutFile))
              CloseFile(OutFile)
            EndIf
          EndIf
          FreeMemory(*OutFile)
        EndIf
        
      EndIf
      
      FreeMemory(*InFile)
    EndIf
    CloseFile(InFile)
  EndIf
  
EndIf


MessageRequester("Info", "File is encrypted")


Filename$ = Filename$ + ".AES"


InFile = ReadFile(#PB_Any, Filename$)
If InFile
  *InFile = AllocateMemory(Lof(InFile), #PB_Memory_NoClear)
  If *InFile
    
    If ReadData(InFile, *InFile, MemorySize(*InFile)) = MemorySize(*InFile)
      
      *OutFile = AllocateMemory(MemorySize(*InFile), #PB_Memory_NoClear)
      If *OutFile
        
        If AESDecoder(*InFile, *OutFile, MemorySize(*InFile), ?Key, 128, ?InitializationVector, #PB_Cipher_CBC)
          OutFile = CreateFile(#PB_Any, Filename$ + ".Normal")
          If OutFile
            WriteData(OutFile, *OutFile, MemorySize(*OutFile))
            CloseFile(OutFile)
          EndIf
        EndIf
        FreeMemory(*OutFile)
      EndIf
      
    EndIf
    
    FreeMemory(*InFile)
  EndIf
  CloseFile(InFile)
EndIf


MessageRequester("Info", "File is decrypted")


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

infratec
Always Here
Always Here
Posts: 7834
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: question about encoding/decoding a text file

Post by infratec »

Btw.:

If you want to read the file in one string, look at #PB_File_IgnoreEOL for the ReadString() command.
So you need no loop.
Lonely_Star77
User
User
Posts: 26
Joined: Sat Feb 20, 2021 3:39 pm

Re: question about encoding/decoding a text file

Post by Lonely_Star77 »

thank you very much :)
User avatar
NicTheQuick
Addict
Addict
Posts: 1584
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: question about encoding/decoding a text file

Post by NicTheQuick »

After encryption the resulting data usually looks like random garbage. And that's also the reason why you can not read that data again with ReadString or similar and store it as a string. The data can contain null bytes and other illegal characters that can not be represented as a valid string. If you work with binary data like this always use plain memory as you see in infratecs example code.

Also remember that the initialization vector and the key are stored in your executable without any further encryption. For a good encryption result you should generate a fresh initialization vector (IV) everytime and store it together with the output file. Use CryptRandomData() for a cryptographically secure IV. And use SHA2 oder SHA3 for generating a key from a password the user types in.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
TI-994A
Addict
Addict
Posts: 2791
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: question about encoding/decoding a text file

Post by TI-994A »

Here's another example to illustrate the fundamentals of file encryption/decryption:

Code: Select all

InitNetwork()

fileName.s = GetTemporaryDirectory() + "LoneRanger.txt"
If FileSize(fileName) < 1
  ReceiveHTTPFile("https://www.dropbox.com/s/oe3p2tpix8jfmow/LoneRanger.txt?dl=1", fileName)
EndIf

Procedure readEncryptedFile(fileName.s, key.s, vector.s)
 
  If ReadFile(0, fileName)
    fileDataSize = Lof(0)
    *fileData = AllocateMemory(fileDataSize)
    *decryptedData = AllocateMemory(fileDataSize)
    If ReadData(0, *fileData, fileDataSize)   
      AESDecoder(*fileData, *decryptedData, fileDataSize, @key, 128, @vector)
      AddGadgetItem(2, -1, "Decrypted File: (" + fileName + "): " +
                           #CRLF$ + #CRLF$ + PeekS(*decryptedData))
    EndIf
    CloseFile(0)
    FreeMemory(*fileData)
    FreeMemory(*decryptedData)
  EndIf
 
EndProcedure

Procedure writeEncryptedFile(fileContent.s, fileName.s, key.s, vector.s)
 
  If CreateFile(0, fileName)   
    fileContentSize = StringByteLength(fileContent)
    *encryptedData = AllocateMemory(fileContentSize)
    AESEncoder(@fileContent, *encryptedData, fileContentSize, @key, 128, @vector)
    AddGadgetItem(1, -1, "Encrypted File: (" + fileName + "): " +
                         #CRLF$ + #CRLF$ + PeekS(*encryptedData))
    WriteData(0, *encryptedData, fileContentSize)
    CloseFile(0)
    FreeMemory(*encryptedData)
  EndIf
 
EndProcedure

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, 0, 0, 600, 800, "File Encryption", wFlags)
EditorGadget(0, 10, 10, 580, 250, #PB_Editor_WordWrap)
EditorGadget(1, 10, 270, 580, 250, #PB_Editor_WordWrap)
EditorGadget(2, 10, 530, 580, 260, #PB_Editor_WordWrap)

If ReadFile(0, fileName)   
  fileDataSize = Lof(0)
  *fileData = AllocateMemory(fileDataSize)
 
  If ReadData(0, *fileData, fileDataSize)         
    fileContent.s = PeekS(*fileData, fileDataSize, #PB_UTF8)
    AddGadgetItem(0, -1, "Original File: (" + fileName + "): " +
                         #CRLF$ + #CRLF$ + fileContent)
  EndIf
  CloseFile(0)
 
  key.s = "A very secret key."
  vector.s = "A very secret vector."
  encryptedFileName.s = GetTemporaryDirectory() + "encryptedFile.aes"
 
  writeEncryptedFile(fileContent, encryptedFileName, key, vector)
  readEncryptedFile(encryptedFileName, key, vector)
  FreeMemory(*fileData)

EndIf

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Lonely_Star77
User
User
Posts: 26
Joined: Sat Feb 20, 2021 3:39 pm

Re: question about encoding/decoding a text file

Post by Lonely_Star77 »

hello i tried to create a password with a key generated and to generate a vector and to write the key in the encrypted file but the code is not working as i'm confused with var *var @var sizeof memorysize etc...

anyway here is the code:

Code: Select all

EnableExplicit


Define Filename$, InFile, *InFile, *OutFile, OutFile, pass.s, text.s, vector.s, *key, i, *text


OpenConsole()

UseSHA2Fingerprint()
Print("plase enter your password: ")
pass = Input()

vector = StringFingerprint(pass, #PB_Cipher_SHA2, 512) ; Use SHA2-512 variant

CloseConsole()



*Key = AllocateMemory(16)
  
  If OpenCryptRandom() And *Key
    CryptRandomData(*Key, 16)
    
    text = "Generated Key:"
    For i = 0 To 15
      text + " " + RSet(Hex(PeekB(*Key+i), #PB_Byte), 2, "0")
    Next i     
    
    CloseCryptRandom()
  EndIf
  

Filename$ = OpenFileRequester("Choose a file for encryption", "", "ALL|*.*", 0)
If Filename$
 
  InFile = ReadFile(#PB_Any, Filename$)
  If InFile
    *InFile = AllocateMemory(Lof(InFile), #PB_Memory_NoClear)
    If *InFile
     
      If ReadData(InFile, *InFile, MemorySize(*InFile)) = MemorySize(*InFile)
       
        *OutFile = AllocateMemory(MemorySize(*InFile), #PB_Memory_NoClear)
        If *OutFile
         
          If AESEncoder(*InFile, *OutFile, MemorySize(*InFile), @text, 128, @vector, #PB_Cipher_CBC)
            OutFile = CreateFile(#PB_Any, Filename$ + ".AES")
            If OutFile
              WriteData(OutFile, *OutFile, MemorySize(*OutFile))
              WriteData(OutFile, *text, SizeOf(character)*Len(text))
              CloseFile(OutFile)
            EndIf
          EndIf
          FreeMemory(*OutFile)
        EndIf
       
      EndIf
     
      FreeMemory(*InFile)
    EndIf
    CloseFile(InFile)
  EndIf
 
EndIf


MessageRequester("Info", "File is encrypted")


Filename$ = Filename$ + ".AES"


InFile = ReadFile(#PB_Any, Filename$)
If InFile
  *InFile = AllocateMemory(Lof(InFile), #PB_Memory_NoClear)
  If *InFile
   
    If ReadData(InFile, *InFile, MemorySize(*InFile)) = MemorySize(*InFile)
     
      *OutFile = AllocateMemory(MemorySize(*InFile), #PB_Memory_NoClear)
      If *OutFile
       
        If AESDecoder(*InFile, *OutFile, MemorySize(*InFile), @text, 128, @vector, #PB_Cipher_CBC)
          OutFile = CreateFile(#PB_Any, Filename$ + ".Normal")
          If OutFile
            WriteData(OutFile, *OutFile, MemorySize(*OutFile))
            CloseFile(OutFile)
          EndIf
        EndIf
        FreeMemory(*OutFile)
      EndIf
     
    EndIf
   
    FreeMemory(*InFile)
  EndIf
  CloseFile(InFile)
EndIf


MessageRequester("Info", "File is decrypted")


; 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
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: question about encoding/decoding a text file

Post by Saki »

Look here for key and IV generating and other :
viewtopic.php?f=12&t=76780

So :

Code: Select all

WriteData(OutFile, @text, SizeOf(character)*Len(text))
The asterisk * has no effect.
*test = test.i

#PB_Memory_NoClear -
should never be used with AES,
you can experience very nasty surprises,
it also brings no noticeable advantage.

You must also not append the key behind but the vector.

Also you have to note that it pops if the file is smaller than 16 bytes.
Last edited by Saki on Mon Mar 01, 2021 9:35 pm, edited 1 time in total.
地球上の平和
Lonely_Star77
User
User
Posts: 26
Joined: Sat Feb 20, 2021 3:39 pm

Re: question about encoding/decoding a text file

Post by Lonely_Star77 »

thank you @Saki for your replay :)

now the code works however it does not decrypt the file only encrypt :-/

p.s. - oh my mistake it does decrypt the file :) my mistake - i thank you all :)

here is the code:

Code: Select all

EnableExplicit


Define Filename$, InFile, *InFile, *OutFile, OutFile, pass.s, text.s, vector.s, *key, i


OpenConsole()

UseSHA2Fingerprint()
Print("plase enter your password: ")
pass = Input()

vector = StringFingerprint(pass, #PB_Cipher_SHA2, 512) ; Use SHA2-512 variant

CloseConsole()



*Key = AllocateMemory(16)
 
  If OpenCryptRandom() And *Key
    CryptRandomData(*Key, 16)
   
    text = "Generated Key:"
    For i = 0 To 15
      text + " " + RSet(Hex(PeekB(*Key+i), #PB_Byte), 2, "0")
    Next i     
   
    CloseCryptRandom()
  EndIf
 

Filename$ = OpenFileRequester("Choose a file for encryption", "", "ALL|*.*", 0)
If Filename$
 
  InFile = ReadFile(#PB_Any, Filename$)
  If InFile
    *InFile = AllocateMemory(Lof(InFile), #PB_Memory_NoClear)
    If *InFile
     
      If ReadData(InFile, *InFile, MemorySize(*InFile)) = MemorySize(*InFile)
       
        *OutFile = AllocateMemory(MemorySize(*InFile), #PB_Memory_NoClear)
        If *OutFile
         
          If AESEncoder(*InFile, *OutFile, MemorySize(*InFile), @text, 128, @vector, #PB_Cipher_CBC)
            OutFile = CreateFile(#PB_Any, Filename$ + ".AES")
            If OutFile
              WriteData(OutFile, *OutFile, MemorySize(*OutFile))
              WriteData(OutFile, @vector, SizeOf(character)*Len(vector))
              CloseFile(OutFile)
            EndIf
          EndIf
          FreeMemory(*OutFile)
        EndIf
       
      EndIf
     
      FreeMemory(*InFile)
    EndIf
    CloseFile(InFile)
  EndIf
 
EndIf


MessageRequester("Info", "File is encrypted")


Filename$ = Filename$ + ".AES"


InFile = ReadFile(#PB_Any, Filename$)
If InFile
  *InFile = AllocateMemory(Lof(InFile), #PB_Memory_NoClear)
  If *InFile
   
    If ReadData(InFile, *InFile, MemorySize(*InFile)) = MemorySize(*InFile)
     
      *OutFile = AllocateMemory(MemorySize(*InFile), #PB_Memory_NoClear)
      If *OutFile
       
        If AESDecoder(*InFile, *OutFile, MemorySize(*InFile), @text, 128, @vector, #PB_Cipher_CBC)
          OutFile = CreateFile(#PB_Any, Filename$ + ".Normal")
          If OutFile
            WriteData(OutFile, *OutFile, MemorySize(*OutFile))
            CloseFile(OutFile)
          EndIf
        EndIf
        FreeMemory(*OutFile)
      EndIf
     
    EndIf
   
    FreeMemory(*InFile)
  EndIf
  CloseFile(InFile)
EndIf


MessageRequester("Info", "File is decrypted")


; 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
Lonely_Star77
User
User
Posts: 26
Joined: Sat Feb 20, 2021 3:39 pm

Re: question about encoding/decoding a text file

Post by Lonely_Star77 »

hello...

i would really like to code a small program that will be able to encrypt/decrypt text files with a password the code above works yet still i do not know how to read separately the vector of encrypted file at the bottom from the rest of the encrypted text - maybe i should save the vector in a separate file?...
User avatar
NicTheQuick
Addict
Addict
Posts: 1584
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: question about encoding/decoding a text file

Post by NicTheQuick »

Just write the IV at the begin or end of the file. It does not need to be secured in any way. And before you decrypt the file, first read the IV and then the data you want to decrypt.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: question about encoding/decoding a text file

Post by Saki »

Hi,
You have already written the vector behind the file.
You must not generate the key with random, you can't get to it later.
You have to read and get the vector from the target file before decrypting it.

But without padding this is not possible.
Make a text file with less than 16 characters and try it with that, it doesn't work.
For strings, you can just make the buffer a little bigger.
Since strings are terminated with zeros, this is automatically truncated.

Code: Select all

*mem=AllocateMemory(100)
x$="Hello World"
CopyMemory(@x$, *mem, StringByteLength(x$))
Debug PeekS(*mem)
You can do the rounding as sample like this:

Code: Select all

x=165
If x%16 : x+16 : x>>4<<4 : EndIf
地球上の平和
Post Reply