It is currently Mon Mar 01, 2021 11:10 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: PB_AES_Enhancer_with advanced StringCrypter
PostPosted: Fri Feb 19, 2021 10:25 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Apr 05, 2020 11:28 am
Posts: 503
Location: Pandora
This new crypter adds the missing convenience functions of AESEncrypter() and AESDecrypter().

The functions of this crypter are fully compatible to the original PB functions.

These new functions extend the original PB functions without changing them
and add easy to use functions for Key and IV generating.

This new Crypter simplifies the AES handling significantly.

With this new enhanced functions you can use AES as usual,
but the annoying problem if files, data or strings are smaller than 16 bytes is eliminated
and there is no more complicated key or IV generation.


Data lengths from zero upwards are now supported.

Included are :
String to binary key generating for AES-128.
String to binary key generating for AES-256.
String to binary IV generating
Randomized IV generating.
Limitless PB AES function compatible AESEncoder().
Limitless PB AES function compatible AESDecoder().
String to hash keystretching function with progressbar support.

Advanced StringCrypter with automatic randomized IV, Fake StringLength feature, automatic Integrity check feature.
Supports UTF8 and UTF16.
UTF8 string files are about 50% smaller than standard UTF16 files.
UTF16 is very fast.
If you encrypt the exact same string million times,
each encrypted file will be have a different content.


You can also use these new functions to encrypt blockwise with Progressbar.
Just increase the IV by one with each new block.
So primarily mostly you do not need the StartAESCipher() and AddCipherBuffer() functions.

It doesn't matter now if the data or strings to be encrypted are smaller or larger than 16 bytes, no padding is needed anymore.

The only thing that changes is that now everything is as extremely simple as you probably always wanted it to be.


Keys and IV are usually implemented incorrectly, so that it cries out for a simple universally usable solution.

Note that for data smaller than 16 Bytes Encrypter and Decrypter work the same.
Therefore, a double encryption with the same Key and IV causes a decryption.
This may be difficult to understand at first, but it is absolutely OK and all works very fine.

Base Code - full featured
Code:
DeclareModule PB_AES_Enhancer
  EnableExplicit
  Declare Create_16_Byte_Key(key$, salt$="") ; For AES-128
  Declare Create_32_Byte_Key(key$, salt$="") ; For AES-256
  Declare Create_IV(iv$)                     ; Binary IV
  Declare Create_RND_IV()                    ; Binary randomized IV
  Declare QAESEncoder(*input.byte, *output.byte, size, *key, bits, *iv, modus=#PB_Cipher_CBC) ; Encoder
  Declare QAESDecoder(*input.byte, *output.byte, size, *key, bits, *iv, modus=#PB_Cipher_CBC) ; Decoder
  Declare.s QAESKeyStretching(window_ID, progressbar_ID, key$, salt$="", key_stretching_loops=1e4) ; Key-stretching
EndDeclareModule

Module PB_AES_Enhancer
  ; PB AES Functions compatible Crypter with smaller 16 Bytes Fix - By Saki
  ; < 16 Bytes Encoder and Decoder works at the same
  ; KeyStretching impeded key brute force quite substantial
  UseMD5Fingerprint() : UseSHA3Fingerprint()
 
  Procedure Create_16_Byte_Key(key$, salt$="")
    Protected i, ii, stepp=SizeOf(character)<<1 : Static Dim register.q(1)
    key$=StringFingerprint(key$+salt$, #PB_Cipher_MD5, 0, #PB_Unicode)
    For i=0 To 15 : PokeB(@register(0)+i, Val("$"+PeekS(@key$+ii, 2))) : ii+stepp : Next
    ProcedureReturn @register.q(0)
  EndProcedure
 
  Procedure Create_32_Byte_Key(key$, salt$="")
    Protected i, ii, stepp=SizeOf(character)<<1 : Static Dim register.q(3)
    key$=StringFingerprint(key$+salt$, #PB_Cipher_SHA3, 256, #PB_Unicode)
    For i=0 To 31 : PokeB(@register(0)+i, Val("$"+PeekS(@key$+ii, 2))) : ii+stepp : Next
    ProcedureReturn @register.q(0)
  EndProcedure
 
  Procedure Create_IV(iv$)
    Protected i, ii, stepp=SizeOf(character)<<1 : Static Dim iv.q(1)
    iv$=StringFingerprint(iv$, #PB_Cipher_MD5, 0, #PB_Unicode)
    For i=0 To 15 : PokeB(@iv(0)+i, Val("$"+PeekS(@iv$+ii, 2))) : ii+stepp : Next
    ProcedureReturn @iv(0)
  EndProcedure
 
  Procedure Create_RND_IV()
    Static Dim iv.q(1) : If OpenCryptRandom() : CryptRandomData(@iv.q(0), 16) : ProcedureReturn @iv.q(0) : EndIf
    ProcedureReturn 0
  EndProcedure
 
  Macro common(coder)
    If size<16
      Protected i, *ii.byte : Dim buffer.q(1) : If modus=#PB_Cipher_CBC : CopyMemory(*iv, @buffer(0), 16) : EndIf
      Protected result=AESEncoder(@buffer(0), @buffer(0), 16, *key, bits, 0, #PB_Cipher_ECB)
      If Not result : ProcedureReturn 0 : EndIf
      ; For i=0 To size-1 : PokeB(*output+i, PeekB(@buffer(0)+i) ! PeekB(*input+i)) : Next : ProcedureReturn size ; simple
      *ii=@buffer(0)
      For i=1 To size : *output\b=*ii\b ! *input\b : *input+1 : *output+1 : *ii+1 : Next
    ProcedureReturn size : Else : ProcedureReturn AES#coder(*input, *output, size, *key, bits, *iv, modus)
    EndIf
  EndMacro
 
  Procedure QAESEncoder(*input.byte, *output.byte, size, *key, bits, *iv, modus=#PB_Cipher_CBC)
    common(Encoder)
  EndProcedure
 
  Procedure QAESDecoder(*input.byte, *output.byte, size, *key, bits, *iv, modus=#PB_Cipher_CBC)
    common(Decoder)
  EndProcedure
 
  Procedure.s QAESKeyStretching(window_ID, progressbar_ID, key$, salt$="", key_stretching_loops=1e4)
    Protected get_time.q , i ; by walbus
    get_time=ElapsedMilliseconds()
    For i=1 To key_stretching_loops ; Iterations
      key$=ReverseString(salt$)+key$+salt$+ReverseString(key$)
      key$=Fingerprint(@key$, StringByteLength(key$), #PB_Cipher_SHA3, 512)
      If IsWindow(window_ID) And IsGadget(progressbar_ID)
        If ElapsedMilliseconds()>get_time+100
          SetGadgetState(progressbar_ID, 100*i/key_stretching_loops)
          get_time.q=ElapsedMilliseconds()
        EndIf
      EndIf
    Next i
    key$=ReverseString(key$)+salt$+key$+ReverseString(key$)
    key$=Fingerprint(@key$, StringByteLength(key$), #PB_Cipher_SHA3, 512) ; Finalize
    If IsGadget(progressbar_ID)
      SetGadgetState(progressbar_ID, 100)
    EndIf
    Delay(100)
    ProcedureReturn key$
  EndProcedure
EndModule
UseModule PB_AES_Enhancer

; ######################### Use the module ##########################
EnableExplicit

Define source$="ABCDEF"; GHIJKLMNOP" ; GHIJKLMNOPQRSTUVWXYZ0123456789" ; You can change how ever you want
Define destination$=Space(Len(source$))

Define password$="Hello Password"
Define iv$="123456"

; With KeyStretching - 1e4 Loops
Define key$=QAESKeyStretching(0, 0, Password$, iv$, 1e4)
Debug "KeyStretching Sample : "+key$
Debug ""
Debug "Bytes encrypted : "+QAESEncoder(@source$, @destination$, StringByteLength(source$), Create_32_Byte_Key(key$), 256, Create_IV(iv$))
Debug destination$
Debug " "
Debug "Bytes decrypted : "+QAESDecoder(@destination$, @source$, StringByteLength(source$), Create_32_Byte_Key(key$), 256, Create_IV(iv$))
Debug source$
Debug ""
; Without KeyStretching
Debug "Bytes encrypted : "+QAESEncoder(@source$, @destination$, StringByteLength(source$), Create_32_Byte_Key(Password$), 256, Create_IV(iv$))
Debug destination$
Debug " "
Debug "Bytes decrypted : "+QAESDecoder(@destination$, @source$, StringByteLength(source$), Create_32_Byte_Key(Password$), 256, Create_IV(iv$))
Debug source$


Code with advanced StringCrypter
Code:
DeclareModule PB_AES_Enhancer
  EnableExplicit
  Declare Create_16_Byte_Key(key$, salt$="") ; For AES-128
  Declare Create_32_Byte_Key(key$, salt$="") ; For AES-256
  Declare Create_IV(iv$)                     ; Binary IV
  Declare Create_RND_IV()                    ; Binary randomized IV
  Declare QAESEncoder(*input.byte, *output.byte, size, *key, bits, *iv, modus=#PB_Cipher_CBC) ; Encoder
  Declare QAESDecoder(*input.byte, *output.byte, size, *key, bits, *iv, modus=#PB_Cipher_CBC) ; Decoder
  Declare.s QAESKeyStretching(window_ID, progressbar_ID, key$, salt$="", key_stretching_loops=1e4) ; Key-stretching
 
  ; String crypter with automatically randomized IV
  ; UTF16 mode=0 / UTF8 mode=1 - UTF8 files are ~50% smaller - UTF16 is very fast
  ; ..._fake_length - Blur the real string length
  Declare QAES_Write_encrypted_string_file(string$, destination_path$, key$, mode=0, min_fake_length=0, max_fake_length=0, check=0) ; WriteFile fail = -1
  Declare.s QAES_Read_encrypted_string_file(destination_path$, key$, mode=0, check=0)
  Declare QAESCheckIntegrity() ; Check the integrity of the string after decrypting a string file
  Declare QAESCheckDecrypter() ; Check the string decrypter state after decrypting a string file - ReadFile fail = -1
  Declare.s QAESGetPath()      ;Get the actual used path
EndDeclareModule

Module PB_AES_Enhancer
  ; PB AES Functions compatible Crypter with smaller 16 Bytes Fix - By Saki
  ; With advanced string crypter
  ; < 16 Bytes Encoder and Decoder works at the same
  ; KeyStretching impeded key brute force quite substantial
  UseMD5Fingerprint() : UseSHA3Fingerprint()
 
  Global QAESCheckIntegrity, QAESCheckDecrypter, QAESGetPath$
 
  Procedure Create_16_Byte_Key(key$, salt$="")
    Protected i, ii, stepp=SizeOf(character)<<1 : Static Dim register.q(1)
    key$=StringFingerprint(key$+salt$, #PB_Cipher_MD5, 0, #PB_Unicode)
    For i=0 To 15 : PokeB(@register(0)+i, Val("$"+PeekS(@key$+ii, 2))) : ii+stepp : Next
    ProcedureReturn @register.q(0)
  EndProcedure
 
  Procedure Create_32_Byte_Key(key$, salt$="")
    Protected i, ii, stepp=SizeOf(character)<<1 : Static Dim register.q(3)
    key$=StringFingerprint(key$+salt$, #PB_Cipher_SHA3, 256, #PB_Unicode)
    For i=0 To 31 : PokeB(@register(0)+i, Val("$"+PeekS(@key$+ii, 2))) : ii+stepp : Next
    ProcedureReturn @register.q(0)
  EndProcedure
 
  Procedure Create_IV(iv$)
    Protected i, ii, stepp=SizeOf(character)<<1 : Static Dim iv.q(1)
    iv$=StringFingerprint(iv$, #PB_Cipher_MD5, 0, #PB_Unicode)
    For i=0 To 15 : PokeB(@iv(0)+i, Val("$"+PeekS(@iv$+ii, 2))) : ii+stepp : Next
    ProcedureReturn @iv(0)
  EndProcedure
 
  Procedure Create_RND_IV()
    Static Dim iv.q(1) : If OpenCryptRandom() : CryptRandomData(@iv.q(0), 16) : ProcedureReturn @iv.q(0) : EndIf
    ProcedureReturn 0
  EndProcedure
 
  Macro common(coder)
    If size<16
      Protected i, *ii.byte : Dim buffer.q(1)
      Protected result=AESEncoder(@buffer(0), @buffer(0), 16, *key, bits, *iv, #PB_Cipher_CBC)
      If Not result : ProcedureReturn 0 : EndIf
      ; For i=0 To size-1 : PokeB(*output+i, PeekB(@buffer(0)+i) ! PeekB(*input+i)) : Next : ProcedureReturn size ; simple
      *ii=@buffer(0) : For i=1 To size : *output\b=*ii\b ! *input\b : *input+1 : *output+1 : *ii+1 : Next
    ProcedureReturn size : Else : ProcedureReturn AES#coder(*input, *output, size, *key, bits, *iv, modus)
    EndIf
  EndMacro
 
  Procedure QAESEncoder(*input.byte, *output.byte, size, *key, bits, *iv, modus=#PB_Cipher_CBC)
    common(Encoder)
  EndProcedure
 
  Procedure QAESDecoder(*input.byte, *output.byte, size, *key, bits, *iv, modus=#PB_Cipher_CBC)
    common(Decoder)
  EndProcedure
 
  Procedure.s QAESKeyStretching(window_ID, progressbar_ID, key$, salt$="", key_stretching_loops=1e4)
    Protected get_time.q , i ; by walbus
    get_time=ElapsedMilliseconds()
    For i=1 To key_stretching_loops ; Iterations
      key$=ReverseString(salt$)+key$+salt$+ReverseString(key$)
      key$=Fingerprint(@key$, StringByteLength(key$), #PB_Cipher_SHA3, 512)
      If IsWindow(window_ID) And IsGadget(progressbar_ID)
        If ElapsedMilliseconds()>get_time+100
          SetGadgetState(progressbar_ID, 100*i/key_stretching_loops)
          get_time.q=ElapsedMilliseconds()
        EndIf
      EndIf
    Next i
    key$=ReverseString(key$)+salt$+key$+ReverseString(key$)
    key$=Fingerprint(@key$, StringByteLength(key$), #PB_Cipher_SHA3, 512) ; Finalize
    If IsGadget(progressbar_ID)
      SetGadgetState(progressbar_ID, 100)
    EndIf
    Delay(100)
    ProcedureReturn key$
  EndProcedure
 
  Procedure QAES_Write_encrypted_string_file(string$, destination_path$, key$, mode=0, min_fake_length=0, max_fake_length=0, check=0)
    QAESGetPath$=destination_path$
    Protected file=CreateFile(#PB_Any, QAESGetPath$), *buffer, *buffer_1, *buffer_2, i, ii, stepp=SizeOf(character)<<1
    If Not file : ProcedureReturn -1 : EndIf
    If min_fake_length<0 : min_fake_length=0 : EndIf
    If max_fake_length<0 : max_fake_length=0 : EndIf
    Protected Dim iv.q(1), seed.q
    If OpenCryptRandom()
      If min_fake_length>0 Or max_fake_length>0
        CryptRandomData(@seed, 8)
      EndIf
      CryptRandomData(@iv(0), 16)
    Else
      If min_fake_length>0 Or max_fake_length>0
        RandomData(@seed, 8)
      EndIf
      RandomData(@iv(0), 16)
    EndIf
    Protected *iv=@iv(0), *key=Create_32_Byte_Key(key$)
    RandomSeed(seed)
    If mode=1
      *buffer=UTF8(string$)
      If Not *buffer : ProcedureReturn -2 : EndIf
      If seed
        If min_fake_length>max_fake_length : max_fake_length=min_fake_length : EndIf
        *buffer_1=AllocateMemory(MemorySize(*buffer)+Random(max_fake_length, min_fake_length))
        If Not *buffer_1 : FreeMemory(*buffer) : ProcedureReturn -3 : EndIf
        CopyMemory(*buffer, *buffer_1, MemorySize(*buffer))
        Swap *buffer, *buffer_1
        FreeMemory(*buffer_1)
      EndIf
    Else
      If seed
        If min_fake_length>max_fake_length : max_fake_length=min_fake_length : EndIf
        *buffer=AllocateMemory(StringByteLength(string$)+1+Random(max_fake_length, min_fake_length))
      Else
        *buffer=AllocateMemory(StringByteLength(string$)+1)
      EndIf
      If Not *buffer : ProcedureReturn -4 : EndIf
    EndIf
    If Not mode
      CopyMemory(@string$, *buffer, MemorySize(*buffer))
    EndIf
    *buffer_2=AllocateMemory(MemorySize(*buffer))
    If Not *buffer_2 : FreeMemory(*buffer) : CloseFile(file) : ProcedureReturn -5 : EndIf
    If Not QAESEncoder(*buffer, *buffer_2, MemorySize(*buffer), *key, 256, *iv, #PB_Cipher_CBC)
      FreeMemory(*buffer) : CloseFile(file) : ProcedureReturn -6
    EndIf
    If WriteData(file, *buffer_2, MemorySize(*buffer))<>MemorySize(*buffer)
      FreeMemory(*buffer) : FreeMemory(*buffer_2) : CloseFile(file) : ProcedureReturn -7
    EndIf
    FreeMemory(*buffer_2)
    If WriteData(file, @iv(0), 16)<>16 : FreeMemory(*buffer) : CloseFile(file) : ProcedureReturn -8 : EndIf
    FreeMemory(*buffer)
    If check
      Protected hash$=StringFingerprint(string$, #PB_Cipher_SHA3, 256, #PB_Unicode)
      *buffer=AllocateMemory(32)
      If Not *buffer : CloseFile(file) : ProcedureReturn -9 : EndIf
      For i=0 To 31 : PokeB(*buffer+i, Val("$"+PeekS(@hash$+ii, 2))) : ii+stepp : Next
      For i=0 To 16 Step 16
        If Not QAESEncoder(*buffer+i, *buffer+i, 16, *key, 256, *iv, #PB_Cipher_CBC)
          FreeMemory(*buffer) : CloseFile(file) : ProcedureReturn -10
        EndIf
      Next
      If WriteData(file, *buffer, 32)<>32 : FreeMemory(*buffer) : CloseFile(file) : ProcedureReturn -11 : EndIf
      FreeMemory(*buffer)
    EndIf
    CloseFile(file)
    Create_32_Byte_Key(" ") ; Clear from ram after processing
    ProcedureReturn 1
  EndProcedure
 
  Procedure.s QAES_Read_encrypted_string_file(destination_path$, key$, mode=0, check=0)
    QAESGetPath$=destination_path$
    Protected *buffer, *buffer_1, *buffer_2, i, ii, stepp=SizeOf(character)<<1
    Macro FreeMem
      If *buffer : FreeMemory(*buffer) : EndIf
      If *buffer_1 : FreeMemory(*buffer_1) : EndIf
    EndMacro
   
    Protected file=ReadFile(#PB_Any, QAESGetPath$)
    If Not file : QAESCheckDecrypter=-1 : ProcedureReturn "" : EndIf
    If file
      If check
        check=32
        *buffer_1=AllocateMemory(32)
        If Not *buffer_1 : CloseFile(file) : QAESCheckDecrypter=-2 : ProcedureReturn "" : EndIf
        FileSeek(file, Lof(file)-check)
        If ReadData(file, *buffer_1, 32)<>32 : FreeMem : QAESCheckDecrypter=-3 : CloseFile(file) : ProcedureReturn "" : EndIf
      EndIf
      *buffer=AllocateMemory(Lof(file)-16-check)
      If Not *buffer : FreeMem : QAESCheckDecrypter=-4 : ProcedureReturn "" : EndIf
      Protected Dim iv.q(1), *iv=@iv(0), *key=Create_32_Byte_Key(key$)
      FileSeek(file, Lof(file)-16-check)
      If ReadData(file, @iv(0), 16)<>16 : FreeMem : QAESCheckDecrypter=-5 : CloseFile(file) : ProcedureReturn "" : EndIf
      FileSeek(file, 0)
      If ReadData(file, *buffer, Lof(file)-16-check)<>Lof(file)-16-check : QAESCheckDecrypter=-6 : FreeMem : CloseFile(file) : ProcedureReturn "" : EndIf
      *buffer_2=AllocateMemory(MemorySize(*buffer))
      If Not *buffer_2 : FreeMem : QAESCheckDecrypter=-7 : CloseFile(file) : ProcedureReturn "" : EndIf
      If Not QAESDecoder(*buffer, *buffer_2, MemorySize(*buffer), *key, 256, *iv, #PB_Cipher_CBC)
        FreeMem : QAESCheckDecrypter=-8 : FreeMemory(*buffer_2) : ProcedureReturn ""
      EndIf
      If mode=1
        Protected string$=PeekS(*buffer_2 , -1, #PB_UTF8)
      Else
        string$=Space(MemorySize(*buffer_2))
        CopyMemory(*buffer_2, @string$, MemorySize(*buffer))
      EndIf
      FreeMemory(*buffer_2)
      CloseFile(file)
      If check
        Protected hash$=StringFingerprint(string$, #PB_Cipher_SHA3, 256, #PB_Unicode)
        *buffer=AllocateMemory(32)
        If Not *buffer : QAESCheckDecrypter=-9 : FreeMem : ProcedureReturn "" : EndIf
        For i=0 To 31 : PokeB(*buffer+i, Val("$"+PeekS(@hash$+ii, 2))) : ii+stepp : Next
        For i=0 To 16 Step 16
          If Not QAESDecoder(*buffer_1+i, *buffer_1+i, 16, *key, 256, *iv, #PB_Cipher_CBC)
            FreeMem : QAESCheckDecrypter=-10 : ProcedureReturn ""
          EndIf
        Next
        QAESCheckIntegrity=CompareMemory(*buffer, *buffer_1, MemorySize(*buffer))
        FreeMemory(*buffer)
      EndIf
      Create_32_Byte_Key(" ") ; Clear from ram after processing
    EndIf
    QAESCheckDecrypter=1
    ProcedureReturn string$
  EndProcedure
 
  Procedure QAESCheckIntegrity()
    ProcedureReturn QAESCheckIntegrity
  EndProcedure
 
  Procedure QAESCheckDecrypter()
    ProcedureReturn QAESCheckDecrypter
  EndProcedure
 
  Procedure.s QAESGetPath()
    ProcedureReturn QAESGetPath$
  EndProcedure
 
EndModule
UseModule PB_AES_Enhancer

; ######################### Use the module ##########################
EnableExplicit

Define source$="ABCDEF"; GHIJKLMNOP" ; GHIJKLMNOPQRSTUVWXYZ0123456789" ; You can change how ever you want
Define destination$=Space(Len(source$))

Define password$="Hello Password"
Define iv$="123456"

; With KeyStretching - 1e4 Loops
Define key$=QAESKeyStretching(0, 0, Password$, iv$, 1e4)
Debug "KeyStretching Sample : "+key$
Debug ""
Debug "Bytes encrypted : "+QAESEncoder(@source$, @destination$, StringByteLength(source$), Create_32_Byte_Key(key$), 256, Create_IV(iv$))
Debug destination$
Debug " "
Debug "Bytes decrypted : "+QAESDecoder(@destination$, @source$, StringByteLength(source$), Create_32_Byte_Key(key$), 256, Create_IV(iv$))
Debug source$
Debug ""
; Without KeyStretching
Debug "Bytes encrypted : "+QAESEncoder(@source$, @destination$, StringByteLength(source$), Create_32_Byte_Key(Password$), 256, Create_IV(iv$))
Debug destination$
Debug " "
Debug "Bytes decrypted : "+QAESDecoder(@destination$, @source$, StringByteLength(source$), Create_32_Byte_Key(Password$), 256, Create_IV(iv$))
Debug source$
Debug ""
; String to file crypting
Define mode=0 ; 0=UTF16 - 1=UTF8
Define path$=GetUserDirectory(#PB_Directory_Desktop)+"Encrypted_String"
Debug "String encrypt State  : "+QAES_Write_encrypted_string_file(source$, path$, password$, mode)
Debug "Path : "+QAESGetPath()
Debug "String decrypted  : "+QAES_Read_encrypted_string_file(path$, password$, mode)
Debug " "
; String to file crypting - Randomized length
Define min_fake_length=5
Define max_fake_length=30
Define mode=0 ; 0=UTF16 - 1=UTF8
Define path$=GetUserDirectory(#PB_Directory_Desktop)+"Encrypted_String_Randomized_length"
Debug "String encrypt State : "+QAES_Write_encrypted_string_file(source$, path$, password$, mode, min_fake_length, max_fake_length)
Debug "Path : "+QAESGetPath()
Debug "String decrypted  : "+QAES_Read_encrypted_string_file(path$, password$, mode)
Debug " "
; String to file crypting - Randomized length - String Check
Define check=1
Define path$=GetUserDirectory(#PB_Directory_Desktop)+"Encrypted_String_Randomized_length_and_Check"
Debug "String encrypt State  : "+QAES_Write_encrypted_string_file(source$, path$, password$, mode, min_fake_length, max_fake_length, check)
Debug "Path : "+QAESGetPath()
Debug "String decrypted  : "+QAES_Read_encrypted_string_file(path$, password$, mode, check)
Debug "State Decrypter after decrypting : "+QAESCheckDecrypter() ; Check the string decrypter state after decrypting a string file
Debug "State decrypted String integrity check : "+QAESCheckIntegrity() ; Check the integrity of the string after decrypting a string file

_________________
地球上の平和


Last edited by Saki on Sun Feb 28, 2021 12:24 am, edited 23 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: PB_AES_Enhancer_with advanced StringCrypter
PostPosted: Tue Feb 23, 2021 11:16 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Apr 05, 2020 11:28 am
Posts: 503
Location: Pandora
Code significantly enhanced

Look the first post.

Have fun

_________________
地球上の平和


Top
 Profile  
Reply with quote  
 Post subject: Re: PB_AES_Enhancer_with advanced StringCrypter
PostPosted: Wed Feb 24, 2021 9:59 am 
Offline
Addict
Addict
User avatar

Joined: Thu Aug 07, 2003 7:01 pm
Posts: 3175
Location: United Kingdom
Thanks. It would be good if @Fred added these to the official pb lib.

_________________
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system


Top
 Profile  
Reply with quote  
 Post subject: Re: PB_AES_Enhancer_with advanced StringCrypter
PostPosted: Wed Feb 24, 2021 3:38 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Apr 05, 2020 11:28 am
Posts: 503
Location: Pandora
Hi, yes, it really wouldn't be bad if something was improved there.
Many users have an extremely hard time with it.

_________________
地球上の平和


Last edited by Saki on Thu Feb 25, 2021 1:08 am, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: PB_AES_Enhancer_with advanced StringCrypter
PostPosted: Wed Feb 24, 2021 8:17 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4870
Location: Lyon - France
Works here thanks for sharing 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
 Post subject: Re: PB_AES_Enhancer_with_advanced_StringCrypter
PostPosted: Wed Feb 24, 2021 10:10 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Apr 05, 2020 11:28 am
Posts: 503
Location: Pandora
Thank you DoubleDutch and Kwai

Please get the new Codes above.

I think now all ist final.

_________________
地球上の平和


Top
 Profile  
Reply with quote  
 Post subject: Re: PB_AES_Enhancer_with advanced StringCrypter
PostPosted: Thu Feb 25, 2021 3:31 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 04, 2015 7:10 am
Posts: 1683
Saki could you please give a description of your padding technique? it's critical. Thankyou :)


Top
 Profile  
Reply with quote  
 Post subject: Re: PB_AES_Enhancer_with advanced StringCrypter
PostPosted: Thu Feb 25, 2021 10:53 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Apr 05, 2020 11:28 am
Posts: 503
Location: Pandora
Codes updated - a little

Hi Keya, no problem.

With PB this is already implemented, but only half.
It is taken into account that data to be encrypted can have lengths that are not divisible by 16,
but unfortunately it is not taken into account that the data can also be shorter than 16 bytes.
Thus the advantage of this thing is almost completely cancelled out and simple becomes cumbersome again.

Suppose you want to encrypt five bytes.
But AES is a 16 byte block cipher, which means that you always have to encrypt 16 bytes.

If a file is now bigger, e.g. 16+5 bytes, then you can steal the
missing 11 bytes from the previous block.
Stealing is the wrong term, but unfortunately it is used that way.

The correct term is borrow.
Because everything you borrow you have to give back.
If you don't do that, there will be trouble.

If the data is only 5 bytes, you can't borrow anything from a previous block, because there is none.

You have to create a 16 bytes register yourself.

But one after the other.
You want to encrypt 5 ascii characters, that looks like this : ABCDE00000000000 .

If you encrypt this now you get for example this: 㢞憉⒙篤妄꒸싥ﻁ㚦韣ᰶ昒했嵌ឨ .

But now you have to cut off eleven excess characters: 㢞憉⒙篤妄 .
But you need these 11 truncated characters to decrypt, you can't replace them with zeros.

Therefore one goes and renounces the decrypting, one encrypts thus only.
And you only encrypt the register you created yourself.
It doesn't matter what's in there, the crypter works with zeros too.
If an IV is given, the Crypter will link it with this register additionally Xor and then encrypt it.

Now you go and take the first 5 encrypted bytes from this register and concatenate
these Xor with the actual 5 bytes to be encrypted.

So in your register that you created yourself, you create 16 encrypted bytes.
You then borrow as many of these as you need, in this case 5.

For decryption it is exactly the same, you only use the encoder for encrypting and decrypting.

If this is applied to the five encrypted bytes, they will be decrypted,
because they are Xor-linked.

Once you figure this out, it's pretty simple.

https://en.wikipedia.org/wiki/Ciphertext_stealing

Have fun with it

_________________
地球上の平和


Top
 Profile  
Reply with quote  
 Post subject: Re: PB_AES_Enhancer_with advanced StringCrypter
PostPosted: Thu Feb 25, 2021 6:08 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4870
Location: Lyon - France
I tested the 2 new codes, and that works too :D
Thanks again for sharing your works 8)

_________________
ImageThe happiness is a road...
Not a destination


Top
 Profile  
Reply with quote  
 Post subject: Re: PB_AES_Enhancer_with advanced StringCrypter
PostPosted: Sat Feb 27, 2021 10:44 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Apr 05, 2020 11:28 am
Posts: 503
Location: Pandora
Many thanks again Kwai.

The advanced code is optimized and extended again.
The String Crypter meets all current security standards.

Remember that you cannot use the String Crypter with the original
PB AES calls.

But you can simply replace the original calls in your code with the QAES calls, then you can use it.

Well, I'm curious to see how the new module will go on.

Hint : You can look from time to time for new codes, i announce this mostly not if there are no compatibility changes.

Have fun with it.

_________________
地球上の平和


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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 15 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:  
cron

 


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