Crypt/Decrypt XSTR Strings with a number or list of numbers

Contributions to this software library are always welcome. Please ensure that you post program listings rather than .raw files. They give a reasonable idea of what your program does without having to load them into a DM42 and you can also include comments in your code. Check out the following link for a decoder/encoder: https://technical.swissmicros.com/decoders/dm42/

You can then copy/paste the listing and post it in "code" tags.
Post Reply
ame
Posts: 7
Joined: Sat Sep 17, 2022 4:42 pm

Crypt/Decrypt XSTR Strings with a number or list of numbers

Post by ame »

USAGE:

Stack Y: XSTR object
Stack X: a Number or LIST of Numbers

In the case where on the stack X register is a list (with numbers), the list is processed number by number from the head to tail.
The string will encrypted with the numbers one after the other


HINT: ASSIGN to CUSTOM: NEWLIST EXTEND form CATALOG/STR and XSTR from CATALOG/PRGM/
Your can use APPEND instead of EXTEND
On Newer Free42 (3.0.11) it is possible to append a number to a list with [+] key ;-)

Code: Select all

XSTR "TEXT"
2 SIN
XEQ "RndStr"
@DECRYPT
2 SIN
XEQ "RndStr"
or

Code: Select all

XSTR "TEXT"
NEWLIST
1 EXTEND 2 EXTEND 3 EXTEND
XEQ "RndStr"
@DECRYPT
NEWLIST
3 EXTEND 2 EXTEND 1 EXTEND
XEQ "RndStr"

or

Code: Select all

XSTR "TEXT"
1 XEQ "RndStr" 2 XEQ "RndStr" 3 XEQ "RndStr"
@Decrypt
3 XEQ "RndStr"
NEWLIST
2 APPEND 1 APPEND
XEQ "RndStr"
Source:

Code: Select all

00 { 85-Byte Prgm }
01▸LBL "RndStr"
02 LIST?
03 GTO B
@GTO A 

@List of numbers as "Secret"
04▸LBL A
05 FUNC 21
06 SEED
07 DROP
08 NEWSTR
@MAIN LOOP
09▸LBL 00
10 HEAD ST Y
@ SKIP <=> GTO a LBL a
11 GTO a
12 RTN
13▸LBL a
14 XEQ C
15 EXTEND
16 GTO 00

@List of numbers as "Secret"
17▸LBL B
18 FUNC 21
19 X<>Y
20▸LBL 01
21 HEAD ST Y
@ SKIP <=> GTO a LBL a
22 GTO b
23 RTN
24▸LBL b
25 XEQ A
26 GTO 01

@CRYPT Char
27▸LBL C
28 FUNC 11
29 C→N
30 RAN
31 256
32 ×
33 XOR
34 N→C
35 RTN
36 END

Attachments
Crypt.raw
(88 Bytes) Downloaded 128 times
redglyph
Posts: 177
Joined: Sat Dec 22, 2018 11:45 am

Re: Crypt/Decrypt XSTR Strings with a number or list of numbers

Post by redglyph »

I've learned a few things, thanks! :D

I think the comment should be '@ Single number as "Secret"' at step 03. ;)

It's a nice touch to use a sequence to change the key for each character. A word of caution though, LCRNG are well-known for being very easy to crack, so using a single number as key should be discarded right away (unless it's for fun of course). I think in the case of the HP-42S/HP-48/DM42, the random value is made from just two 8-bit numbers (RAN's statistical characteristics are not very good either).

A possible and challenging refinement would be to use a CBC mode instead of ECB (using the previous encrypted char), though I'm a bit rusty but I'm not sure that with a simple XOR it would reinforce it much.
Last edited by redglyph on Mon Sep 26, 2022 7:26 pm, edited 1 time in total.
Thomas Okken
Posts: 1098
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Crypt/Decrypt XSTR Strings with a number or list of numbers

Post by Thomas Okken »

redglyph wrote:
Mon Sep 26, 2022 10:14 am
I think in the case of the HP-42S/HP-48/DM42, the random value is made from just two 8-bit numbers (RAN's statistical characteristics are not very good either).
I can't speak to the statistical characteristics, but it uses a lot more than 16 bits:

https://groups.google.com/g/comp.sys.hp ... JTaWW0OeUJ
https://www.hpmuseum.org/forum/thread-9 ... 84260.html
redglyph
Posts: 177
Joined: Sat Dec 22, 2018 11:45 am

Re: Crypt/Decrypt XSTR Strings with a number or list of numbers

Post by redglyph »

Thomas Okken wrote:
Mon Sep 26, 2022 12:05 pm
redglyph wrote:
Mon Sep 26, 2022 10:14 am
I think in the case of the HP-42S/HP-48/DM42, the random value is made from just two 8-bit numbers (RAN's statistical characteristics are not very good either).
I can't speak to the statistical characteristics, but it uses a lot more than 16 bits:

https://groups.google.com/g/comp.sys.hp ... JTaWW0OeUJ
https://www.hpmuseum.org/forum/thread-9 ... 84260.html
That's interesting, perhaps I got the wrong part of code?

This is what I saw respectively in core_globals.cc and core_math2.cc. I admit the values in the code look rather odd.

Code: Select all

/* Random number generator */
int8 random_number_low, random_number_high; 

[...]

phloat math_random() {
    if (random_number_low == 0 && random_number_high == 0) {
        random_number_high = 0;
        random_number_low = 2787;
        // The RPL RAND/RDZ functions differ from the HP-42S RAN/SEED
        // only in their initial seed, which is this:
        // random_number_high = 9995003;
        // random_number_low = 33083533;
    }
    int8 temp = random_number_low * 30928467;
    random_number_high = (random_number_low * 28511 + random_number_high * 30928467 + temp / 100000000) % 10000000;
    random_number_low = temp % 100000000;
For the statistics, I saw for ex. this report.
Thomas Okken
Posts: 1098
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: Crypt/Decrypt XSTR Strings with a number or list of numbers

Post by Thomas Okken »

That int8 is an 8-byte integer. I don't think the int8_t naming convention had caught on very widely when I started working on Free42, and the notation I used was common in older code.
Last edited by Thomas Okken on Mon Sep 26, 2022 7:36 pm, edited 1 time in total.
redglyph
Posts: 177
Joined: Sat Dec 22, 2018 11:45 am

Re: Crypt/Decrypt XSTR Strings with a number or list of numbers

Post by redglyph »

Thomas Okken wrote:
Mon Sep 26, 2022 7:00 pm
That int8 is an 8-byte integer. I don't think the int8_y naming convention had caught on very widely when I started working on Free42, and the notation I used was common in older code.
Yeah, I should have realized that it would have been a poor pool of random values, even in the 80's.

Thanks!
Post Reply