simple encryption

Share your advanced PureBasic knowledge/code with the community.
moricode
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 25, 2023 3:55 am

simple encryption

Post 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.
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: world class super encryption

Post 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
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: world class super encryption

Post by Quin »

Very nice, thanks for sharing!
User avatar
NicTheQuick
Addict
Addict
Posts: 1517
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: world class super encryption

Post 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?
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: world class super encryption

Post 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:
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: simple encryption

Post 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)
User avatar
STARGÅTE
Addict
Addict
Posts: 2228
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: simple encryption

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
SPH
Enthusiast
Enthusiast
Posts: 566
Joined: Tue Jan 04, 2011 6:21 pm

Re: simple encryption

Post 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$

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
User avatar
SPH
Enthusiast
Enthusiast
Posts: 566
Joined: Tue Jan 04, 2011 6:21 pm

Re: simple encryption

Post 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)


!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
moricode
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 25, 2023 3:55 am

Re: world class super encryption

Post 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 .
moricode
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 25, 2023 3:55 am

Re: world class super encryption

Post 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 .
User avatar
idle
Always Here
Always Here
Posts: 5888
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: simple encryption

Post 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.
moricode
Enthusiast
Enthusiast
Posts: 162
Joined: Thu May 25, 2023 3:55 am

Re: simple encryption

Post 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)

User avatar
STARGÅTE
Addict
Addict
Posts: 2228
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: simple encryption

Post 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?
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
NicTheQuick
Addict
Addict
Posts: 1517
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: world class super encryption

Post 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:
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Post Reply