Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
FieldView encryption challenge
#32
man neos is just gonna annihilate mine, i know, but this one is pretty fast. ive heard of julius caesar b4.. this is like.. dynamic julius caesar ;) ive toyed with it for a while but i could never get the password to modify the data like i was able to tonight. here, its pretty fast, i included a 1meg file to test it with.

Code:
'''' This example uses some functions to transfer data
'''' between a file and memory and to en/decrypt that data.
'''' The Subs "encrypt_file" and "decrypt_file" demonstrate
'''' how to use those functions to encrypt and decrypt an
'''' entire file to another file with a new name.
''''
'''' Added a new wrapper to match compo specs.




#Include "crt.bi"

Option Explicit



Type passtype

  l As Integer
  a As Integer
  a2 As Integer

  s As String '' <---- this is where the actual password is in memory

End Type



'''  internal subs / functions
Declare Function str2buf ( msg As String ) As uByte Ptr
Declare Function buf2str ( buffer As uByte Ptr ) As String
Declare Function fil2buf ( file_name As String ) As uByte Ptr
Declare Function decrypt_buffer ( buffer As uByte Ptr, pass As passtype ) As uByte Ptr
Declare Sub buf2fil ( file_name As String, buffer As uByte Ptr )
Declare Function encrypt_buffer ( buffer As uByte Ptr, pass As passtype ) As uByte Ptr
Declare Sub setup_pass ( pass As passtype, flen As Integer Ptr )


''   user subs
Declare Function decrypt_message ( msg As String, pass As String ) As String
Declare Function encrypt_message ( msg As String, pass As String ) As String
Declare Sub decrypt_file ( src As String, dest As String, pass As passtype )
Declare Sub encrypt_file ( src As String, dest As String, pass As passtype )



Dim As String msg, msg2

msg = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"

Dim As Double t, p
t = Timer


For p = 0 To 19999
msg2 = encrypt_message ( msg, "heheh" )
Next

? Timer - t ; " seconds to do 20000"
? (Timer - t) / 20000; " seconds to do one"

Sleep


? msg2
?
Sleep
? decrypt_message ( msg2, "ohhcrap" ) '' wrong pass!!!
?
Sleep
? decrypt_message ( msg2, "mfgahhhhhhhhhhhhhthispassiscrazy" ) '' wrong pass!!!
?
Sleep
? decrypt_message ( msg2, "heheh" ) '' there we go!!!
?
Sleep



End





Function fil2buf ( file_name As String ) As uByte Ptr

  'read file into buffer

  Dim buffer As uByte Ptr
  Dim As Integer o = FreeFile

   Open file_name For Binary As o

     Dim As Integer al = Lof ( o )
     Dim As Integer Ptr fln
     Dim buf ( al - 1 ) As uByte
  
     buffer = CAllocate ( al + 4 )
  
     Get #o, , buf()

   Close

   fln = buffer
   fln[0] = al

   memcpy ( buffer + 4, @buf (0), al )

   Return buffer


End Function

Function encrypt_buffer ( buffer As uByte Ptr, pass As passtype ) As uByte Ptr

  'encrypt buffer

  Dim buffer2 As uByte Ptr
  Dim As Integer Ptr fln , fln2

   fln = buffer

   buffer2 = CAllocate ( fln[0] + 4 )

   fln2 = buffer2
   fln2[0] = fln[0]

   Dim As Integer enc, c

   Do
    
    
     For enc = 0 To pass.a
       buffer2[c + 4] = buffer[c + 4] + ( enc * pass.a2 ) Mod 256

       c + = 1
       If c = fln[0] Then Return buffer2
    
     Next
    
   Loop

End Function


Sub buf2fil ( file_name As String, buffer As uByte Ptr )

  'send buffer to file

  Dim As Integer o = FreeFile
  Dim As Integer Ptr lfn = buffer

   Open file_name For Binary As o
  
     Dim As Integer al = lfn[0]
     Dim buf ( al - 1 ) As uByte

     memcpy ( @buf(0), buffer + 4, al )
  
     Put #o, , buf()

   Close


