Anyone have an encryption library to share?

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

Anyone have an encryption library to share?

Post by jassing »

Looking to decode (this project) specifically blowfish. poked around, found some code that doesn't compile & another forum that never sent me an email validation link so I could see the files...

Hoping someone has either asm/pb code or a userlib or a dll that they could share.
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: Anyone have an encryption library to share?

Post by LuCiFeR[SD] »

This code by HeX0R still seems to work for me in PB4.51

Code: Select all

;/------------------------------
;|
;| BlowFish.pbi
;| (c)HeX0R 2006-2008
;|
;| Include File for
;| some BlowFish-Action.
;|
;| Needs PureBasic >= 4.0,
;| no Userlibrarys,
;| no Api
;| Should work with Demo also.
;|
;| Updated 5/2007
;| Now with Unicode-Support
;|
;| Updated 9/2008
;| Some performance enhancements
;|
;|
;/-------------------------------

Structure BLOWFISH
   K.l[18]
   S0.l[256]
   S1.l[256]
   S2.l[256]
   S3.l[256]
EndStructure

Structure BLOWFISHKEY
   b.b[16]
EndStructure

Structure BLOWFISHHELPER
   StructureUnion
      L.l[0]
      B.b[0]
   EndStructureUnion
EndStructure

#BlowFish_UseBase64 = $01
#BlowFish_UseCBC    = $02

Procedure.l BF_Internal_Sbox_Round(*L.BLOWFISHHELPER, *B.BLOWFISH)

   Protected Result.l

   Result = *B\S0[*L\B[3] & $FF] + *B\S1[*L\B[2] & $FF]
   Result ! *B\S2[*L\B[1] & $FF]
   Result + *B\S3[*L\B[0] & $FF]

   ProcedureReturn Result

EndProcedure


Procedure BF_Internal_Block_Encrypt(*vl.LONG, *vr.LONG, *B.BLOWFISH)

   Protected i.l

   While i < 16

      *vl\l ! *B\K[i]
      *vr\l ! BF_Internal_Sbox_Round(*vl, *B)
      Swap *vl\l, *vr\l
      i + 1
      
   Wend

   Swap *vl\l, *vr\l
   *vr\l ! *B\K[16]
   *vl\l ! *B\K[17]

EndProcedure

Procedure BF_Internal_Block_Decrypt(*vl.LONG, *vr.LONG, *B.BLOWFISH)

   Protected i.l = 17

   While i > 1

      *vl\l ! *B\K[i]
      *vr\l ! BF_Internal_Sbox_Round(*vl, *B)
      Swap *vl\l, *vr\l
      i - 1

   Wend

   Swap *vl\l, *vr\l
   *vr\l ! *B\K[1]
   *vl\l ! *B\K[0]

EndProcedure

Procedure BF_Internal_Cipher_Key(*key, keylen.l, *B.BLOWFISH)

   Protected key_md5.s, i.l, v0.l, v1.l, nkey.BLOWFISHKEY
   Dim MyKey.l(4)

   CopyMemory(?BLOWFISH_K, *B, 4168)
   
   key_md5 = MD5Fingerprint(*key, keylen)

   If keylen < 16
      CopyMemory(*key, @nkey\b[0], keylen)
      CopyMemory(@key_md5, @nkey\b[keylen], 16 - keylen)
   Else
      CopyMemory(*key, @nkey\b[0], 16)
   EndIf

   For i = 0 To 3
      MyKey(i) = PeekL(@nkey\b[i*4])
   Next i

   For i = 0 To 17
      *B\K[i] ! MyKey(i % 4)
   Next i

   For i = 0 To 16 Step 2
      BF_Internal_Block_Encrypt(@v0, @v1, *B)
      *B\K[i]   = v0
      *B\K[i+1] = v1
   Next i

   For i = 0 To 254 Step 2
      BF_Internal_Block_Encrypt(@v0, @v1, *B)
      *B\S0[i]   = v0
      *B\S0[i+1] = v1
   Next i

   For i = 0 To 254 Step 2
      BF_Internal_Block_Encrypt(@v0, @v1, *B)
      *B\S1[i]   = v0
      *B\S1[i+1] = v1
   Next i

   For i = 0 To 254 Step 2
      BF_Internal_Block_Encrypt(@v0, @v1, *B)
      *B\S2[i]   = v0
      *B\S2[i+1] = v1
   Next i

   For i = 0 To 254 Step 2
      BF_Internal_Block_Encrypt(@v0, @v1, *B)
      *B\S3[i]   = v0
      *B\S3[i+1] = v1
   Next i

EndProcedure


