Page 2 of 6

Posted: Mon Oct 16, 2006 3:25 pm
by JCV
I added another library

Whirlpool Hashing Algorithm

See first post.

Posted: Tue Oct 17, 2006 2:25 am
by NoahPhense
JCV wrote:I added another library

Whirlpool Hashing Algorithm

See first post.
Sweet.. nice libs

- np

Posted: Tue Oct 17, 2006 7:12 pm
by Konne
Thx for this very much.
Can I use it in comercial apps?

Posted: Tue Oct 17, 2006 7:43 pm
by Beach
Konne wrote:Can I use it in comercial apps?
Looks like you can, here is text from the BLOWFISH example:
; BLOWFISH
; JCV @ PureBasic Forum
;
; This library is provided 'as-is', without any express Or implied warranty.
; In no event will the author be held liable For any damages arising from the use of This software.
; Permission is granted To anyone To use This software For any purpose, including commercial
; applications, And redistribute it freely.

Posted: Wed Oct 18, 2006 6:18 am
by JCV
Konne wrote:Thx for this very much.
Can I use it in comercial apps?
its ok to use ;)

Those libs are optimized in c and asm. :wink:

Posted: Wed Oct 18, 2006 4:47 pm
by JCV
I got several pms today on how to use BlowFish library. Heres a sample usage. Its up to your creativity on how to use the library. I copied some codes of HeX0R's for this example.

All libraries I posted are based from original functions. Its up to you to design on how to use.

Code: Select all

Structure BLOWFISH_CTX
  K.q[18]  
  S0.q[256]
  S1.q[256]
  S2.q[256]
  S3.q[256]
EndStructure

#FL                 = $FFFFFFFF
#BlowFish_UseBase64 = $01
#BlowFish_UseCBC    = $02
;#BlowFish_UseCBC = Will always produce a different encrypted Text(or Memoryblock)
;#BlowFish_UseBase64 = Will encrypt the result in base64.

