Page 1 of 2

simple encryption

Posted: Mon Jan 20, 2025 8:05 am
by moricode
you do not need a super complex world class security encryption for your simple office daily application's ,
unless you are working with the Pentagon .

for other case ,you can try this super easy , super fast tiny encryption method for some good medium security .

Code: Select all

Structure key 
   k.l[0]
EndStructure
DataSection
   key:
   Data.i $ABCDEFAB,$9876BCDE,$dcbaafe0
EndDataSection

Global *key.key
*key = ?key

Procedure enc(buf , size)
   n=size%4
   IF n 
      *n=ReAllocateMemory(buf,size + n)
   ENDIF   
   IF Not *n
      *n=buf
   ENDIF   
   b=size/4-1
   For i=0 To 2
      For p=0 To b
         j=*n+p<<2
         PokeL(j,PeekL(j)!*key\k[i])         
      Next
   Next
   IF n 
      *o=ReAllocateMemory(*n,size)
   ENDIF
   IF Not *o
      *o=*n
   ENDIF
   ProcedureReturn *o
EndProcedure

Procedure dec(buf,size)
   n=size%4
   IF n 
      *n=ReAllocateMemory(buf,size + n)
   ENDIF   
   IF Not *n
      *n=buf
   ENDIF
   b=size/4-1
   For i=2 To 0 Step -1
      For p=0 To b
         j=*n+p<<2
         PokeL(j,PeekL(j)!*key\k[i])         
      Next
   Next   
   IF n 
      *o=ReAllocateMemory(*n,size)
   ENDIF
   IF Not *o
      *o=*n
   ENDIF
   ProcedureReturn *o     
EndProcedure
; Example use:
A$="The quick brown fox jumps over the lazy dog 12 times a day"
L=Len(A$)<<1+2
*u=AllocateMemory(200)
PokeS(*u,A$)
Debug PeekS(*u)
Encrypt = enc(*u, L )
Debug PeekS( Encrypt)
Decrypt = dec(Encrypt, L)
Debug PeekS(Decrypt,L)


you could even make it smaller by using only one procedure to encrypt and decrypt , they are same actually.

Re: world class super encryption

Posted: Mon Jan 20, 2025 10:05 am
by BarryG
moricode wrote: Mon Jan 20, 2025 8:05 amyou could even make it smaller by using only one procedure to encrypt and decrypt , they are same actually.
Yep, so just make the "dec()" procedure like this:

Code: Select all

Procedure dec(buf,size)
  ProcedureReturn enc(buf,size)
EndProcedure

Re: world class super encryption

Posted: Tue Jan 21, 2025 10:31 pm
by Quin
Very nice, thanks for sharing!

Re: world class super encryption

Posted: Wed Jan 22, 2025 12:15 pm
by NicTheQuick
moricode wrote: Mon Jan 20, 2025 8:05 am you do not need a super complex world class security encryption for your simple office daily application's ,
unless you are working with the Pentagon .

for other case ,you can try this super easy , super fast tiny encryption method for some good medium security .
Sorry, but that's bullshit. Either you use proper encryption or you don't. There is nothing in between. Why do you want to tell people here that they only need such cheap encryption for their stuff?

Re: world class super encryption

Posted: Wed Jan 22, 2025 2:36 pm
by Caronte3D
Even if you intention was nice, with PB is very easy to use a good encryption method, so... IMHO, no need to use worse ones :wink:

Re: simple encryption

Posted: Wed Jan 22, 2025 7:39 pm
by miso
Though it's always nice to browse code in the forum. Thanks for sharing it. I myself use fast and minimal encryption for my unimportant data for things nobody would want to decrypt anyway. (and if they do, does not matter)

Re: simple encryption

Posted: Thu Jan 23, 2025 2:11 pm
by STARGÅTE
@moricode:

Your code has some bugs:
  1. On line 15, you re-allocate the memory "buf" to be a multiple of 4. However, "size + n" with "n=size%4" does not align on 4! If size is 5, n calculates to 1 and size+n become 6.
  2. Further, ReAllocateMemory requier a memory address created by AllocateMemory. The code crashes when "buf" is a different address.
  3. On line 18, you set "*n=buf" if the buffer can't be re-allocated. That doesn't make sense, does it, because then you forgot to encrypt the last bytes.
Nevertheless, I agree with NicTheQuick. Either you use a real encryption on nothing at all. And why do you thing. Further, I doubt that your encryption is "super fast", because typical encryption algorithms are nowadays implemented on hardware level:https://en.wikipedia.org/wiki/AES_instruction_set

Re: simple encryption

Posted: Thu Jan 23, 2025 9:35 pm
by SPH

Code: Select all

txt$="Coucou les amis, j'espère que vous allez bien !!!"
Debug txt$
key.b=250
crypt$=""
For i=1 To Len(txt$)
  a=Asc(Mid(txt$,i,1))
  a!key
  crypt$+Chr(a)
Next
Debug crypt$

Re: simple encryption

Posted: Fri Jan 24, 2025 12:27 am
by SPH
Other:

Code: Select all

Global txt$, txt2$, key.w

txt$="Coucou les amis, j'espère que vous allez bien !!!"

Procedure dec_enc(txt$,key.w)
  txt2$=""
  For i=1 To Len(txt$)
    a=Asc(Mid(txt$,i,1))
    a!key
  txt2$+Chr(a)
Next
Debug txt2$
EndProcedure