Procedure.l Blowfish_Crypt(*Source, SourceLength.l, *Password, PassLength.l, *Result, ResultLength.l, Mode.l = #BlowFish_UseBase64)

   Protected BF.BLOWFISH, *Buffer, i.l, a.l, P.l, b.l = 1, *T.BLOWFISHHELPER = AllocateMemory(SourceLength + 16), n.l
   Protected *S2

   BF_Internal_Cipher_Key(*Password, PassLength, @BF)
   
   If *T
      PokeQ(*T, SourceLength) ;<- Add real SourceLength
      CopyMemory(*Source, *T + 8, SourceLength)
      SourceLength + 8
      n = SourceLength

      *Buffer = AllocateMemory(SourceLength * 2)
      If *Buffer
         If n % 8
            n = 8 - (n % 8)
            CopyMemory(?BLOWFISH_HELPER, *T + SourceLength, n)
            SourceLength + n
         EndIf

         n = SourceLength / 4

         Dim Cipher.l(n + 2)

         If Mode & #BlowFish_UseCBC
            Cipher(0) = Date();
            Cipher(1) = ElapsedMilliseconds() * 1000000
            P = 2
         EndIf

         If n*8 <= ResultLength
            For i = 0 To n - 1 Step 2

               If Mode & #BlowFish_UseCBC
                  *T\L[i]   ! Cipher(b - 1)
                  *T\L[i+1] ! Cipher(b)
               EndIf

               Cipher(i+P)   = *T\L[i]
               Cipher(i+P+1) = *T\L[i+1]
               BF_Internal_Block_Encrypt(@Cipher(i + P), @Cipher(i + P + 1), @BF)
               b + 2
            Next i

            a = 0
            b = n + P
            i = 0
            While i < b
               PokeL(*Buffer + a, Cipher(i))
               PokeL(*Buffer + a + 4, Cipher(i + 1))
               a + 8
               i + 2
            Wend

            If Mode & #BlowFish_UseBase64
               Base64Encoder(*Buffer, a, *Result, ResultLength)
               a = MemoryStringLength(*Result) * SizeOf(CHARACTER)
            Else
               CopyMemory(*Buffer, *Result, a)
            EndIf
         EndIf
         FreeMemory(*Buffer)
      EndIf
      FreeMemory(*T)
   EndIf

   ProcedureReturn a

EndProcedure

Procedure.l Blowfish_Decrypt(*Source, SourceLength.l, *Password, PassLength.l, *Result, ResultLength.l, Mode.l = #BlowFish_UseBase64)

   Protected BF.BLOWFISH, n.l, i.l, a.l, j.l, K1.l, K2.l, *Buffer.BLOWFISHHELPER

   BF_Internal_Cipher_Key(*Password, PassLength, @BF)

   *Buffer = AllocateMemory(SourceLength * 2 + 78)
   If *Buffer
      If Mode & #BlowFish_UseBase64
         SourceLength = Base64Decoder(*Source, SourceLength, *Buffer, SourceLength * 2 + 78)
      Else
         CopyMemory(*Source, *Buffer, SourceLength)
      EndIf
      n = SourceLength / 4

      Dim L.l(n)

      If Mode & #BlowFish_UseCBC
         j = 2
      EndIf

      a = 0
      If n*8 <= ResultLength
         For i = j To n - 1 Step 2
            K1 = *Buffer\L[i]
            K2 = *Buffer\L[i+1]
            BF_Internal_Block_Decrypt(@K1, @K2, @BF)
            If Mode & #BlowFish_UseCBC
               L(i-j)   = K1 ! *Buffer\L[i-2]
               L(i+1-j) = K2 ! *Buffer\L[i-1]
            Else
               L(i-j)   = K1
               L(i+1-j) = K2
            EndIf
         Next i
         a = Round((n - 1 - j) / 2, 1) * 8
         CopyMemory(@L() + 8, *Result, a - 8)
         a = PeekQ(@L()) ;<- Get real Size
      EndIf
      FreeMemory(*Buffer)
   EndIf
   
   ProcedureReturn a
EndProcedure

DataSection
   BLOWFISH_K:
   Data.l $243F6A88, $85A308D3, $13198A2E, $03707344, $A4093822, $299F31D0, $082EFA98, $EC4E6C89
   Data.l $452821E6, $38D01377, $BE5466CF, $34E90C6C, $C0AC29B7, $C97C50DD, $3F84D5B5, $B5470917
   Data.l $9216D5D9, $8979FB1B, 0, 0

   ;S0:
   Data.l $D1310BA6, $98DFB5AC, $2FFD72DB, $D01ADFB7, $B8E1AFED, $6A267E96, $BA7C9045, $F12C7F99
   Data.l $24A19947, $B3916CF7, $0801F2E2, $858EFC16, $636920D8, $71574E69, $A458FEA3, $F4933D7E
   Data.l $0D95748F, $728EB658, $718BCD58, $82154AEE, $7B54A41D, $C25A59B5, $9C30D539, $2AF26013
   Data.l $C5D1B023, $286085F0, $CA417918, $B8DB38EF, $8E79DCB0, $603A180E, $6C9E0E8B, $B01E8A3E
   Data.l $D71577C1, $BD314B27, $78AF2FDA, $55605C60, $E65525F3, $AA55AB94, $57489862, $63E81440
   Data.l $55CA396A, $2AAB10B6, $B4CC5C34, $1141E8CE, $A15486AF, $7C72E993, $B3EE1411, $636FBC2A
   Data.l $2BA9C55D, $741831F6, $CE5C3E16, $9B87931E, $AFD6BA33, $6C24CF5C, $7A325381, $28958677
   Data.l $3B8F4898, $6B4BB9AF, $C4BFE81B, $66282193, $61D809CC, $FB21A991, $487CAC60, $5DEC8032
   Data.l $EF845D5D, $E98575B1, $DC262302, $EB651B88, $23893E81, $D396ACC5, $0F6D6FF3, $83F44239
   Data.l $2E0B4482, $A4842004, $69C8F04A, $9E1F9B5E, $21C66842, $F6E96C9A, $670C9C61, $ABD388F0
   Data.l $6A51A0D2, $D8542F68, $960FA728, $AB5133A3, $6EEF0B6C, $137A3BE4, $BA3BF050, $7EFB2A98
   Data.l $A1F1651D, $39AF0176, $66CA593E, $82430E88, $8CEE8619, $456F9FB4, $7D84A5C3, $3B8B5EBE
   Data.l $E06F75D8, $85C12073, $401A449F, $56C16AA6, $4ED3AA62, $363F7706, $1BFEDF72, $429B023D
   Data.l $37D0D724, $D00A1248, $DB0FEAD3, $49F1C09B, $075372C9, $80991B7B, $25D479D8, $F6E8DEF7
   Data.l $E3FE501A, $B6794C3B, $976CE0BD, $04C006BA, $C1A94FB6, $409F60C4, $5E5C9EC2, $196A2463
   Data.l $68FB6FAF, $3E6C53B5, $1339B2EB, $3B52EC6F, $6DFC511F, $9B30952C, $CC814544, $AF5EBD09
   Data.l $BEE3D004, $DE334AFD, $660F2807, $192E4BB3, $C0CBA857, $45C8740F, $D20B5F39, $B9D3FBDB
   Data.l $5579C0BD, $1A60320A, $D6A100C6, $402C7279, $679F25FE, $FB1FA3CC, $8EA5E9F8, $DB3222F8
   Data.l $3C7516DF, $FD616B15, $2F501EC8, $AD0552AB, $323DB5FA, $FD238760, $53317B48, $3E00DF82
   Data.l $9E5C57BB, $CA6F8CA0, $1A87562E, $DF1769DB, $D542A8F6, $287EFFC3, $AC6732C6, $8C4F5573
   Data.l $695B27B0, $BBCA58C8, $E1FFA35D, $B8F011A0, $10FA3D98, $FD2183B8, $4AFCB56C, $2DD1D35B
   Data.l $9A53E479, $B6F84565, $D28E49BC, $4BFB9790, $E1DDF2DA, $A4CB7E33, $62FB1341, $CEE4C6E8
   Data.l $EF20CADA, $36774C01, $D07E9EFE, $2BF11FB4, $95DBDA4D, $AE909198, $EAAD8E71, $6B93D5A0
   Data.l $D08ED1D0, $AFC725E0, $8E3C5B2F, $8E7594B7, $8FF6E2FB, $F2122B64, $8888B812, $900DF01C
   Data.l $4FAD5EA0, $688FC31C, $D1CFF191, $B3A8C1AD, $2F2F2218, $BE0E1777, $EA752DFE, $8B021FA1
   Data.l $E5A0CC0F, $B56F74E8, $18ACF3D6, $CE89E299, $B4A84FE0, $FD13E0B7, $7CC43B81, $D2ADA8D9
   Data.l $165FA266, $80957705, $93CC7314, $211A1477, $E6AD2065, $77B5FA86, $C75442F5, $FB9D35CF
   Data.l $EBCDAF0C, $7B3E89A0, $D6411BD3, $AE1E7E49, $00250E2D, $2071B35E, $226800BB, $57B8E0AF
   Data.l $2464369B, $F009B91E, $5563911D, $59DFA6AA, $78C14389, $D95A537F, $207D5BA2, $02E5B9C5
   Data.l $83260376, $6295CFA9, $11C81968, $4E734A41, $B3472DCA, $7B14A94A, $1B510052, $9A532915
   Data.l $D60F573F, $BC9BC6E4, $2B60A476, $81E67400, $08BA6FB5, $571BE91F, $F296EC6B, $2A0DD915
   Data.l $B6636521, $E7B9F9B6, $FF34052E, $C5855664, $53B02D5D, $A99F8FA1, $08BA4799, $6E85076A

   ;S1:
   Data.l $4B7A70E9, $B5B32944, $DB75092E, $C4192623, $AD6EA6B0, $49A7DF7D, $9CEE60B8, $8FEDB266
   Data.l $ECAA8C71, $699A17FF, $5664526C, $C2B19EE1, $193602A5, $75094C29, $A0591340, $E4183A3E
   Data.l $3F54989A, $5B429D65, $6B8FE4D6, $99F73FD6, $A1D29C07, $EFE830F5, $4D2D38E6, $F0255DC1
   Data.l $4CDD2086, $8470EB26, $6382E9C6, $021ECC5E, $09686B3F, $3EBAEFC9, $3C971814, $6B6A70A1
   Data.l $687F3584, $52A0E286, $B79C5305, $AA500737, $3E07841C, $7FDEAE5C, $8E7D44EC, $5716F2B8
   Data.l $B03ADA37, $F0500C0D, $F01C1F04, $0200B3FF, $AE0CF51A, $3CB574B2, $25837A58, $DC0921BD
   Data.l $D19113F9, $7CA92FF6, $94324773, $22F54701, $3AE5E581, $37C2DADC, $C8B57634, $9AF3DDA7
   Data.l $A9446146, $0FD0030E, $ECC8C73E, $A4751E41, $E238CD99, $3BEA0E2F, $3280BBA1, $183EB331
   Data.l $4E548B38, $4F6DB908, $6F420D03, $F60A04BF, $2CB81290, $24977C79, $5679B072, $BCAF89AF
   Data.l $DE9A771F, $D9930810, $B38BAE12, $DCCF3F2E, $5512721F, $2E6B7124, $501ADDE6, $9F84CD87
   Data.l $7A584718, $7408DA17, $BC9F9ABC, $E94B7D8C, $EC7AEC3A, $DB851DFA, $63094366, $C464C3D2
   Data.l $EF1C1847, $3215D908, $DD433B37, $24C2BA16, $12A14D43, $2A65C451, $50940002, $133AE4DD
   Data.l $71DFF89E, $10314E55, $81AC77D6, $5F11199B, $043556F1, $D7A3C76B, $3C11183B, $5924A509
   Data.l $F28FE6ED, $97F1FBFA, $9EBABF2C, $1E153C6E, $86E34570, $EAE96FB1, $860E5E0A, $5A3E2AB3
   Data.l $771FE71C, $4E3D06FA, $2965DCB9, $99E71D0F, $803E89D6, $5266C825, $2E4CC978, $9C10B36A
   Data.l $C6150EBA, $94E2EA78, $A5FC3C53, $1E0A2DF4, $F2F74EA7, $361D2B3D, $1939260F, $19C27960
   Data.l $5223A708, $F71312B6, $EBADFE6E, $EAC31F66, $E3BC4595, $A67BC883, $B17F37D1, $018CFF28
   Data.l $C332DDEF, $BE6C5AA5, $65582185, $68AB9802, $EECEA50F, $DB2F953B, $2AEF7DAD, $5B6E2F84
   Data.l $1521B628, $29076170, $ECDD4775, $619F1510, $13CCA830, $EB61BD96, $0334FE1E, $AA0363CF
   Data.l $B5735C90, $4C70A239, $D59E9E0B, $CBAADE14, $EECC86BC, $60622CA7, $9CAB5CAB, $B2F3846E
   Data.l $648B1EAF, $19BDF0CA, $A02369B9, $655ABB50, $40685A32, $3C2AB4B3, $319EE9D5, $C021B8F7
   Data.l $9B540B19, $875FA099, $95F7997E, $623D7DA8, $F837889A, $97E32D77, $11ED935F, $16681281
   Data.l $0E358829, $C7E61FD6, $96DEDFA1, $7858BA99, $57F584A5, $1B227263, $9B83C3FF, $1AC24696
   Data.l $CDB30AEB, $532E3054, $8FD948E4, $6DBC3128, $58EBF2EF, $34C6FFEA, $FE28ED61, $EE7C3C73
   Data.l $5D4A14D9, $E864B7E3, $42105D14, $203E13E0, $45EEE2B6, $A3AAABEA, $DB6C4F15, $FACB4FD0
   Data.l $C742F442, $EF6ABBB5, $654F3B1D, $41CD2105, $D81E799E, $86854DC7, $E44B476A, $3D816250
   Data.l $CF62A1F2, $5B8D2646, $FC8883A0, $C1C7B6A3, $7F1524C3, $69CB7492, $47848A0B, $5692B285
   Data.l $095BBF00, $AD19489D, $1462B174, $23820E00, $58428D2A, $0C55F5EA, $1DADF43E, $233F7061
   Data.l $3372F092, $8D937E41, $D65FECF1, $6C223BDB, $7CDE3759, $CBEE7460, $4085F2A7, $CE77326E
   Data.l $A6078084, $19F8509E, $E8EFD855, $61D99735, $A969A7AA, $C50C06C2, $5A04ABFC, $800BCADC
   Data.l $9E447A2E, $C3453484, $FDD56705, $0E1E9EC9, $DB73DBD3, $105588CD, $675FDA79, $E3674340
   Data.l $C5C43465, $713E38D8, $3D28F89E, $F16DFF20, $153E21E7, $8FB03D4A, $E6E39F2B, $DB83ADF7

   ;S2:
   Data.l $E93D5A68, $948140F7, $F64C261C, $94692934, $411520F7, $7602D4F7, $BCF46B2E, $D4A20068
   Data.l $D4082471, $3320F46A, $43B7D4B7, $500061AF, $1E39F62E, $97244546, $14214F74, $BF8B8840
   Data.l $4D95FC1D, $96B591AF, $70F4DDD3, $66A02F45, $BFBC09EC, $03BD9785, $7FAC6DD0, $31CB8504
   Data.l $96EB27B3, $55FD3941, $DA2547E6, $ABCA0A9A, $28507825, $530429F4, $0A2C86DA, $E9B66DFB
   Data.l $68DC1462, $D7486900, $680EC0A4, $27A18DEE, $4F3FFEA2, $E887AD8C, $B58CE006, $7AF4D6B6
   Data.l $AACE1E7C, $D3375FEC, $CE78A399, $406B2A42, $20FE9E35, $D9F385B9, $EE39D7AB, $3B124E8B
   Data.l $1DC9FAF7, $4B6D1856, $26A36631, $EAE397B2, $3A6EFA74, $DD5B4332, $6841E7F7, $CA7820FB
   Data.l $FB0AF54E, $D8FEB397, $454056AC, $BA489527, $55533A3A, $20838D87, $FE6BA9B7, $D096954B
   Data.l $55A867BC, $A1159A58, $CCA92963, $99E1DB33, $A62A4A56, $3F3125F9, $5EF47E1C, $9029317C
   Data.l $FDF8E802, $04272F70, $80BB155C, $05282CE3, $95C11548, $E4C66D22, $48C1133F, $C70F86DC
   Data.l $07F9C9EE, $41041F0F, $404779A4, $5D886E17, $325F51EB, $D59BC0D1, $F2BCC18F, $41113564
   Data.l $257B7834, $602A9C60, $DFF8E8A3, $1F636C1B, $0E12B4C2, $02E1329E, $AF664FD1, $CAD18115
   Data.l $6B2395E0, $333E92E1, $3B240B62, $EEBEB922, $85B2A20E, $E6BA0D99, $DE720C8C, $2DA2F728
   Data.l $D0127845, $95B794FD, $647D0862, $E7CCF5F0, $5449A36F, $877D48FA, $C39DFD27, $F33E8D1E
   Data.l $0A476341, $992EFF74, $3A6F6EAB, $F4F8FD37, $A812DC60, $A1EBDDF8, $991BE14C, $DB6E6B0D
   Data.l $C67B5510, $6D672C37, $2765D43B, $DCD0E804, $F1290DC7, $CC00FFA3, $B5390F92, $690FED0B
   Data.l $667B9FFB, $CEDB7D9C, $A091CF0B, $D9155EA3, $BB132F88, $515BAD24, $7B9479BF, $763BD6EB
   Data.l $37392EB3, $CC115979, $8026E297, $F42E312D, $6842ADA7, $C66A2B3B, $12754CCC, $782EF11C
   Data.l $6A124237, $B79251E7, $06A1BBE6, $4BFB6350, $1A6B1018, $11CAEDFA, $3D25BDD8, $E2E1C3C9
   Data.l $44421659, $0A121386, $D90CEC6E, $D5ABEA2A, $64AF674E, $DA86A85F, $BEBFE988, $64E4C3FE
   Data.l $9DBC8057, $F0F7C086, $60787BF8, $6003604D, $D1FD8346, $F6381FB0, $7745AE04, $D736FCCC
   Data.l $83426B33, $F01EAB71, $B0804187, $3C005E5F, $77A057BE, $BDE8AE24, $55464299, $BF582E61
   Data.l $4E58F48F, $F2DDFDA2, $F474EF38, $8789BDC2, $5366F9C3, $C8B38E74, $B475F255, $46FCD9B9
   Data.l $7AEB2661, $8B1DDF84, $846A0E79, $915F95E2, $466E598E, $20B45770, $8CD55591, $C902DE4C
   Data.l $B90BACE1, $BB8205D0, $11A86248, $7574A99E, $B77F19B6, $E0A9DC09, $662D09A1, $C4324633
   Data.l $E85A1F02, $09F0BE8C, $4A99A025, $1D6EFE10, $1AB93D1D, $0BA5A4DF, $A186F20F, $2868F169
   Data.l $DCB7DA83, $573906FE, $A1E2CE9B, $4FCD7F52, $50115E01, $A70683FA, $A002B5C4, $0DE6D027
   Data.l $9AF88C27, $773F8641, $C3604C06, $61A806B5, $F0177A28, $C0F586E0, $006058AA, $30DC7D62
   Data.l $11E69ED7, $2338EA63, $53C2DD94, $C2C21634, $BBCBEE56, $90BCB6DE, $EBFC7DA1, $CE591D76
   Data.l $6F05E409, $4B7C0188, $39720A3D, $7C927C24, $86E3725F, $724D9DB9, $1AC15BB4, $D39EB8FC
   Data.l $ED545578, $08FCA5B5, $D83D7CD3, $4DAD0FC4, $1E50EF5E, $B161E6F8, $A28514D9, $6C51133C
   Data.l $6FD5C7E7, $56E14EC4, $362ABFCE, $DDC6C837, $D79A3234, $92638212, $670EFA8E, $406000E0

   ;S3:
   Data.l $3A39CE37, $D3FAF5CF, $ABC27737, $5AC52D1B, $5CB0679E, $4FA33742, $D3822740, $99BC9BBE
   Data.l $D5118E9D, $BF0F7315, $D62D1C7E, $C700C47B, $B78C1B6B, $21A19045, $B26EB1BE, $6A366EB4
   Data.l $5748AB2F, $BC946E79, $C6A376D2, $6549C2C8, $530FF8EE, $468DDE7D, $D5730A1D, $4CD04DC6
   Data.l $2939BBDB, $A9BA4650, $AC9526E8, $BE5EE304, $A1FAD5F0, $6A2D519A, $63EF8CE2, $9A86EE22
   Data.l $C089C2B8, $43242EF6, $A51E03AA, $9CF2D0A4, $83C061BA, $9BE96A4D, $8FE51550, $BA645BD6
   Data.l $2826A2F9, $A73A3AE1, $4BA99586, $EF5562E9, $C72FEFD3, $F752F7DA, $3F046F69, $77FA0A59
   Data.l $80E4A915, $87B08601, $9B09E6AD, $3B3EE593, $E990FD5A, $9E34D797, $2CF0B7D9, $022B8B51
   Data.l $96D5AC3A, $017DA67D, $D1CF3ED6, $7C7D2D28, $1F9F25CF, $ADF2B89B, $5AD6B472, $5A88F54C
   Data.l $E029AC71, $E019A5E6, $47B0ACFD, $ED93FA9B, $E8D3C48D, $283B57CC, $F8D56629, $79132E28
   Data.l $785F0191, $ED756055, $F7960E44, $E3D35E8C, $15056DD4, $88F46DBA, $03A16125, $0564F0BD
   Data.l $C3EB9E15, $3C9057A2, $97271AEC, $A93A072A, $1B3F6D9B, $1E6321F5, $F59C66FB, $26DCF319
   Data.l $7533D928, $B155FDF5, $03563482, $8ABA3CBB, $28517711, $C20AD9F8, $ABCC5167, $CCAD925F
   Data.l $4DE81751, $3830DC8E, $379D5862, $9320F991, $EA7A90C2, $FB3E7BCE, $5121CE64, $774FBE32
   Data.l $A8B6E37E, $C3293D46, $48DE5369, $6413E680, $A2AE0810, $DD6DB224, $69852DFD, $09072166
   Data.l $B39A460A, $6445C0DD, $586CDECF, $1C20C8AE, $5BBEF7DD, $1B588D40, $CCD2017F, $6BB4E3BB
   Data.l $DDA26A7E, $3A59FF45, $3E350A44, $BCB4CDD5, $72EACEA8, $FA6484BB, $8D6612AE, $BF3C6F47
   Data.l $D29BE463, $542F5D9E, $AEC2771B, $F64E6370, $740E0D8D, $E75B1357, $F8721671, $AF537D5D
   Data.l $4040CB08, $4EB4E2CC, $34D2466A, $0115AF84, $E1B00428, $95983A1D, $06B89FB4, $CE6EA048
   Data.l $6F3F3B82, $3520AB82, $011A1D4B, $277227F8, $611560B1, $E7933FDC, $BB3A792B, $344525BD
   Data.l $A08839E1, $51CE794B, $2F32C9B7, $A01FBAC9, $E01CC87E, $BCC7D1F6, $CF0111C3, $A1E8AAC7
   Data.l $1A908749, $D44FBD9A, $D0DADECB, $D50ADA38, $0339C32A, $C6913667, $8DF9317C, $E0B12B4F
   Data.l $F79E59B7, $43F5BB3A, $F2D519FF, $27D9459C, $BF97222C, $15E6FC2A, $0F91FC71, $9B941525
   Data.l $FAE59361, $CEB69CEB, $C2A86459, $12BAA8D1, $B6C1075E, $E3056A0C, $10D25065, $CB03A442
   Data.l $E0EC6E0E, $1698DB3B, $4C98A0BE, $3278E964, $9F1F9532, $E0D392DF, $D3A0342B, $8971F21E
   Data.l $1B0A7441, $4BA3348C, $C5BE7120, $C37632D8, $DF359F8D, $9B992F2E, $E60B6F47, $0FE3F11D
   Data.l $E54CDA54, $1EDAD891, $CE6279CF, $CD3E7E6F, $1618B166, $FD2C1D05, $848FD2C5, $F6FB2299
   Data.l $F523F357, $A6327623, $93A83531, $56CCCD02, $ACF08162, $5A75EBB5, $6E163697, $88D273CC
   Data.l $DE966292, $81B949D0, $4C50901B, $71C65614, $E6C6C7BD, $327A140A, $45E1D006, $C3F27B9A
   Data.l $C9AA53FD, $62A80F00, $BB25BFE2, $35BDD2F6, $71126905, $B2040222, $B6CBCF7C, $CD769C2B
   Data.l $53113EC0, $1640E3D3, $38ABBD60, $2547ADF0, $BA38209C, $F746CE76, $77AFA1C5, $20756060
   Data.l $85CBFE4E, $8AE88DD8, $7AAAF9B0, $4CF9AA7E, $1948C25C, $02FB8A8C, $01C36AE4, $D6EBE1F9
   Data.l $90D4F869, $A65CDEA0, $3F09252D, $C208E69F, $B74E6132, $CE77E25B, $578FDFE3, $3AC372E6
   
   ;Dummys
   BLOWFISH_HELPER:
   Data.b 32, 32, 32, 32, 32, 32, 32, 32
EndDataSection

Code: Select all

IncludeFile "BlowFish.pbi"

;Example
text_to_encrypt$         = "Now we are checking if this stupid code will really work ...¿"
password_for_encryption$ = "0815"
Result$                  = Space(1024)
Flags.l                  = #BlowFish_UseCBC | #BlowFish_UseBase64
;#BlowFish_UseCBC    = Will always produce a different encrypted Text(or Memoryblock)
;#BlowFish_UseBase64 = Will encrypt the result in base64.

Length = Blowfish_Crypt(@text_to_encrypt$, StringByteLength(text_to_encrypt$), @password_for_encryption$, StringByteLength(password_for_encryption$), @Result$, StringByteLength(Result$), Flags)

If Length
   Debug Length
   Debug Result$

   text_to_decrypt$ = Result$
   Result$          = Space(1024)
   Length = Blowfish_Decrypt(@text_to_decrypt$, Length, @password_for_encryption$, StringByteLength(password_for_encryption$), @Result$, StringByteLength(Result$), Flags)
   If Length
      Debug Left(Result$, Length / SizeOf(CHARACTER))
   EndIf
EndIf

End
jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

Re: Anyone have an encryption library to share?

Post by jassing »

Thanks. indeed, that does work; but I guess the data is using a different flavour of blowfish;

When I decrypt the string, it's gibberish. (should be an xml file)

(my original problem with the code was not reading the code well enough to understand my decode buffer was too small)
User avatar
KJ67
Enthusiast
Enthusiast
Posts: 218
Joined: Fri Jun 26, 2009 3:51 pm
Location: Westernmost tip of Norway

Re: Anyone have an encryption library to share?

Post by KJ67 »

Are you compiling in ASCII-Mode?
The best preparation for tomorrow is doing your best today.
jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

Re: Anyone have an encryption library to share?

Post by jassing »

KJ67 wrote:Are you compiling in ASCII-Mode?
yes.
jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

Re: Anyone have an encryption library to share?

Post by jassing »

Here's the code I'm using, in case someone can see a problem...

Code: Select all

;- Header/includes
EnableExplicit
IncludePath "c:\src\purebasic"
XIncludeFile "blowfish.pbi"

;- Constants
#EncryptionKey = "FEDCBA9876543210"
;- Declare, only needed for testing
Declare.s DecodeFile( cFile.s )

;- Use readdate or readstring
#UseReadData = #True ; set to false to readstring()

; NOTE: For testing purposes, the data file contains a single line of text: (no space on either end nor cr/lf)
; fCnEnwQgtfDGyCYXIPgR6xHz+4Acna+UZjsGWHr9KxsEsTLs4inl/MifcEA3z0IFqYVQ3W5ANtnUhu1CIJ2bog==

Debug "******* BEGIN *********"
Define c$ = "EncryptedDataFile.txt"
If FileSize(c$)<=0
  Debug "Creating test file"
  Define fhnd = CreateFile(#PB_Any,c$)
  WriteString(fhnd,"fCnEnwQgtfDGyCYXIPgR6xHz+4Acna+UZjsGWHr9KxsEsTLs4inl/MifcEA3z0IFqYVQ3W5ANtnUhu1CIJ2bog==")
  CloseFile(fhnd)
EndIf

c$ = DecodeFile( c$ )
If c$ : Debug "RESULT: "+c$ : Else : Debug "Failed to decode/decrypt" : EndIf

;**********************************************************************************************************************************
;- Procedures
Procedure.s DecodeFile( cFile.s )
  Protected.i nFileSize = FileSize(cFile)
  Protected.l *RawData, *Base64Decoded, *BlowfishDecrypted, *Key
  Protected.i nMemoryBufferSize = 4096, nKeyLen = StringByteLength(#EncryptionKey)
  Protected.i nDecodedLength, nDecryptedLength
  Protected.l hndFile, nReadData
  Protected.s cDecryptedText = ""
  
  Debug "file: "+cFile
  
  If nFileSize > 0
    Debug "File size: "+Str(nFileSize)
    hndFile = ReadFile(#PB_Any,cFile)
    If hndFile
      
      ; read data file to memory
      *RawData = AllocateMemory( nFileSize + 1 )
      CompilerIf  #UseReadData
        nReadData = ReadData(hndFile, *RawData, nFileSize )
      CompilerElse
        Protected.s cLine = ReadString(hndFile)
        nReadData = StringByteLength(cLine)
        PokeS(*RawData,cLine)
      CompilerEndIf
      
      CloseFile(hndFile)
      
      If nReadData > 0
        Debug "Raw file data: "+PeekS(*RawData, nFileSize)
        
        ; Obtain the base64decoded text.
        *Base64Decoded = AllocateMemory( nMemoryBufferSize )
        nDecodedLength = Base64Decoder(*RawData,nFileSize,*Base64Decoded,nMemoryBufferSize)
        Debug "Decoded length: "+Str(nDecodedLength)
        
        If nDecodedLength > 0
          Debug "Base64decode: "+PeekS(*Base64Decoded, nDecodedLength)
          
          ; Put the key in memory..
          *Key = AllocateMemory( nMemoryBufferSize )
          PokeS(*key,#EncryptionKey,nKeyLen)
          
          ; now we need to decrypt using blowfish
          *BlowfishDecrypted = AllocateMemory( nFileSize )
          
          
          ;DO NOT USE #BlwoFish_UseBase64, we decode it ourselves.
          nDecryptedLength = Blowfish_Decrypt(*Base64Decoded, nDecodedLength, *Key, nKeyLen, *BlowfishDecrypted, nMemoryBufferSize,0); W/ or W/o it -- doesn't decode properly,#BlowFish_UseCBC )
          
          Debug "Decrypted Length: "+Str(nDecryptedLength)
          Debug "Decrypted data: "+PeekS(*BlowfishDecrypted)
          
          If nDecryptedLength > 0
            If LCase(PeekS(*BlowfishDecrypted,4)) = "<xml"
              cDecryptedText = PeekS(*BlowfishDecrypted, nDecryptedLength)
            Else
              Debug "Decrypted text not XML!"
            EndIf
          Else
            Debug "Failed to decrypt"
          EndIf
          
          FreeMemory(*Key)
          FreeMemory(*BlowfishDecrypted)
          
        Else
          Debug "Failed to decode"
        EndIf
        FreeMemory(*Base64Decoded)
      Else
        Debug "Failed to actually read any data"
      EndIf
      
      FreeMemory(*RawData) :
    EndIf
  Else
    Debug "File does not exist"
  EndIf
  ProcedureReturn cDecryptedText
EndProcedure
User avatar
charvista
Addict
Addict
Posts: 943
Joined: Tue Sep 23, 2008 11:38 pm
Location: Belgium

Re: Anyone have an encryption library to share?

Post by charvista »

@Jassing,
Maybe a bit late, but I have also an encryption / decryption algorithm for you. I just finished modifying the code from variable to memory, which is necessary because it can output nulls (Chr(0)), which is not possible with variables.
I hope you can use it. I think this code is RSA's RC4 compatible. I translated this code from another language a very long time ago, so I cannot give more information about this algorithm.
Encryption and decryption is the same syntax. The zCrypt() procedure simply converts the string with a key.
The other procedures are included here only to make the code working.

Code: Select all

Procedure.d zFpt(Value.d,RoundTo.i=2)
	ProcedureReturn ValD(StrD(Value-Int(Value),RoundTo))
EndProcedure

Procedure.s zRepeat(RepeatLen.i,String.s=" ")
    If RepeatLen<1
        ProcedureReturn ""
    EndIf
    If String=""
        String=" "
    EndIf
    Fill.s=ReplaceString(Space(RepeatLen)," ",String)
    ProcedureReturn Mid(Fill,1,RepeatLen); truncate several characters repetition
EndProcedure

Procedure.s zPad(StrExpr.s,NewLen.i,PadMethod.i=1,PadChr.s=" ")
    If PadChr=""
        PadChr=" "
    EndIf
    If Len(PadChr)>1
        PadChr=Left(PadChr,1); pad only with the first character
    EndIf
    StrExprLen.i=Len(StrExpr)
    Select PadMethod
        Case 1; Pad on Right [A-----] = Left justify  (Default)
            If NewLen<StrExprLen
                ProcedureReturn Left(StrExpr,NewLen); truncate
            Else
                ProcedureReturn StrExpr+zRepeat(NewLen-StrExprLen,PadChr); padding
            EndIf
        Case 2; Pad on Left [-----A] = Right justify
            If NewLen<StrExprLen
                ProcedureReturn Right(StrExpr,NewLen); truncate
            Else
                ProcedureReturn zRepeat(NewLen-StrExprLen,PadChr)+StrExpr; padding
            EndIf
        Case 3; Center [--A---]  (if inequal, more Left justification)
            If NewLen<StrExprLen
                ProcedureReturn Left(StrExpr,NewLen); truncate Left justify only (same as PadMethod=1)
            Else
                DiffLen.d=(NewLen-StrExprLen)/2
                If zFpt(DiffLen)
                    ProcedureReturn zRepeat(Int(DiffLen),PadChr)+StrExpr+zRepeat(Int(DiffLen)+1,PadChr)
                Else
                    ProcedureReturn zRepeat(DiffLen,PadChr)+StrExpr+zRepeat(DiffLen,PadChr)
                EndIf
            EndIf
        Default
            ProcedureReturn ""
    EndSelect
EndProcedure

Procedure.s zStringOffset(Var.s,Offset.i,Length.i,SubStr.s)
   If Var="" Or Offset<1 Or Length=0 Or SubStr=""
      MessageRequester("Error","Invalid Length in module zStringOffset")
      ProcedureReturn ""
   EndIf
   If Length<0
      ; Act like an editor (variable length)
      ProcedureReturn Mid(Var,1,Offset-1)+SubStr+Mid(Var,Offset+Abs(Length))
   Else
      ; Replace the exact given length (fixed length)
      If Len(SubStr)>Length
         SubStr=Mid(SubStr,1,Length)
      Else
         SubStr=zPad(SubStr,Length)
      EndIf
      ProcedureReturn Mid(Var,1,Offset-1)+SubStr+Mid(Var,Offset+Length)
   EndIf
EndProcedure

Procedure.s zXor(Byte1.i,Byte2.i)
    B1.s=RSet(Bin(Byte1),8,"0")
    B2.s=RSet(Bin(Byte2),8,"0")
    BXor.s=Space(8)
    For P=8 To 1 Step -1
        P1.s=Mid(B1,P,1)
        P2.s=Mid(B2,P,1)
        If P1=P2; 0=0 or 1=1
            BXor=zStringOffset(BXor,P,1,"0")
        Else
            BXor=zStringOffset(BXor,P,1,"1")
        EndIf
    Next P
    ProcedureReturn BXor.s
EndProcedure

Procedure zCrypt(*String,KeyC.s)
    Dim SboxA(255)
    Dim SboxB(255)
    For I=0 To 255
        SboxA(I)=I
    Next I
    J=1
    For I=0 To 255
        If J>Len(KeyC.s)
            J=1
        EndIf
        SboxB(I)=Asc(Mid(KeyC.s,J,1))
        J+1
    Next I
    J=0
    For I=0 To 255
        J=Mod(J+SboxA(I)+SboxB(I),256)
        P=SboxA(I)
        SboxA(I)=SboxA(J)
        SboxA(J)=P
    Next I
    I=0
    J=0
    *Duplicate=AllocateMemory(MemorySize(*String))
    CopyMemory(*String,*Duplicate,MemorySize(*String))
    For X=1 To MemorySize(*Duplicate)-1
        I=Mod(I+1,256)
        J=Mod(J+SboxA(I),256)
        P=SboxA(I)
        SboxA(I)=SboxA(J)
        SboxA(J)=P
        T=Mod(SboxA(i)+SboxA(J),256)
        K=SboxA(T)
        PokeA(*String+X-1,Val("%"+zXor(PeekA(*Duplicate+X-1),K)))
    Next X
    FreeMemory(*Duplicate)
EndProcedure



Pwd.s="þܺ?vT2"+Chr(16)
Word.s="This is a secret!"
*S=AllocateMemory(Len(Word.s)+1)
PokeS(*S,Word.s,Len(Word.s),#PB_Ascii)
Debug PeekS(*S,Len(Word.s),#PB_Ascii)
; ----- encrypt ---------------------
zCrypt(*S,Pwd) ; zMemDump(Win,*S,0)
Debug PeekS(*S,Len(Word.s),#PB_Ascii)
; ----- decrypt ---------------------
zCrypt(*S,Pwd) ; zMemDump(Win,*S,0)
Debug PeekS(*S,Len(Word.s),#PB_Ascii)
- Windows 11 Home 64-bit
- PureBasic 6.10 LTS (x64)
- 64 Gb RAM
- 13th Gen Intel(R) Core(TM) i9-13900K 3.00 GHz
- 5K monitor with DPI @ 200%
jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

Re: Anyone have an encryption library to share?

Post by jassing »

charvista wrote:@Jassing,
Maybe a bit late, but I have also an encryption / decryption algorithm for you. I just finished modifying the code from variable
Never too late ;-) But I am specifically looking for blowfish - I need to decrypt a string. So far the blowfish I've found isn't compatible with another library... and I can't use that library...
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Anyone have an encryption library to share?

Post by Kukulkan »

Hi,

I use CryptoSys API for that. It supports all main algorithms and is definitively standard-conform. It works quite well with purebasic.

You can find more information here http://www.cryptosys.net/#api

It is still commercial, but costs only US$169 (if you are a frequest cryptography user, this is worth every cent).

Kukulkan
jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

Re: Anyone have an encryption library to share?

Post by jassing »

Kukulkan wrote:Hi,

I use CryptoSys API for that. It supports all main algorithms and is definitively standard-conform. It works quite well with purebasic.

You can find more information here http://www.cryptosys.net/#api

It is still commercial, but costs only US$169 (if you are a frequest cryptography user, this is worth every cent).

Kukulkan
Thanks -- do you have any example pb code I could borrow for it?
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Anyone have an encryption library to share?

Post by Kukulkan »

Hi,
Thanks -- do you have any example pb code I could borrow for it?
Yes, but this covers only a subset of the functions. Please take it as example. You surely will need to dig deeper into, to get your preferred functions (bluefish).

Code: Select all

; -------------------------------------
; INITIALIZE CRYPTO-API's
; -------------------------------------
#CryptLibrary = 0

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    
  ; open Linux library

  PrototypeC  AES256_File(szFileOut, szFileIn, lpKey, fEncrypt, szMode, lpIV)
  PrototypeC  AES256_FileHex(szFileOut, szFileIn, szKey, fEncrypt, szMode, szIV)
  PrototypeC  AES256_Hex(szOutput, szInput, szKey, fEncrypt)
  PrototypeC  AES256_Init(lpKey, fEncrypt, szMode, lpIV)
  PrototypeC  AES256_InitHex(szKey, fEncrypt, szMode, szIV)
  PrototypeC  API_ErrorCode()
  PrototypeC  API_ErrorLookup(szOutput, nMaxChars, nErrCode)
  PrototypeC  API_Version()
  PrototypeC  MAC_Bytes(lpOutput, nOutLen, lpMessage, nMsgLen, lpKey, nKeyLen, nOptions)
  PrototypeC  MAC_HexFromBytes(szOutput, nMaxs, lpMessage, nMsgLen, lpKey, nKeyLen, nOptions)
  PrototypeC  MAC_HexFromHex(szOutput, nMaxs, szMsgHex, szKeyHex, nOptions)
  PrototypeC  PAD_BytesBlock(lpOutput, nOutputLen, lpInput, nBytes, nBlkLen, nOptions)
  PrototypeC  PAD_HexBlock(szOutput, nMaxs, szInput, nBlkLen, nOptions)
  PrototypeC  PAD_UnpadBytes(lpOutput, nOutputLen, lpInput, nBytes, nBlkLen, nOptions)
  PrototypeC  PAD_UnpadHex(szOutput, nMaxs, szInput, nBlkLen, nOptions)
  PrototypeC  RNG_KeyHex(szOutput, nMaxs, nBytes, lpSeed, nSeedLen)
  PrototypeC  SHA1_StringHexHash(szDigest, szMessage)
  PrototypeC  SHA2_FileHexHash(szDigest, szFileName, szMode)
  PrototypeC  SHA2_StringHexHash(szDigest, szMessage)
  PrototypeC  ZLIB_Deflate(lpOutput, nOutputLen, lpInput, nBytes)
  PrototypeC  ZLIB_Inflate(lpOutput, nOutputLen, lpInput, nBytes)
  
  Global AES256_File.AES256_File
  Global AES256_FileHex.AES256_FileHex
  Global AES256_Hex.AES256_Hex
  Global AES256_Init.AES256_Init
  Global AES256_InitHex.AES256_InitHex
  Global API_ErrorCode.API_ErrorCode
  Global API_ErrorLookup.API_ErrorLookup
  Global API_Version.API_Version
  Global MAC_Bytes.MAC_Bytes
  Global MAC_HexFromBytes.MAC_HexFromBytes
  Global MAC_HexFromHex.MAC_HexFromHex
  Global PAD_BytesBlock.PAD_BytesBlock
  Global PAD_HexBlock.PAD_HexBlock
  Global PAD_UnpadBytes.PAD_UnpadBytes
  Global PAD_UnpadHex.PAD_UnpadHex
  Global RNG_KeyHex.RNG_KeyHex
  Global SHA1_StringHexHash.SHA1_StringHexHash
  Global SHA2_FileHexHash.SHA2_FileHexHash
  Global SHA2_StringHexHash.SHA2_StringHexHash
  Global ZLIB_Deflate.ZLIB_Deflate
  Global ZLIB_Inflate.ZLIB_Inflate

  hDLL = OpenLibrary(#CryptLibrary, "libcryptosysapi.so")
  If hDLL <> 0
    AES256_File         = GetFunction(#CryptLibrary, "AES256_File")
    AES256_FileHex      = GetFunction(#CryptLibrary, "AES256_FileHex")
    AES256_Hex          = GetFunction(#CryptLibrary, "AES256_Hex")
    AES256_Init         = GetFunction(#CryptLibrary, "AES256_Init")
    AES256_InitHex      = GetFunction(#CryptLibrary, "AES256_InitHex")
    API_ErrorCode       = GetFunction(#CryptLibrary, "API_ErrorCode")
    API_ErrorLookup     = GetFunction(#CryptLibrary, "API_ErrorLookup")
    API_Version         = GetFunction(#CryptLibrary, "API_Version")
    MAC_Bytes           = GetFunction(#CryptLibrary, "MAC_Bytes")
    MAC_HexFromBytes    = GetFunction(#CryptLibrary, "MAC_HexFromBytes")
    MAC_HexFromHex      = GetFunction(#CryptLibrary, "MAC_HexFromHex")
    PAD_BytesBlock      = GetFunction(#CryptLibrary, "PAD_BytesBlock")
    PAD_HexBlock        = GetFunction(#CryptLibrary, "PAD_HexBlock")
    PAD_UnpadBytes      = GetFunction(#CryptLibrary, "PAD_UnpadBytes")
    PAD_UnpadHex        = GetFunction(#CryptLibrary, "PAD_UnpadHex")
    RNG_KeyHex          = GetFunction(#CryptLibrary, "RNG_KeyHex")
    SHA1_StringHexHash  = GetFunction(#CryptLibrary, "SHA1_StringHexHash")
    SHA2_FileHexHash    = GetFunction(#CryptLibrary, "SHA2_FileHexHash")
    SHA2_StringHexHash  = GetFunction(#CryptLibrary, "SHA2_StringHexHash")
    ZLIB_Deflate        = GetFunction(#CryptLibrary, "ZLIB_Deflate")
    ZLIB_Inflate        = GetFunction(#CryptLibrary, "ZLIB_Inflate")
  Else
    PrintN("Cant use libcryptosysapi.so. Please download the latest program-version and re-install.")
  EndIf
  
CompilerElse
  
  ; open Windows library
  
  Prototype  AES256_File(szFileOut, szFileIn, lpKey, fEncrypt, szMode, lpIV)
  Prototype  AES256_FileHex(szFileOut, szFileIn, szKey, fEncrypt, szMode, szIV)
  Prototype  AES256_Hex(szOutput, szInput, szKey, fEncrypt)
  Prototype  AES256_Init(lpKey, fEncrypt, szMode, lpIV)
  Prototype  AES256_InitHex(szKey, fEncrypt, szMode, szIV)
  Prototype  API_ErrorCode()
  Prototype  API_ErrorLookup(szOutput, nMaxChars, nErrCode)
  Prototype  API_Version()
  Prototype  MAC_Bytes(lpOutput, nOutLen, lpMessage, nMsgLen, lpKey, nKeyLen, nOptions)
  Prototype  MAC_HexFromBytes(szOutput, nMaxs, lpMessage, nMsgLen, lpKey, nKeyLen, nOptions)
  Prototype  MAC_HexFromHex(szOutput, nMaxs, szMsgHex, szKeyHex, nOptions)
  Prototype  PAD_BytesBlock(lpOutput, nOutputLen, lpInput, nBytes, nBlkLen, nOptions)
  Prototype  PAD_HexBlock(szOutput, nMaxs, szInput, nBlkLen, nOptions)
  Prototype  PAD_UnpadBytes(lpOutput, nOutputLen, lpInput, nBytes, nBlkLen, nOptions)
  Prototype  PAD_UnpadHex(szOutput, nMaxs, szInput, nBlkLen, nOptions)
  Prototype  RNG_KeyHex(szOutput, nMaxs, nBytes, lpSeed, nSeedLen)
  Prototype  SHA1_StringHexHash(szDigest, szMessage)
  Prototype  SHA2_FileHexHash(szDigest, szFileName, szMode)
  Prototype  SHA2_StringHexHash(szDigest, szMessage)
  Prototype  ZLIB_Deflate(lpOutput, nOutputLen, lpInput, nBytes)
  Prototype  ZLIB_Inflate(lpOutput, nOutputLen, lpInput, nBytes)
  
  Global AES256_File.AES256_File
  Global AES256_FileHex.AES256_FileHex
  Global AES256_Hex.AES256_Hex
  Global AES256_Init.AES256_Init
  Global AES256_InitHex.AES256_InitHex
  Global API_ErrorCode.API_ErrorCode
  Global API_ErrorLookup.API_ErrorLookup
  Global API_Version.API_Version
  Global MAC_Bytes.MAC_Bytes
  Global MAC_HexFromBytes.MAC_HexFromBytes
  Global MAC_HexFromHex.MAC_HexFromHex
  Global PAD_BytesBlock.PAD_BytesBlock
  Global PAD_HexBlock.PAD_HexBlock
  Global PAD_UnpadBytes.PAD_UnpadBytes
  Global PAD_UnpadHex.PAD_UnpadHex
  Global RNG_KeyHex.RNG_KeyHex
  Global SHA1_StringHexHash.SHA1_StringHexHash
  Global SHA2_FileHexHash.SHA2_FileHexHash
  Global SHA2_StringHexHash.SHA2_StringHexHash
  Global ZLIB_Deflate.ZLIB_Deflate
  Global ZLIB_Inflate.ZLIB_Inflate

  hDLL = OpenLibrary(#CryptLibrary, "diCryptoSys.dll")
  If hDLL <> 0
    AES256_File         = GetFunction(#CryptLibrary, "AES256_File")
    AES256_FileHex      = GetFunction(#CryptLibrary, "AES256_FileHex")
    AES256_Hex          = GetFunction(#CryptLibrary, "AES256_Hex")
    AES256_Init         = GetFunction(#CryptLibrary, "AES256_Init")
    AES256_InitHex      = GetFunction(#CryptLibrary, "AES256_InitHex")
    API_ErrorCode       = GetFunction(#CryptLibrary, "API_ErrorCode")
    API_ErrorLookup     = GetFunction(#CryptLibrary, "API_ErrorLookup")
    API_Version         = GetFunction(#CryptLibrary, "API_Version")
    MAC_Bytes           = GetFunction(#CryptLibrary, "MAC_Bytes")
    MAC_HexFromBytes    = GetFunction(#CryptLibrary, "MAC_HexFromBytes")
    MAC_HexFromHex      = GetFunction(#CryptLibrary, "MAC_HexFromHex")
    PAD_BytesBlock      = GetFunction(#CryptLibrary, "PAD_BytesBlock")
    PAD_HexBlock        = GetFunction(#CryptLibrary, "PAD_HexBlock")
    PAD_UnpadBytes      = GetFunction(#CryptLibrary, "PAD_UnpadBytes")
    PAD_UnpadHex        = GetFunction(#CryptLibrary, "PAD_UnpadHex")
    RNG_KeyHex          = GetFunction(#CryptLibrary, "RNG_KeyHex")
    SHA1_StringHexHash  = GetFunction(#CryptLibrary, "SHA1_StringHexHash")
    SHA2_FileHexHash    = GetFunction(#CryptLibrary, "SHA2_FileHexHash")
    SHA2_StringHexHash  = GetFunction(#CryptLibrary, "SHA2_StringHexHash")
    ZLIB_Deflate        = GetFunction(#CryptLibrary, "ZLIB_Deflate")
    ZLIB_Inflate        = GetFunction(#CryptLibrary, "ZLIB_Inflate")
  Else
    PrintN("Cant use diCryptoSys.dll. Please download the latest program-version and re-install.")
  EndIf
CompilerEndIf

; usage example

Procedure.s HashOfFile_SHA256(Filename.s)
  ; Calculates SHA256 hashcode of a file (returns in HEX)
  ; Returns "" in case of failure.
  Protected Digest.s, Erg.l, strMsg.s, lngRet.l
  
  Digest.s = Space(64)
  Erg.l = SHA2_FileHexHash(@Digest.s, @Filename.s, @"b") ; Binary-Mode!
  If Erg.l <> 0
    strMsg.s = Space(128)
    lngRet.l = API_ErrorLookup(@strMsg, Len(strMsg.s), Erg.l)
    PrintN("Error calculating hashcode of '" + Filename.s + "' (" + strMsg.s + ")!")
    ProcedureReturn ""
  Else
    ProcedureReturn UCase(Digest.s)
  EndIf
EndProcedure

Filename.s = "c:\somefiletohash.jpg"
Debug HashOfFile_SHA256(Filename.s)
Kukulkan
jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

Re: Anyone have an encryption library to share?

Post by jassing »

Thank you.
Post Reply