Procedure.l Blowfish_CryptText(*text, textlen.l, *Password, passlen.l, *Result, Resultlen.l, Mode.l = #BlowFish_UseBase64)
  
  Protected BF.BLOWFISH_CTX, *Buffer, n.l, i.l, a.l, K.q, P.l, b.l
  
  Blowfish_Init(@BF, *Password, passlen)
  
  *Buffer = AllocateMemory(textlen * 2)
  If *Buffer
    n = textlen
    If n % 8 <> 0
      n = 8 - (n % 8)
      For i = 0 To n - 1
        PokeB(*text + textlen + i, 32)
      Next i
      textlen + n
    EndIf
    
    n = textlen / 4
    
    Dim L.Quad(n)
    Dim Cipher.Quad(n+2)
    
    If Mode & #BlowFish_UseCBC
      Cipher(0)\q = Date();
      Cipher(1)\q = ElapsedMilliseconds()*1000000
      P = 2
    EndIf
    
    For i = 0 To n - 1
      L(i)\q = PeekL(*text + i*4) & #FL
    Next i
    
    b = 1
    If n*8 <= Resultlen
      For i = 0 To n - 1 Step 2
        
        If Mode & #BlowFish_UseCBC
          L(i)\q   ! Cipher(b-1)\q
          L(i+1)\q ! Cipher(b)\q
        EndIf
        
        Cipher(i+P)\q   = L(i)\q
        Cipher(i+P+1)\q = L(i+1)\q
        Blowfish_Encrypt(@BF, @Cipher(i + P), @Cipher(i + P+ 1))
        b + 2
      Next i
      
      a = 0
      For i = 0 To n - 1 + P Step 2
        K = Cipher(i)\q & #FL
        PokeL(*Buffer + a, PeekL(@K))
        K = Cipher(i + 1)\q & #FL
        PokeL(*Buffer + a + 4, PeekL(@K))
        a + 8
      Next i
      
      If Mode & #BlowFish_UseBase64
        Base64Encoder(*Buffer, a, *Result, Resultlen)
        a = MemoryStringLength(*Result)
      Else
        CopyMemory(*Buffer, *Result, a)
      EndIf
    EndIf
    FreeMemory(*Buffer)
  EndIf
  
  ProcedureReturn a
  
EndProcedure

Procedure.l Blowfish_DecryptText(*text, textlen.l, *Password, passlen.l, *Result, Resultlen.l, Mode.l = #BlowFish_UseBase64)
  
  Protected BF.BLOWFISH_CTX, *Buffer, n.l, i.l, a.l, j.l, K1.q, K2.q
  
  Blowfish_Init(@BF, *Password, passlen)
  
  *Buffer = AllocateMemory(textlen * 2 + 64)
  If *Buffer
    If Mode & #BlowFish_UseBase64
      textlen = Base64Decoder(*text, textlen, *Buffer, textlen * 2 + 64)
      n = textlen / 4
    Else
      CopyMemory(*text, *Buffer, textlen)
      n = textlen / 4
    EndIf
    
    Dim Cipher.Quad(n+2)
    Dim L.Quad(n)
    
    For i = 0 To n - 1
      Cipher(i)\q = PeekL(*Buffer + i*4) & #FL
    Next i
    
    If Mode & #BlowFish_UseCBC
      j = 2
    Else
      j = 0
    EndIf
    
    a = 0
    If n*8 <= Resultlen
      For i = j To n - 1 Step 2
        K1 = Cipher(i)\q
        K2 = Cipher(i+1)\q
        Blowfish_Decrypt(@BF, @K1, @K2)
        If Mode & #BlowFish_UseCBC
          L(i-j)\q   = K1 ! Cipher(i-2)\q
          L(i+1-j)\q = K2 ! Cipher(i-1)\q
        Else
          L(i-j)\q   = K1
          L(i+1-j)\q = K2
        EndIf
      Next i
      For i = 0 To n - 1 - j Step 2
        K1 = L(i)\q & #FL
        K2 = L(i + 1)\q & #FL
        PokeL(*Result + a, PeekL(@K1))
        PokeL(*Result + a + 4, PeekL(@K2))
        a + 8
      Next i
    EndIf
    
    FreeMemory(*Buffer)
  EndIf
  ProcedureReturn a
  
EndProcedure

Procedure LetsCheck()
  text_to_encrypt$ = "Lets check if you can read this message... Heeeeeeeeeeeeeemmmmmmmmmm... Can you read me? Yessss!!!!"
  password_for_encryption$ = "get me a hard password plssss... Oh am i hard to memorize?  hsdhfhasfkj32432423424"
  Result$ = Space(1024)
  flags.l = #BlowFish_UseCBC | #BlowFish_UseBase64
  
  length = Blowfish_CryptText(@text_to_encrypt$, Len(text_to_encrypt$), @password_for_encryption$, Len(password_for_encryption$), @Result$, Len(Result$), flags)
  Debug Result$
  Debug length  
  
  If length
    text_to_decrypt$ = Result$
    Result$ = Space(1024)
    length = Blowfish_DecryptText(@text_to_decrypt$, length, @password_for_encryption$, Len(password_for_encryption$), @Result$, Len(Result$), flags)
    If length
      Debug Result$
      Debug length
    EndIf
  EndIf
EndProcedure

LetsCheck()
Hope it helps. ;)

Posted: Thu Jan 11, 2007 11:31 pm
by ricardo
localmotion34 wrote: AND if you are a nonUS citizen and use this LIB, and then make it available to Us citizens, the US Gov't can extradite you for the same violation.
Extradite me from my country?

I can't use this to sale an appp in internet????

I live in Argentina and most of my customers are from USA

Posted: Thu Jan 11, 2007 11:38 pm
by ricardo
JCV wrote:I got several pms today on how to use BlowFish library. Heres a sample usage. Its up to your creativity on how to use the library. I copied some codes of HeX0R's for this example.

All libraries I posted are based from original functions. Its up to you to design on how to use.
If i run it i get

Code: Select all

[19:37:38] ApKmRcCkRTr8G47ih4iCm7tJPx9lhuH4+l53ry2LURFWyxv2GUkyRnRCNAmUChJeqzHKAMZ6z3G5Sqma3VPWTAEpR/aKWwPCJpVXSUPgk53ZS0uIYDAdIcQ/UiJvmuhi4soRWzO4DNfOFjGZsHOZig==
[19:37:38] 152
[19:37:38] Lets check if you can read this message... Heeeeeeeeeeeeeemmmmmmmmmm... Can you read me? Yessss!!!!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
[19:37:38] 104
I was expecting to get some encrypted string, right?

Posted: Fri Jan 12, 2007 3:07 am
by JCV
Encrypted String: ApKmRcCkRTr8G47ih4iCm7tJPx9lhuH4+l53ry2LURFWyxv2GUkyRnRCNAmUChJeqzHKAMZ6z3G5Sqma3VPWTAEpR/aKWwPCJpVXSUPgk53ZS0uIYDAdIcQ/UiJvmuhi4soRWzO4DNfOFjGZsHOZig==
Length of Encrypted String: 152

Decrpted String: Lets check if you can read this message... Heeeeeeeeeeeeeemmmmmmmmmm... Can you read me? Yessss!!!!
Length of Decrypted String: 104

Posted: Fri Jan 12, 2007 5:41 am
by ricardo
Ok, sorry my mistake to not read well.

Thanks!!

Posted: Fri Jan 12, 2007 10:46 am
by Num3
localmotion34 wrote:Selling any software to or from the US without approval from the Gov't is a Federal Crime,
and for non-US citizens, can end you up in Gitmo or extradited to the US.
Really?

Kidnapped in Europe... Sent to U.S. in secret CIA flight, and jailed in Guantanamo ???

That's impossible.... Never heard about that ... :twisted: :twisted: :twisted:

Posted: Sat Apr 28, 2007 10:08 am
by Jacobus
I have tested blowfish code in a little example and a problem is appear when i want try again. The first try is ok but the second is failed !?!
With this example, try in this way :
- write a text in the first string
- clic on "Crypt"
- clic on "Decrypt"
All is ok, and now to do again clic on "Crypt" the result is an error. Why??

Code: Select all

Enumeration 
 #WIN_TEST
 #Text1
 #btnEnc
 #Text2
 #btnDec
EndEnumeration

Structure BLOWFISH_CTX 
  K.q[18]  
  S0.q[256] 
  S1.q[256] 
  S2.q[256] 
  S3.q[256] 
EndStructure 

#FL                 = $FFFFFFFF 
#BlowFish_UseBase64 = $01 
#BlowFish_UseCBC    = $02 
;#BlowFish_UseCBC = Will always produce a different encrypted Text(or Memoryblock) 
;#BlowFish_UseBase64 = Will encrypt the result in base64. 

Procedure.l Blowfish_CryptText(*text, textlen.l, *Password, passlen.l, *Result, Resultlen.l, Mode.l = #BlowFish_UseBase64) 
  
  Protected BF.BLOWFISH_CTX, *Buffer, n.l, i.l, a.l, K.q, P.l, b.l 
  
  Blowfish_Init(@BF, *Password, passlen) 
  
  *Buffer = AllocateMemory(textlen * 2) 
  If *Buffer 
    n = textlen 
    If n % 8 <> 0 
      n = 8 - (n % 8) 
      For i = 0 To n - 1 
        PokeB(*text + textlen + i, 32) 
      Next i 
      textlen + n 
    EndIf 
    
    n = textlen / 4 
    
    Dim L.Quad(n) 
    Dim Cipher.Quad(n+2) 
    
    If Mode & #BlowFish_UseCBC 
      Cipher(0)\q = Date(); 
      Cipher(1)\q = ElapsedMilliseconds()*1000000 
      P = 2 
    EndIf 
    
    For i = 0 To n - 1 
      L(i)\q = PeekL(*text + i*4) & #FL 
    Next i 
    
    b = 1 
    If n*8 <= Resultlen 
      For i = 0 To n - 1 Step 2 
        
        If Mode & #BlowFish_UseCBC 
          L(i)\q   ! Cipher(b-1)\q 
          L(i+1)\q ! Cipher(b)\q 
        EndIf 
        
        Cipher(i+P)\q   = L(i)\q 
        Cipher(i+P+1)\q = L(i+1)\q 
        Blowfish_Encrypt(@BF, @Cipher(i + P), @Cipher(i + P+ 1)) 
        b + 2 
      Next i 
      
      a = 0 
      For i = 0 To n - 1 + P Step 2 
        K = Cipher(i)\q & #FL 
        PokeL(*Buffer + a, PeekL(@K)) 
        K = Cipher(i + 1)\q & #FL 
        PokeL(*Buffer + a + 4, PeekL(@K)) 
        a + 8 
      Next i 
      
      If Mode & #BlowFish_UseBase64 
        Base64Encoder(*Buffer, a, *Result, Resultlen) 
        a = MemoryStringLength(*Result) 
      Else 
        CopyMemory(*Buffer, *Result, a) 
      EndIf 
    EndIf 
    FreeMemory(*Buffer) 
  EndIf 
  
  ProcedureReturn a 
  
EndProcedure 

Procedure.l Blowfish_DecryptText(*text, textlen.l, *Password, passlen.l, *Result, Resultlen.l, Mode.l = #BlowFish_UseBase64) 
  
  Protected BF.BLOWFISH_CTX, *Buffer, n.l, i.l, a.l, j.l, K1.q, K2.q 
  
  Blowfish_Init(@BF, *Password, passlen) 
  
  *Buffer = AllocateMemory(textlen * 2 + 64) 
  If *Buffer 
    If Mode & #BlowFish_UseBase64 
      textlen = Base64Decoder(*text, textlen, *Buffer, textlen * 2 + 64) 
      n = textlen / 4 
    Else 
      CopyMemory(*text, *Buffer, textlen) 
      n = textlen / 4 
    EndIf 
    
    Dim Cipher.Quad(n+2) 
    Dim L.Quad(n) 
    
    For i = 0 To n - 1 
      Cipher(i)\q = PeekL(*Buffer + i*4) & #FL 
    Next i 
    
    If Mode & #BlowFish_UseCBC 
      j = 2 
    Else 
      j = 0 
    EndIf 
    
    a = 0 
    If n*8 <= Resultlen 
      For i = j To n - 1 Step 2 
        K1 = Cipher(i)\q 
        K2 = Cipher(i+1)\q 
        Blowfish_Decrypt(@BF, @K1, @K2) 
        If Mode & #BlowFish_UseCBC 
          L(i-j)\q   = K1 ! Cipher(i-2)\q 
          L(i+1-j)\q = K2 ! Cipher(i-1)\q 
        Else 
          L(i-j)\q   = K1 
          L(i+1-j)\q = K2 
        EndIf 
      Next i 
      For i = 0 To n - 1 - j Step 2 
        K1 = L(i)\q & #FL 
        K2 = L(i + 1)\q & #FL 
        PokeL(*Result + a, PeekL(@K1)) 
        PokeL(*Result + a + 4, PeekL(@K2)) 
        a + 8 
      Next i 
    EndIf 
    
    FreeMemory(*Buffer) 
  EndIf 
  ProcedureReturn a 
  
EndProcedure 

Global Pfe$
Pfe$ = "d41d8cd98f00b204e9800998ecf8427e" ; encryption key based on md5 file

Procedure CryptBF(ToEnc$)
  
  ResultC$ = Space(1024) 
  flags.l = #BlowFish_UseCBC | #BlowFish_UseBase64
  lengthC = Blowfish_CryptText(@ToEnc$, Len(ToEnc$), @Pfe$, Len(Pfe$), @ResultC$, Len(ResultC$), flags) 
  If lengthC 
   SetGadgetText(#Text2, ResultC$)
   SetGadgetText(#Text1, "")
  ; Debug Len(ToEnc$)
   Else 
   MessageRequester("Error!","Encryption is failed",#MB_ICONEXCLAMATION)
  EndIf 
  
EndProcedure

Procedure DecryptBF(ToDec$)
  
  ResultD$ = Space(1024)
  flags.l = #BlowFish_UseCBC | #BlowFish_UseBase64
  lengthD = Blowfish_DecryptText(@ToDec$, Len(ToDec$), @Pfe$, Len(Pfe$), @ResultD$, Len(ResultD$), flags) 
  If lengthD 
   SetGadgetText(#Text1, ResultD$)
   SetGadgetText(#Text2, "")
  ; Debug Len(ToDec$)
    Else 
   MessageRequester("Error!","Decrypt is failed",#MB_ICONEXCLAMATION)
  EndIf 
  
EndProcedure

If OpenWindow(#WIN_TEST,0,0, 600, 210,"Window test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)=0 Or CreateGadgetList(WindowID(#WIN_TEST))=0  
    End 
  EndIf 
 
  StringGadget(#Text1, 10,10,580,20,"")
  ButtonGadget(#btnEnc, 250,50,100,20,"Crypt")
  
  StringGadget(#Text2, 10,90,580,20,"")
  ButtonGadget(#btnDec, 250,130,100,20,"Decrypt")
    
  Repeat
    Event = WaitWindowEvent() 
    If Event = #PB_Event_Gadget
      Select EventGadget() 
        
          Case #btnEnc 
           Enc$ = GetGadgetText(#Text1)
           If Enc$<>""
           Debug Enc$
           CryptBF(Enc$) 
           EndIf        
              
                       
          Case #btnDec 
           Dec$ = GetGadgetText(#Text2)
           If Dec$<>""
           Debug Dec$
           DecryptBF(Dec$)
           EndIf
                    

      EndSelect  
    EndIf 
  Until Event = #PB_Event_CloseWindow
  
  End 

Posted: Sat Apr 28, 2007 12:45 pm
by Kukulkan
Hi JCV,

Thank you very much for theese library's. Are they available for linux, too? This is actually the main need for me. Or are they available in source?

How about extending with the very common SHA hashes? SHA1 and SHA256 are widely used.

Is it possible to vary the bitlength of RSA (1024, 2048, 4192 bits)?

Can you add the generation of strong random RSA keys (1024, 2048, 4192 bits)?

Kukulkan

Posted: Sat Apr 28, 2007 2:55 pm
by Beach
Jacobus wrote:I have tested blowfish code in a little example and a problem is appear when i want try again. The first try is ok but the second is failed !?!
The same happens here. Thanks for posting this example.

Posted: Sat Apr 28, 2007 3:41 pm
by JCV
@Jacobus

you forgot to trim the result.
lengthD = Blowfish_DecryptText(@ToDec$, Len(ToDec$), @Pfe$, Len(Pfe$), @ResultD$, Len(ResultD$), flags)
If lengthD
SetGadgetText(#Text1, Left(ResultD$, lengthD))

Trim the decrypted output or else its padded with many space.
Try checking the end of the decrypted txt.

I'll try other encryptions if I have time ;)