Debug txt$
dec_enc(txt$,149)
dec_enc(txt2$,149)


Re: world class super encryption

Posted: Fri Jan 24, 2025 2:57 am
by moricode
NicTheQuick wrote: Wed Jan 22, 2025 12:15 pm Sorry, but that's bullshit. Either you use proper encryption or you don't. There is nothing in between. Why do you want to tell people here that they only need such cheap encryption for their stuff?
dose forum means user can have difference idea and more discuss on variant code method ?
and some body can correct the bugs and raise more better code and more people can learn from insight from codes that never seen before ?

does we need brain storming ?

do you allow the chicken to grow up when it is still in the egg grader ?

what do you think ?

silent the junior user to bring up idea ?

Sorry , and with my million apologize .

Re: world class super encryption

Posted: Fri Jan 24, 2025 3:07 am
by moricode
Caronte3D wrote: Wed Jan 22, 2025 2:36 pm Even if you intention was nice, with PB is very easy to use a good encryption method, so... IMHO, no need to use worse ones :wink:
Intention is to invite more user to come out with their code example , either good one or worse one , junior was quiet and shy here , we must encourage more people on learning PB , is't it ?

junior coder are scare to show their code here because they scare to face the master coder .

Re: simple encryption

Posted: Fri Jan 24, 2025 3:37 am
by idle
I to would encourage people to post their code. just remember not to over talk it.
though I'm probably guilty of that myself.

Re: simple encryption

Posted: Fri Jan 24, 2025 3:54 am
by moricode
STARGÅTE wrote: Thu Jan 23, 2025 2:11 pm @moricode:

Your code has some bugs:
  1. On line 15, you re-allocate the memory "buf" to be a multiple of 4. However, "size + n" with "n=size%4" does not align on 4! If size is 5, n calculates to 1 and size+n become 6.
  2. Further, ReAllocateMemory requier a memory address created by AllocateMemory. The code crashes when "buf" is a different address.
  3. On line 18, you set "*n=buf" if the buffer can't be re-allocated. That doesn't make sense, does it, because then you forgot to encrypt the last bytes.
Nevertheless, I agree with NicTheQuick. Either you use a real encryption on nothing at all. And why do you thing. Further, I doubt that your encryption is "super fast", because typical encryption algorithms are nowadays implemented on hardware level:https://en.wikipedia.org/wiki/AES_instruction_set
Thanks for input and finding the bugs, @STARGÅTE

you are always a lovely master.

i try to do a "NOT Commonly XOR" routines , and it can't be decrypt by just XOR with key simply , for use with hobby software / utility/ application that dosen't matter if a world class hacker wanted to hack my soft, it is just a "food recipes database" or song player or some "life helper" or "office utility", not require the so complex and third party encryption libraries.

below is another version for you pleasure:

Code: Select all


Structure key 
   k.b[0]
EndStructure
DataSection
   key:
   Data.i $ABCDEFAB,$9876BCDE,$dcbaafe0  ; xor multiple loop with 3 key , key = 4 byte
EndDataSection

Global *key.key
*key = ?key

Procedure New_Enc_Dec(buf,size)
   b= size
   For i = 0 To 2
      b -1
      For p=0 To b
         key = *key\k[i]
         IF p%2
            key!*key\k[i+1]
         ENDIF
         j=buf+p
         PokeB(j,PeekB(j)!key)
      Next 
   Next
   ProcedureReturn buf
EndProcedure

; --------------------------------------------
; Example use: 
A$="The quick brown fox jumps over the lazy dog 12 times a day"
L=Len(A$)<<1+2
*u=AllocateMemory(200)
PokeS(*u,A$)
Debug PeekS(*u)
Debug "---- Encrypted ----"
Encrypt = New_Enc_dec(*u, L )

Debug PeekS( Encrypt)
Debug "----Decrypted ----"

Decrypt = New_Enc_Dec(*u, L )
Debug PeekS(Decrypt,L)


Re: simple encryption

Posted: Fri Jan 24, 2025 9:32 am
by STARGÅTE
It's a pleasure, indeed. Your new code has now other bugs:
  1. On line 15, "b -1", you decrease the loop limit each round by one byte. So the latter bytes are encrypted just one or two rounds, not three.
  2. The "key" structure is now a byte array and as your round-loop has only three rounds (plus an extra byte) you use only 4 bytes of your 12 bytes key!
  3. What is the sense of line 18-20, where you change the key itself, when the byte position is odd?

Re: world class super encryption

Posted: Fri Jan 24, 2025 10:03 am
by NicTheQuick
moricode wrote: Fri Jan 24, 2025 2:57 am
NicTheQuick wrote: Wed Jan 22, 2025 12:15 pm Sorry, but that's bullshit. Either you use proper encryption or you don't. There is nothing in between. Why do you want to tell people here that they only need such cheap encryption for their stuff?
dose forum means user can have difference idea and more discuss on variant code method ?
and some body can correct the bugs and raise more better code and more people can learn from insight from codes that never seen before ?

does we need brain storming ?

do you allow the chicken to grow up when it is still in the egg grader ?

what do you think ?

silent the junior user to bring up idea ?

Sorry , and with my million apologize .
I didn't look at your code but at your statement "you do not need a super complex world class security encryption for your simple office daily application's ,
unless you are working with the Pentagon."
That's what I meant with bullshit. Just don't tell people to use a custom encryption method when there are well known methods out there that are proven to be secure. :wink: