SHA3 512 bit based high secure crypter

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

SHA3 512 bit based high secure crypter

Post by walbus »

SHA3 512bit special hash based CTR mode crypter

A very cool and very other crypter


This crypter use not AES :shock:

It's a propietary special crypter, usable as pre or post crypter, or simple as stand alone crypter
This crypter works slowly, but you become a very high security level
Compared with AES256 you have with this crypter a double key register length = 64 Bytes / 512 Bit

The crypter works as sample also with the QAES crypted pinboard or additional with each other AES crypter
http://www.purebasic.fr/english/viewtop ... 27&t=68633

Use you this crypter as pre or post crypter, use ever a separately password for this crypter ( A additional needed pin )
Two crypter with the same password are available, but mostly not the best idea

I think not you have seen the same before :wink:

I see often false AES using, false IV using, false IV and key generating
False understanding what is a IV and what do a IV and how works a IV
False hash handling
AES is a very cool thing, but very false using is the same, as you tow a truck with a clothesline :shock:

With this crypter here you want not a IV, the block counter do the same, absolutely simple,
without the weakpoints from a IV
And you can not go wrong with key generating
Also you want not a separately decrypter
You want not a padding, the encrypted data has exactly the same length as the plain data

Using this crypter is very, very simple, important simpler as using the PB AES function !
Alterable you found here a fast AES based OFB mode crypter, with string termination handling for ascii and unicode !
http://www.purebasic.fr/english/viewtop ... 27&t=66894

Code: Select all

;- SHA3 hash based CTR crypter  ----------------------------------------------------
; Author : (c) Werner Albus - www-nachtoptik.de - www.quick-aes-256.de
; Please set a hint in your software for using QAES contents 
; This is a special SHA3 based crypter for files and strings - simplest to use
; The coder works with all data lengths

DeclareModule QAES_smart_hash_coder
  Declare QAES_smart_hash_coder(*buffer_in, *buffer_out.quad, bytes.q, key$, counter.q=0)
EndDeclareModule

