Encryption dilemma

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Encryption dilemma

Post by doctorized »

I am making an app that will be installed on two or more computers and every app will send data to the others. The data are stored in files, most of plain text, and the data size will not be more than 1MB. (1024 bytes is the default but the user can set any number he wants.) I want to use a small encryption but I cannot decide which of the following two should I choose.

1) Use RAR (the console app) to make an archive password protected with the header encrypted too. "WinRAR offers you the benefit of industry strength archive encryption using AES with a key of 128 bits." as the developer says.
2) Write a code of mine that will use AES with two keys of 256 bits, the encryption key and the initialization vector, to encrypt every line of the file.

My question is this: which method is faster? I am interested in security but speed is very important too. Some milliseconds will make the difference for me. RAR will give me the opportunity to have one file at a time to send and I can wipe the file after compression but the second way some times will have two or three files to send and I must create a new file with the encrypted data.

Any advice is welcome.
PrincieD
Addict
Addict
Posts: 861
Joined: Wed Aug 10, 2005 2:08 pm
Location: Yorkshire, England
Contact:

Re: Encryption dilemma

Post by PrincieD »

Hi doctorized,

You should check out Ciphersaber 2, it's a very strong stream cypher and very fast :) http://ciphersaber.gurus.org/faq.html#cs2

These are my routines that I use:

Code: Select all

; max key length of 246 bytes
Procedure saber(mem, memLen, output, key, keyLen)
  
  If keyLen > 245
    keyLen = 245
  ElseIf keyLen < 0
    ProcedureReturn 0
  EndIf
  
  ; define state array, key array and 10 byte IV vector
  Dim s.w(255)
  Dim k.w(255)
  Dim V.w(9)
  
  ; setup index variables
  i.w = 0
  j.w = 0
  n.w = 0
  
  ; generate random 10 byte initialization vector (IV)
  RandomSeed(ElapsedMilliseconds())
  For i = 0 To 9
    V(i) = Random(255)
  Next
  
  ; put first 246 bytes of key into K array
  For i = 0 To keyLen
    k(i) = PeekB(key+i)&$FF
  Next
  
  ; add IV to end of user Key
  For i = 0 To 9
    k(keyLen+i+1) = V(i)
  Next
  keyLen = keyLen + 10
  
  ; set up state array
  For i = 0 To 255
    s(i) = i
  Next
  
  ; mix up the state array
  For n = 0 To 20 ; value of 20 is very secure
    For i = 0 To 255 
      j = (j + s(i) + k(i%keyLen))%255
      temp = s(i)
      s(i) = s(j)
      s(j) = temp
    Next
  Next

  ; write IV vector first to output
  For i = 0 To 9
    PokeB(output+i, V(i))
  Next
  output = output + 10
  
  ; ciphering operation
  j = 0
  For i = 0 To memLen
    j = (j + s(i))%255
    temp = s(i)
    s(i) = s(j)
    s(j) = temp
    PokeB(output+i, s((s(i)+s(j))%255)!PeekB(mem+i))
  Next
  
  ProcedureReturn memLen+10
  
EndProcedure

Procedure desaber(mem, memLen, output, key, keyLen)
  
  If keyLen > 245
    keyLen = 245
  ElseIf keyLen < 0
    ProcedureReturn 0
  EndIf
  
  ; define state array, key array and 10 byte IV vector
  Dim s.w(255)
  Dim k.w(255)
  Dim V.w(9)
  
  ; setup index variables
  i.w = 0
  j.w = 0
  n.w = 0
  
  ; grab 10 byte initialization vector (IV) from beggining of mem
  For i = 0 To 9
    V(i) = PeekB(mem+i)&$FF
  Next
  mem = mem + 10
  
  ; put first 246 bytes of key into K array
  For i = 0 To keyLen
    k(i) = PeekB(key+i)&$FF
  Next
  
  ; add IV to end of user Key
  For i = 0 To 9
    k(keyLen+i+1) = V(i)
  Next
  keyLen = keyLen + 10
  
  ; set up state array
  For i = 0 To 255
    s(i) = i
  Next
  
  ; mix up the state array
  For n = 0 To 20 ; value of 20 is very secure
    For i = 0 To 255 
      j = (j + s(i) + k(i%keyLen))%255
      temp = s(i)
      s(i) = s(j)
      s(j) = temp
    Next
  Next
  
  ; ciphering operation
  j = 0
  For i = 0 To memLen
    j = (j + s(i))%255
    temp = s(i)
    s(i) = s(j)
    s(j) = temp
    PokeB(output+i, s((s(i)+s(j))%255)!PeekB(mem+i))
  Next
  
EndProcedure
Chris.
ProGUI - Professional Graphical User Interface Library - http://www.progui.co.uk
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Encryption dilemma

Post by doctorized »

Thanx for your code but I have one that is a little smaller.

Code: Select all

;encode
a.l=ElapsedMilliseconds()
handle.l = ReadFile(#PB_Any, <original file>)
Size.l=Lof(handle)
*FileData = AllocateMemory(Size)
ReadData(handle,*FileData,Size)
CloseFile(handle)
*CipheredString = AllocateMemory(Size+1) ; Space for the string and null
Size2.l = Size *1.35
AESEncoder(*FileData, *CipheredString, Size, ?Key, 256, ?InitializationVector); 256bit encrytpion.
handle2.l = CreateFile(#PB_Any, <encrypted file>)
WriteData(handle2, *CipheredString, Size)
CloseFile(handle2)
FreeMemory(*CipheredString)
b.l= ElapsedMilliseconds()
Debug b-a

;decode
c.l= ElapsedMilliseconds()
handle.l = ReadFile(#PB_Any, <encrypted file>)
Size=Lof(handle)
*AESEncoded=AllocateMemory(Size)
ReadData(handle,*AESEncoded,Size)
CloseFile(handle)

*AESDecoded = AllocateMemory(Size)
AESDecoder(*AESEncoded,*AESDecoded,Size,?Key, 256, ?InitializationVector)
handle2.l = CreateFile(#PB_Any, <original file>)
WriteData(handle2, *AESDecoded,Size)
CloseFile(handle2)
FreeMemory(*AESEncoded)
FreeMemory(*AESDecoded)
d.l=ElapsedMilliseconds()
Debug d-c
	
	
DataSection; random data block
Key:
Data.b ..... 32 bytes

InitializationVector:
Data.b ....... 32 bytes
EndDataSection
PrincieD
Addict
Addict
Posts: 861
Joined: Wed Aug 10, 2005 2:08 pm
Location: Yorkshire, England
Contact:

Re: Encryption dilemma

Post by PrincieD »

ahh yes that's a bit smaller than mine :) although the ciphersaber is good to learn from!

Chris.
ProGUI - Professional Graphical User Interface Library - http://www.progui.co.uk
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Encryption dilemma

Post by Thorium »

As i understand it the software does the encryption automaticly, so the key is included in the software? Or does the user choose the key?

If the key is inside the software i would not care much about security. The weakspot would be the actual key anyways. So it would not make a difference what encryption you use. So with that in mind i would go with a xor encryption with a long key, maybe 1KB long. Its extremly fast, much faster than any other encryption.
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Encryption dilemma

Post by doctorized »

Thorium wrote:As i understand it the software does the encryption automaticly, so the key is included in the software? Or does the user choose the key?
The program has the key, not the user. The whole point to have te program installed n two or more computers that will send data from one to an other and I want an encryption just in case the data fall in hands other than the real recipient's.
Thorium wrote:If the key is inside the software i would not care much about security. The weakspot would be the actual key anyways. So it would not make a difference what encryption you use.
The program will not be public. Only some users will have and use it so it will be a little difficult for someone else to get the program an find the key.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Encryption dilemma

Post by wilbert »

I read somewhere about PRESENT yesterday.
http://homes.esat.kuleuven.be/%7Eabogda ... ches07.pdf
It seems to be very lightweight and I found a site with a few implementations (no PureBasic of course)
http://www.lightweightcrypto.org/implementations.php
Post Reply