End Sub


Function decrypt_buffer ( buffer As uByte Ptr, pass As passtype ) As uByte Ptr


  Dim buffer2 As uByte Ptr
  Dim As Integer Ptr fln, fln2

   fln = buffer

   buffer2 = CAllocate ( fln[0] + 4)

   fln2 = buffer2
   fln2[0] = fln[0]
  
   Dim As Integer enc, c
  
   Do
  
     For enc = 0 To pass.a
       buffer2[c + 4] = buffer[c + 4] - (enc * pass.a2) Mod 256
    
       c + = 1
       If c = fln[0] Then Return buffer2
  
     Next
    
   Loop


End Function


Sub encrypt_file ( src As String, dest As String, pass As passtype )


  Dim As uByte Ptr file_handle, buffer_handle
  Dim As Integer Ptr flen

   file_handle = fil2buf ( src )
   flen = file_handle

   setup_pass pass, flen

   buffer_handle = encrypt_buffer ( file_handle, pass )
   buf2fil dest, buffer_handle


End Sub


Function encrypt_message ( msg As String, pwd As String ) As String

  Dim pass As passtype
  Dim As uByte Ptr msg_handle, buffer_handle
  Dim As Integer Ptr flen
  Dim As String nwmsg

   msg_handle = str2buf ( msg )
   flen = msg_handle

   pass.s = pwd
   setup_pass pass, flen

   buffer_handle = encrypt_buffer ( msg_handle, pass )

   Return buf2str ( buffer_handle )


End Function


Function decrypt_message ( msg As String, pwd As String ) As String

  Dim pass As passtype
  Dim As uByte Ptr msg_handle, buffer_handle
  Dim As Integer Ptr flen
  Dim As String nwmsg

   msg_handle = str2buf ( msg )
   flen = msg_handle

   pass.s = pwd
   setup_pass pass, flen

   buffer_handle = decrypt_buffer ( msg_handle, pass )

   Return buf2str ( buffer_handle )


End Function


Sub decrypt_file ( src As String, dest As String, pass As passtype )




  Dim As uByte Ptr file_handle, buffer_handle
  Dim As Integer Ptr flen

   file_handle = fil2buf ( src )
   flen = file_handle

   setup_pass pass, flen
    
   buffer_handle = decrypt_buffer ( file_handle, pass )
   buf2fil dest, buffer_handle


End Sub


Sub setup_pass ( pass As passtype, flen As Integer Ptr )


  pass.l = Len(pass.s)

  Dim As Integer avg, ac

   For avg = 0 To Len(pass.s) - 1
     ac += pass.s[avg]

   Next

   ac = ac \ pass.l

   pass.a =  flen[0] Mod ac
   pass.a2 = pass.l - (pass.a + ((pass.s[0] + pass.s[Len(pass) - 1]) \ 2 ))


End Sub


Function str2buf ( msg As String ) As uByte Ptr

  'read string into buffer

  Dim As Integer al = Len ( msg )
  Dim As Integer Ptr fln
  Dim buf ( al - 1 ) As uByte
  Dim buffer As uByte Ptr
  
   buffer = CAllocate ( al + 4 )
  
   memcpy ( buffer + 4, @msg [0], al )
  
   fln = buffer
   fln[0] = al


  Return buffer


End Function


Function buf2str ( buffer As uByte Ptr ) As String


  Dim msg As String
  Dim As Integer Ptr fln = buffer
  Dim As Integer al = fln[0]

   msg = space ( al )
   memcpy ( @msg [0], buffer + 4 , al )
  


  Return msg


End Function

edit: forgot, the files at http://members.aol.com/rubentbstk/cryp.rar

edit2: made it a lil more modular

edit3: just now read the actual format you wanted the functions in, here. wrote a wrapper for my functions to use this input

edit4: edited for the last time hahahah.... just added speed testing, this is what i get:


Quote: 0.6186107712155975 seconds to do 20000
3.099456913454075e-005 seconds to do one
.00003099456913454075 seconds for 200 bytes :D
Reply


Messages In This Thread
FieldView encryption challenge - by Z!re - 08-13-2005, 07:54 PM
FieldView encryption challenge - by KiZ - 08-16-2005, 05:54 AM
FieldView encryption challenge - by barok - 08-16-2005, 09:29 AM
FieldView encryption challenge - by dumbledore - 08-16-2005, 09:46 AM
FieldView encryption challenge - by KiZ - 08-16-2005, 10:07 AM
FieldView encryption challenge - by anarky - 08-16-2005, 11:59 AM
FieldView encryption challenge - by barok - 08-16-2005, 12:36 PM
FieldView encryption challenge - by anarky - 08-16-2005, 12:44 PM
FieldView encryption challenge - by dumbledore - 08-16-2005, 01:25 PM
FieldView encryption challenge - by Z!re - 08-16-2005, 06:18 PM
FieldView encryption challenge - by barok - 08-16-2005, 06:56 PM
FieldView encryption challenge - by Rattrapmax6 - 08-16-2005, 07:32 PM
FieldView encryption challenge - by anarky - 08-16-2005, 07:34 PM
FieldView encryption challenge - by Rattrapmax6 - 08-16-2005, 07:39 PM
FieldView encryption challenge - by Neo - 08-16-2005, 08:06 PM
FieldView encryption challenge - by dumbledore - 08-16-2005, 08:15 PM
FieldView encryption challenge - by Z!re - 08-16-2005, 10:23 PM
FieldView encryption challenge - by Neo - 08-16-2005, 10:32 PM
FieldView encryption challenge - by dumbledore - 08-16-2005, 11:54 PM
FieldView encryption challenge - by Neo - 08-17-2005, 12:10 AM
FieldView encryption challenge - by Rattrapmax6 - 08-17-2005, 12:37 AM
FieldView encryption challenge - by dumbledore - 08-17-2005, 06:28 AM
FieldView encryption challenge - by Rattrapmax6 - 08-17-2005, 06:56 AM
FieldView encryption challenge - by KiZ - 08-17-2005, 11:08 AM
FieldView encryption challenge - by Z!re - 08-17-2005, 12:05 PM
FieldView encryption challenge - by dumbledore - 08-17-2005, 01:32 PM
FieldView encryption challenge - by Neo - 08-17-2005, 01:57 PM
FieldView encryption challenge - by Anonymous - 08-17-2005, 04:22 PM
FieldView encryption challenge - by Z!re - 08-17-2005, 04:35 PM
FieldView encryption challenge - by Rattrapmax6 - 08-17-2005, 06:07 PM
FieldView encryption challenge - by Neo - 08-17-2005, 06:31 PM
FieldView encryption challenge - by Neo - 08-18-2005, 02:54 AM
FieldView encryption challenge - by Rattrapmax6 - 08-18-2005, 05:49 AM
FieldView encryption challenge - by Moneo - 08-18-2005, 05:57 AM
FieldView encryption challenge - by dumbledore - 08-19-2005, 02:39 AM
FieldView encryption challenge - by Z!re - 08-19-2005, 04:11 AM
FieldView encryption challenge - by Rattrapmax6 - 08-19-2005, 04:41 AM
FieldView encryption challenge - by Mr Match - 08-19-2005, 05:40 AM
FieldView encryption challenge - by Rattrapmax6 - 08-19-2005, 05:48 AM
FieldView encryption challenge - by Mr Match - 08-19-2005, 06:00 AM
FieldView encryption challenge - by Anonymous - 08-19-2005, 07:37 AM
FieldView encryption challenge - by KiZ - 08-19-2005, 11:50 AM
FieldView encryption challenge - by Z!re - 08-19-2005, 03:07 PM
FieldView encryption challenge - by Anonymous - 08-19-2005, 04:48 PM
FieldView encryption challenge - by anarky - 08-19-2005, 04:50 PM
FieldView encryption challenge - by anarky - 08-19-2005, 04:56 PM
FieldView encryption challenge - by Z!re - 08-19-2005, 05:03 PM
FieldView encryption challenge - by red_Marvin - 08-19-2005, 05:53 PM
FieldView encryption challenge - by Neo - 08-19-2005, 06:41 PM
FieldView encryption challenge - by Mr Match - 08-19-2005, 07:30 PM
FieldView encryption challenge - by Z!re - 08-19-2005, 07:36 PM
FieldView encryption challenge - by Rattrapmax6 - 08-19-2005, 08:27 PM
FieldView encryption challenge - by Z!re - 08-19-2005, 10:17 PM
FieldView encryption challenge - by Neo - 08-19-2005, 11:23 PM
FieldView encryption challenge - by Anonymous - 08-19-2005, 11:48 PM
FieldView encryption challenge - by Neo - 08-20-2005, 12:00 AM
FieldView encryption challenge - by Anonymous - 08-20-2005, 12:04 AM
FieldView encryption challenge - by Moneo - 08-20-2005, 06:19 AM
FieldView encryption challenge - by dumbledore - 08-20-2005, 06:42 AM
FieldView encryption challenge - by Neo - 08-20-2005, 12:41 PM
FieldView encryption challenge - by Neo - 08-20-2005, 01:08 PM
FieldView encryption challenge - by dumbledore - 08-20-2005, 10:06 PM
FieldView encryption challenge - by Neo - 08-21-2005, 01:03 AM
FieldView encryption challenge - by Dr_Davenstein - 08-21-2005, 02:17 AM
FieldView encryption challenge - by Neo - 08-21-2005, 02:18 AM
FieldView encryption challenge - by Neo - 08-21-2005, 02:20 AM
FieldView encryption challenge - by Dr_Davenstein - 08-21-2005, 03:31 AM
FieldView encryption challenge - by Neo - 08-21-2005, 05:52 AM
FieldView encryption challenge - by Moneo - 08-21-2005, 06:08 AM
FieldView encryption challenge - by KiZ - 08-21-2005, 08:14 AM
FieldView encryption challenge - by Z!re - 08-21-2005, 01:00 PM
FieldView encryption challenge - by Anonymous - 08-21-2005, 01:09 PM
FieldView encryption challenge - by Neo - 08-21-2005, 03:11 PM
FieldView encryption challenge - by Anonymous - 08-22-2005, 02:51 AM
FieldView encryption challenge - by Dr_Davenstein - 08-22-2005, 02:54 AM
FieldView encryption challenge - by Neo - 08-22-2005, 04:05 AM
FieldView encryption challenge - by Z!re - 08-22-2005, 04:25 AM
FieldView encryption challenge - by KiZ - 08-22-2005, 09:34 AM
FieldView encryption challenge - by Anonymous - 08-22-2005, 11:25 AM
FieldView encryption challenge - by Z!re - 08-22-2005, 03:07 PM
FieldView encryption challenge - by dumbledore - 08-23-2005, 09:47 AM
FieldView encryption challenge - by red_Marvin - 08-26-2005, 01:42 AM
FieldView encryption challenge - by red_Marvin - 08-27-2005, 01:51 AM
FieldView encryption challenge - by Deleter - 08-27-2005, 02:35 AM
FieldView encryption challenge - by Anonymous - 08-27-2005, 02:55 AM
FieldView encryption challenge - by Neo - 08-27-2005, 03:29 AM
FieldView encryption challenge - by Deleter - 08-27-2005, 09:49 AM
FieldView encryption challenge - by Anonymous - 08-27-2005, 12:54 PM
FieldView encryption challenge - by dumbledore - 08-28-2005, 03:44 AM
FieldView encryption challenge - by Z!re - 09-14-2005, 11:32 AM
FieldView encryption challenge - by Dr_Davenstein - 09-15-2005, 06:30 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)