Module QAES_smart_hash_coder
  
  UseSHA3Fingerprint()
  
  EnableExplicit
  
  Procedure.s hash64_stretch(key$)
    #KeyStretchingLoops=1000
    #Salt$="%/E=?=$/Tkuerfo8e67r3o8qawegmp/TJ&§ O(vgtrHfRE%$(:ßr2384n46nc qw7po"
    Protected i
    For i=1 To #KeyStretchingLoops
      key$+Str(i)
      key$=Fingerprint(@key$, StringByteLength(key$), #PB_Cipher_SHA3, 512)
      ; While WindowEvent() : Wend ; ################# When required activate this Line #################
    Next i
    key$=ReverseString(key$+#Salt$)
    ProcedureReturn Fingerprint(@key$, StringByteLength(key$), #PB_Cipher_SHA3, 512)
  EndProcedure
  
  ;- Hash Coder - SHA3 512 based - This coder works with all data lengths ----------------- 
  Procedure QAES_smart_hash_coder(*buffer_in, *buffer_out.quad, bytes.q, key$, counter.q=0)
    ; You cipher a file blockwise, set ever the current block number (consecutive) with the counter - Important !
    ; UNICODE results are other as ASCII results
    
    #UNICODE=SizeOf(character)
    Protected.q i, rounds=bytes>>4
    Protected flip, stepp=#UNICODE<<1, ii, iii, bytes_minus_1=bytes-1
    Protected mult_16_stepp=stepp<<4, mult_64_stepp=stepp<<6-1, rest=bytes%16, unicode_mult_128=#UNICODE<<7, unicode_mult_16=#UNICODE<<4
    Static fixed_hash_string${128}
    Static Dim register.q(1)
    
    fixed_hash_string$=hash64_stretch(key$+Str(counter))
    
    If bytes<16 ; Less 16 bytes
      
      For ii=bytes_minus_1 To 0 Step -1
        PokeA(@register(0)+ii, Val("$"+PeekS(@fixed_hash_string$+iii, 2)))
        iii+stepp
      Next
      
      For ii=0 To bytes_minus_1
        PokeA(*buffer_out+ii, PeekA(*buffer_in+ii) ! PeekA(@register(0)+ii))
      Next
      ProcedureReturn 1
    EndIf
    
    While i<rounds ; =>16 bytes
      If Not flip
        PokeQ(@fixed_hash_string$, PeekQ(@fixed_hash_string$)+i)
        fixed_hash_string$=Fingerprint(@fixed_hash_string$, unicode_mult_128,  #PB_Cipher_SHA3, 512)
      EndIf
      
      PokeQ(@register(0), Val("$"+PeekS(@fixed_hash_string$+flip, 16)))
      PokeQ(@register(0)+8, Val("$"+PeekS(@fixed_hash_string$+unicode_mult_16+flip, 16)))
      
      If *buffer_in<>*buffer_out
        MoveMemory(*buffer_in, *buffer_out, 16)
      EndIf
      *buffer_out\q ! register(0)
      *buffer_out+8
      *buffer_out\q ! register(1)
      *buffer_out+8 : *buffer_in+16
      flip+mult_16_stepp : i+1
      If flip>mult_64_stepp : flip=0 : EndIf
    Wend
    
    If rest
      i+1
      PokeQ(@fixed_hash_string$, PeekQ(@fixed_hash_string$)+i)
      fixed_hash_string$=Fingerprint(@fixed_hash_string$, unicode_mult_128,  #PB_Cipher_SHA3, 512)
      iii=0
      
      For ii=rest-1 To 0 Step -1
        PokeA(@register(0)+ii, Val("$"+PeekS(@fixed_hash_string$+iii, 2)))
        iii+stepp
      Next
      
      For ii=0 To rest-1
        PokeA(*buffer_out+ii, PeekA(*buffer_in+ii) ! PeekA(@register(0)+ii))
      Next
    EndIf
    
    ProcedureReturn 1
  EndProcedure
EndModule
UseModule QAES_smart_hash_coder

EnableExplicit

; ;- SHA3 hash based CTR Coder  ----------------------------------------------------
; ; This is a special SHA3 based coder for files, data and strings - simplest to use
; ; The coder works with all data lengths, a padding is not needed
; ; The coder go ever forward, a separately decoder is not needed
; 
; ; block_counter.q          : You cipher a file blockwise, set ever the current block number (consecutive) with a counter - Important !
; ; key$                     : You can use any strings as key
; ; @text$ / @string_result$ : Pointer to the non encrypted data
; ; *buffer                  : Pointer to the encrypted data

Define block_counter=0 ; The pointer is a quad, positive or negative

; Define text$="The quick brown fox jumps over the lazy dog 0123456789"
; Debug "Text to encrypting : "+#TAB$+text$
; ; text$="A"+RSet("Z", 500, "x") ; Demo text string to encrypting; For Unicode ist 1 character = 2 bytes !
; Define *buffer=AllocateMemory(StringByteLength(text$)+1) ; +1 to avoid zero length
; Define string_result$=Space(Len(text$))
; 
; Define key$="This is a simple key"
; 
; ; Encoding
; QAES_smart_hash_coder(@text$, *buffer, StringByteLength(text$), key$, block_counter)
; ShowMemoryViewer(*buffer,StringByteLength(text$))
; Debug "-----------------------------"
; 
; ; Decoding
; QAES_smart_hash_coder(*buffer, @string_result$, StringByteLength(text$), key$, block_counter)
; Debug "Decrypted text : "+#TAB$+RTrim(string_result$) 
; Debug "-----------------------------"

;- Crypt a file blockwise in it self ----------------------------------
Define file, file_size, readed, writen, block_size, window_event, *buffer

Define path$=OpenFileRequester("Select a file to encrypting or decrypting !", "", "*.*", 0)
If path$="" : End : EndIf
OpenWindow(1, 0, 0, 300, 50, "QAES crypter works - please wait", #PB_Window_ScreenCentered)
AddWindowTimer(1, 1, 300)
ProgressBarGadget(1, 10, 10, 280, 30, 0, 100, #PB_ProgressBar_Smooth)
file=OpenFile(#PB_Any, path$)

If file
  file_size=Lof(file) : block_size=4096*4 : FileBuffersSize(file, block_size ) : *buffer=AllocateMemory(block_size)
  Repeat
    readed=ReadData(file, *buffer, block_size)
    QAES_smart_hash_coder(*buffer, *buffer, block_size, "Your key", block_counter) ; QAES crypter - Binary mode
    FileSeek(file, -readed, #PB_Relative) : writen+WriteData(file, *buffer, readed)
    window_event=WaitWindowEvent(1)
    If window_event=#PB_Event_Timer And EventTimer()=1
      SetGadgetState(1, 100*writen/file_size)
    EndIf
    block_counter+1
  Until Not readed
  CloseFile(file) : FreeMemory(*buffer)
  If writen<>file_size : MessageRequester("ERROR", "Writen fails"+#LFCR$+#LFCR$+"Coded Bytes : "+Str(writen)) : EndIf
  #crypt_extender$=" [encrypted]"
  If Right(path$, Len(#crypt_extender$))=#crypt_extender$
    RenameFile(path$, Left(path$, Len(path$)-Len(#crypt_extender$)))
  Else
    RenameFile(path$, path$+#crypt_extender$)
  EndIf
EndIf