Encryption Quick'n'dirty

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
User avatar
stefano
Posts: 336
Joined: Tue Dec 11, 2012 8:24 am
Contact:

Encryption Quick'n'dirty

Post by stefano » Wed May 31, 2017 3:51 pm

This is a short experiment to use the Sinclair BASIC's RND function to encrypt text messages. Since it is able to predict the RND call results after specifying a 'seed' with the RAND command, we can use such well distributed sequence to create an ENIGMA-like shift sequence

I worked on a Spectrum but I think the code can be easily adapted on a ZX81.
The SEED size is a 16 bit number, so the encryption could require some extra tweak to get stronger ;)

5 INPUT "(E)ncrypt or (D)ecrypt ? ";e$
10 INPUT "Key ?",a
20 RANDOMIZE a
30 INPUT "Message ? ";a$
40 FOR x=1 TO LEN a$
50 LET a=CODE a$(x): GO SUB 100
55 PRINT CHR$ a;
60 NEXT x
70 STOP

100 IF a>90 THEN LET a=a-32
105 IF a<64 THEN RETURN
110 IF e$="e" THEN LET a=a+INT (RND*26)
120 IF e$="d" THEN LET a=a-INT (RND*26)
130 IF a>90 THEN LET a=a-26
140 IF a<65 THEN LET a=a+26
150 RETURN



.. for the record, a much more serious code is here but it was never ported to BASIC (and it is much bigger):
https://github.com/z88dk/z88dk/wiki/aes256

Moggy
Posts: 1507
Joined: Wed Jun 18, 2008 1:00 pm

Re: Encryption Quick'n'dirty

Post by Moggy » Wed May 31, 2017 4:48 pm

Nice to see someone else interested in encryption as ciphers and encryption are two of my passions especially on the 81.
I was encouraged in this after afternoon discussions many years ago with two late friends who were former Bletchley Park operatives during WW2.

One of them suggested that the RND function was not ideal and is easily broken and suggested a more "Chaotic" approach.

I have a few cipher programs from the net which I have altered to include shuffling functions, done first on the ZX80 then that code is transferred to the 81. Finally it's enciphered onto a one time pad created by the 81 which gives a triple encryption which I believe to be unbreakable.

Rather than use the RND function try something along the lines of this based on the logistics function..

LET X=L*x*(1-x)

for your seed/cipher generation where L= any number between 1 and 4 and X= any number <1
EG L=2.00 L= 3.5 and X=.2345 X=.678 etc. This then can be scaled up by say 100-1000 or whatever then use the 81's integer function to remove the fractionate element.
Use this iteratively keeping the value of L constant throughout the run.

The above function can also be used to make a more consistent RND number generator.


One thing to observe at this point is the initial value of L which is important to achieve randomness.
The included diagrams should tell you why so I'll let you work it out, substitute the diagrams "r=n" number function for the "L=n" used in my example.
For instance r=3.00 becomes L=3.00
Attachments
Bifurcation0.jpg
Bifurcation0.jpg (23.19 KiB) Viewed 211 times
bifurcation.gif
bifurcation.gif (67.22 KiB) Viewed 236 times

User avatar
stefano
Posts: 336
Joined: Tue Dec 11, 2012 8:24 am
Contact:

Re: Encryption Quick'n'dirty

Post by stefano » Wed May 31, 2017 8:05 pm

Love the idea of the logistic map !
.. if you want to see the map diagram on a zx81, this should work but hrg is strongly suggested: https://github.com/z88dk/z88dk/wiki/bifurc

I'm not a good math expert, so I really can't discuss the effectiveness in term of strength, I simply tried an alternate use of the ready made functions to get a fast and small program :) Once built and run I started thinking at its actual strength and.. well, the seed is 2 bytes long (at most), equivalent to about a 5 characters password.
By the way it is far better than a simple xor, and it is as much simple (not counting the fact that a xor operation is not available in BASIC).

Moggy
Posts: 1507
Joined: Wed Jun 18, 2008 1:00 pm

Re: Encryption Quick'n'dirty

Post by Moggy » Wed May 31, 2017 8:11 pm

stefano wrote:
Wed May 31, 2017 8:05 pm
Love the idea of the logistic map !
.. if you want to see the map diagram on a zx81, this should work but hrg is strongly suggested: https://github.com/z88dk/z88dk/wiki/bifurc

I'm not a good math expert, so I really can't discuss the effectiveness in term of strength, I simply tried an alternate use of the ready made functions to get a fast and small program :) Once built and run I started thinking at its actual strength and.. well, the seed is 2 bytes long (at most), equivalent to about a 5 characters password.
By the way it is far better than a simple xor, and it is as much simple (not counting the fact that a xor operation is not available in BASIC).
Not a criticism from me Stefano. :D

In fact I will add this to my cipher collection if that is ok with you?

User avatar
stefano
Posts: 336
Joined: Tue Dec 11, 2012 8:24 am
Contact:

Re: Encryption Quick'n'dirty

Post by stefano » Wed May 31, 2017 8:13 pm

+ the idea to use logistics for RND is very good, thank you!
Sadly my instinct pushes me in the opposite direction than accuracy, eg. by substituting Greek pi with 355/113 or 22/7 :)

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests