It is currently Fri May 24, 2013 7:23 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Base64 encryption/decryption of a file
PostPosted: Sun Feb 19, 2012 3:40 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Mar 27, 2009 9:41 am
Posts: 333
Location: Athens, Greece
I have writen the following code to Base64 encode/decode a file.
Code:
;encode
handle.l = ReadFile(#PB_Any, <original file>);
Size.l=Lof(handle)
*FileData = AllocateMemory(Size)
ReadData(handle,*FileData,Size)
CloseFile(handle)

Size2.l= Size * 1.35
*NewFileData = AllocateMemory(Size2)
Base64Encoder(*FileData, Size, *NewFileData, Size2)

handle2.l = CreateFile(#PB_Any, <encrypted file>);
WriteData(handle2, *NewFileData, Size2)
CloseFile(handle2)
FreeMemory(*FileData)
FreeMemory(*NewFileData)

Code:
;decode
handle.l = ReadFile(#PB_Any, <encrypted file>)
Size=Lof(handle)
*FileData = AllocateMemory(Size)
ReadData(handle, *FileData, Size)
CloseFile(handle)

*Decoded = AllocateMemory(Size/1.35)
Bytes.l = Base64Decoder(*FileData, Size, *Decoded, Size)
Debug Bytes

handle2.l = CreateFile(#PB_Any,<original file>)
WriteData(handle2, *Decoded, MemorySize(*Decoded))
CloseFile(handle2)
FreeMemory(*Decoded)

Some files are encoded/decoded fine, some others not. Some times the code with some files runs fine and the new file has the same CRC with the original one. Some times with other different original files the new file has the last byte 00 than the original one. Some other times the code returns "Invalid memory access" to the last line. Can somebody tell me what am I doing wrong?

_________________
http://kc2000labs.shadowtavern.com/pb/pb_eng.htm


Top
 Profile  
 
 Post subject: Re: Base64 encryption/decryption of a file
PostPosted: Sun Feb 19, 2012 4:18 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 1575
Location: Berlin, Germany
Use the return values of Base64Encoder() and Base64Decoder() to get the exact size of the encoded/decoded data. Size of buffer <> size of data.
(BTW: If a built-in procedure has a return value, always use it !!)

_________________
Math problems?
Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x].


Top
 Profile  
 
 Post subject: Re: Base64 encryption/decryption of a file
PostPosted: Sun Feb 19, 2012 4:56 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1443
Location: Germany
Hi,

what LittleJohn means:
Code:
;encode
handle = ReadFile(#PB_Any, <original file>);
If handle
  Size = Lof(handle)
  *FileData = AllocateMemory(Size)
  If *FileData
    If ReadData(handle,*FileData,Size) = Size
      Size2 = Size * 1.35
      *NewFileData = AllocateMemory(Size2)
      If *NewFileData
        EncSize = Base64Encoder(*FileData, Size, *NewFileData, Size2)
        handle2 = CreateFile(#PB_Any, <encrypted file>)
        If handle2
          WriteData(handle2, *NewFileData, EncSize)
          CloseFile(handle2)
        EndIf
        FreeMemory(*NewFileData)
      EndIf
    EndIf
    FreeMemory(*FileData)
  EndIf
  CloseFile(handle)
EndIf
and
Code:
;decode
handle = ReadFile(#PB_Any, <encrypted file>)
If handle
  Size = Lof(handle)
  *FileData = AllocateMemory(Size)
  If *FileData
    If ReadData(handle, *FileData, Size) = Size
      *Decoded = AllocateMemory(Size)
      If *Decoded     
        Bytes = Base64Decoder(*FileData, Size, *Decoded, Size)
        Debug Bytes
        handle2 = CreateFile(#PB_Any,<original file>)
        If handle2
          WriteData(handle2, *Decoded, Bytes)
          CloseFile(handle2)
        EndIf
        FreeMemory(*Decoded)
      EndIf
    EndIf
    FreeMemory(*FileData)
  EndIf
  CloseFile(handle)
EndIf
Bernd


Top
 Profile  
 
 Post subject: Re: Base64 encryption/decryption of a file
PostPosted: Sun Feb 19, 2012 5:11 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 5:21 pm
Posts: 5188
Location: Germany
btw, base64 is not an encryption. It is just a translation that everybody can reverse.

_________________
Perl – The only language that looks the same before and after RSA encryption.
-- Keith Bostic


Top
 Profile  
 
 Post subject: Re: Base64 encryption/decryption of a file
PostPosted: Sun Feb 19, 2012 8:01 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Mar 27, 2009 9:41 am
Posts: 333
Location: Athens, Greece
@ infratec: can you tell me why Base64Decoder() in your code returns the number of bytes decoded but in my code doesn't?
The only thing that you did is to add some 'If' statements and check the return codes of the functions. We use the same parameters in Base64Decoder(). Why?
(I changed "*Decoded = AllocateMemory(Size/1.35)" to "*Decoded = AllocateMemory(Size)" and zero still comes back.)

@ freak: I know that it is a cheap way of translation but I want to use it within other types of encryption. I want the data to be a little transformed.

_________________
http://kc2000labs.shadowtavern.com/pb/pb_eng.htm


Top
 Profile  
 
 Post subject: Re: Base64 encryption/decryption of a file
PostPosted: Sun Feb 19, 2012 8:13 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Fri Mar 27, 2009 9:41 am
Posts: 333
Location: Athens, Greece
@ infratec: I found it. The problem was in encoding. I was saying:
Code:
WriteData(handle2, *NewFileData, Size2)
instead of:
Code:
WriteData(handle2, *NewFileData, EncSize)
So, more bytes, dummy, were writen to the encoded file.

_________________
http://kc2000labs.shadowtavern.com/pb/pb_eng.htm


Top
 Profile  
 
 Post subject: Re: Base64 encryption/decryption of a file
PostPosted: Sun Feb 19, 2012 9:48 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 1443
Location: Germany
:D


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: NabDa and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye