Reading a text as json array

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
User avatar
Torlan
Kobold
Posts: 15
Joined: Fri Jan 07, 2022 8:12 am

Reading a text as json array

Post by Torlan »

Hello everyone, hope this finds you well.

I've been scavenging through the forums, but found nothing on the subject...please tell me if anything exists and I'll pull this off, thanks :wink:

The question is as simple as that: can a text be read as a JSON array?
Now, you might ask "Why on Earth you need that?". Well, I'm planning to build an in-game coder/decoder for the players to handle secret messages found during the campaign and such :mrgreen:

The idea is simple: get a text input, read it as an array then apply key-coding and return the coded messages. Same goes for decoding, more or less.

Any idea how to do that?
Roses are grey, violets are grey...I'm dead and colorblind!

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Reading a text as json array

Post by aliasmask »

Surely you can store your text in an array, but that part seems irrelevant. The method of encryption is where the guts of the problem. MT can generate random number but doesn't have an option to set a seed so you can generate "the same" random numbers. That is something I did a long time ago to encrypt text then undo it in a fancy looking way. (I was a big fan of the Sneakers movie). Depending on how you want your encrypted text to look, there are a lot of options. You can have a table of the top 10,000 most used works and use a word swap method for example.

User avatar
Torlan
Kobold
Posts: 15
Joined: Fri Jan 07, 2022 8:12 am

Re: Reading a text as json array

Post by Torlan »

aliasmask wrote:
Fri Jan 28, 2022 9:27 am
Surely you can store your text in an array, but that part seems irrelevant. The method of encryption is where the guts of the problem. MT can generate random number but doesn't have an option to set a seed so you can generate "the same" random numbers. That is something I did a long time ago to encrypt text then undo it in a fancy looking way. (I was a big fan of the Sneakers movie). Depending on how you want your encrypted text to look, there are a lot of options. You can have a table of the top 10,000 most used works and use a word swap method for example.
I see. Anyway the code I intend to use has not much to do with random numbers: it'll be a Vigenère code, in which you substitute a letter in the original message with that letter+the positional value of the corresponding letter of the key (kind of a shift encryption, with the shift value varying based on the key letter associated with the original message letter).

Here is a brief example:

Code: Select all

ORIGINAL MESSAGE : "HELLO"
KEY : "NOT"
You have to repeat the key until you cover the entire message
Thus, encrypting the first letter of the message will result in:

Code: Select all

"H" (positional value = 8) + "N" (positional value = 14) = 22 --> "V"
The full encrypted message will be (if I'm not mistaken, that is...):

Code: Select all

"VTFZD"

Of course all calculations will be mod 26 and you'll have to reverse the process for decrypting.
This is why I'd rather use array functions, so I can 'relate' each letter of the original message with the corresponding letter from the key.

Any hint?
Roses are grey, violets are grey...I'm dead and colorblind!

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Reading a text as json array

Post by aliasmask »

That seems easy enough. Just use a loop the size of your string. Did you want to preserve spacing and punctuation? I'm writing something up right now. Also, what about numbers?

User avatar
Torlan
Kobold
Posts: 15
Joined: Fri Jan 07, 2022 8:12 am

Re: Reading a text as json array

Post by Torlan »

aliasmask wrote:
Fri Jan 28, 2022 11:13 am
That seems easy enough. Just use a loop the size of your string. Did you want to preserve spacing and punctuation? I'm writing something up right now.
Yes, spaces and punctuation should not be affected by encryption. Moreover, uppercase/lowercase, both from message and from key, must not affect encryption.

And...well...thank you very much for taking the burden of writing code on this...way too cool :D
Roses are grey, violets are grey...I'm dead and colorblind!

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Reading a text as json array

Post by aliasmask »

This is what I have so far, bit tricky reversing it. I tried it with -mod, but that's not right.

Code: Select all

[H: string = "This is my 1st message."]
[H: key = "Secret"]
[H: reverse = 0]

<!-- generate key mods -->
[H: keyMods = "[]"]
[H, c(length(key)), code: {
   [H: index = roll.count]
   <!-- letters are expected, but can convert any character to a mod from 0 to 25 -->
   [H: mod = abs(math.mod(97 - number(eval("0x"+strformat("%h",lower(substring(key,index,index+1))))),26))]
   [H: keyMods = json.append(keyMods,mod)]
}]

[H: keyLength = json.length(keyMods)]

[H: newString = ""]
[H, c(length(string)), code: {
   [H: index = roll.count]
   [H: letter = substring(string,index,index+1)]
   [H: decimal = number(eval("0x"+strformat("%h",letter)))]
   [H: mod = json.get(keyMods,math.mod(index,keyLength))]
   [H: newChar = ""]
   <!-- upper case range -->
   [H, if(decimal >= 65 && decimal <= 90): newChar = decode("%" + strformat("%h",65 + math.mod(decimal - 65 + mod,26)))]
   <!-- lower case range -->
   [H, if(decimal >= 97 && decimal <= 122): newChar = decode("%" + strformat("%h",97 + math.mod(decimal - 97 + mod,26)))]
   <!-- numbers -->
   [H, if(decimal >= 48 && decimal <= 57): newChar = decode("%" + strformat("%h",48 + math.mod(decimal - 48 + mod,10)))]
   [H, if(json.isEmpty(newChar)): newChar = letter]
   
   [H: newString = newString + newChar]
}]

[R: newString]

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Reading a text as json array

Post by aliasmask »

okay, I got it.

Code: Select all

[H: string = "This is my 1st message."]
[H: string = "Llkj bk op 1kx dilkeiv."]
[H: key = "Secret"]
[H: reverse = 1]

<!-- generate key mods -->
[H: keyMods = "[]"]
[H, c(length(key)), code: {
   [H: index = roll.count]
   <!-- letters are expected, but can convert any character to a mod from 0 to 25 -->
   [H: mod = abs(math.mod(97 - number(eval("0x"+strformat("%h",lower(substring(key,index,index+1))))),26))]
   [H: keyMods = json.append(keyMods,mod)]
}]

[H: keyLength = json.length(keyMods)]

[H: newString = ""]
[H, c(length(string)), code: {
   [H: index = roll.count]
   [H: letter = substring(string,index,index+1)]
   [H: decimal = number(eval("0x"+strformat("%h",letter)))]
   [H: mod = json.get(keyMods,math.mod(index,keyLength))]
   [H, if(reverse): mod = -mod]
   [H: newChar = ""]
   <!-- upper case range -->
   [H, if(decimal >= 65 && decimal <= 90): newChar = decode("%" + strformat("%h",65 + math.mod(26 + math.mod(decimal - 65 + mod,26),26)))]
   <!-- lower case range -->
   [H, if(decimal >= 97 && decimal <= 122): newChar = decode("%" + strformat("%h",97 + math.mod(26 + math.mod(decimal - 97 + mod,26),26)))]
   <!-- numbers -->
   [H, if(decimal >= 48 && decimal <= 57): newChar = decode("%" + strformat("%h",48 + math.mod(10 + math.mod(decimal - 48 + mod,10),10)))]
   [H, if(json.isEmpty(newChar)): newChar = letter]
   
   [H: newString = newString + newChar]
}]

[R: newString]

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Reading a text as json array

Post by aliasmask »

I'll let you work out the detail on how to apply to your code.

User avatar
Torlan
Kobold
Posts: 15
Joined: Fri Jan 07, 2022 8:12 am

Re: Reading a text as json array

Post by Torlan »

aliasmask wrote:
Fri Jan 28, 2022 12:25 pm
okay, I got it.

Code: Select all

[H: string = "This is my 1st message."]
[H: string = "Llkj bk op 1kx dilkeiv."]
[H: key = "Secret"]
[H: reverse = 1]

<!-- generate key mods -->
[H: keyMods = "[]"]
[H, c(length(key)), code: {
   [H: index = roll.count]
   <!-- letters are expected, but can convert any character to a mod from 0 to 25 -->
   [H: mod = abs(math.mod(97 - number(eval("0x"+strformat("%h",lower(substring(key,index,index+1))))),26))]
   [H: keyMods = json.append(keyMods,mod)]
}]

[H: keyLength = json.length(keyMods)]

[H: newString = ""]
[H, c(length(string)), code: {
   [H: index = roll.count]
   [H: letter = substring(string,index,index+1)]
   [H: decimal = number(eval("0x"+strformat("%h",letter)))]
   [H: mod = json.get(keyMods,math.mod(index,keyLength))]
   [H, if(reverse): mod = -mod]
   [H: newChar = ""]
   <!-- upper case range -->
   [H, if(decimal >= 65 && decimal <= 90): newChar = decode("%" + strformat("%h",65 + math.mod(26 + math.mod(decimal - 65 + mod,26),26)))]
   <!-- lower case range -->
   [H, if(decimal >= 97 && decimal <= 122): newChar = decode("%" + strformat("%h",97 + math.mod(26 + math.mod(decimal - 97 + mod,26),26)))]
   <!-- numbers -->
   [H, if(decimal >= 48 && decimal <= 57): newChar = decode("%" + strformat("%h",48 + math.mod(10 + math.mod(decimal - 48 + mod,10),10)))]
   [H, if(json.isEmpty(newChar)): newChar = letter]
   
   [H: newString = newString + newChar]
}]

[R: newString]
Thanks mate, you’re just great!
I’ll wade through it and get the thing done, hopefully.
Roses are grey, violets are grey...I'm dead and colorblind!

User avatar
JoeDuncan
Giant
Posts: 118
Joined: Sun Nov 22, 2020 9:02 pm

Re: Reading a text as json array

Post by JoeDuncan »

aliasmask wrote:
Fri Jan 28, 2022 12:26 pm
I'll let you work out the detail on how to apply to your code.
Fast work dude, nice!
"Joe's Ugly Hacks - Get 'em while their hot! Guaranteed ugliest hacks around or your money back!"

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Reading a text as json array

Post by aliasmask »

JoeDuncan wrote:
Fri Jan 28, 2022 1:09 pm
aliasmask wrote:
Fri Jan 28, 2022 12:26 pm
I'll let you work out the detail on how to apply to your code.
Fast work dude, nice!
The key trick was converting the characters to values and back while keeping the result in the range of the categories, but I worked that out a long time ago here: viewtopic.php?f=20&t=13467&p=141858#p141858

Although, I did have to tweak the "convert ascii decimal to character" because I wanted the character an not the html entity.

User avatar
Torlan
Kobold
Posts: 15
Joined: Fri Jan 07, 2022 8:12 am

Re: Reading a text as json array

Post by Torlan »

aliasmask wrote:
Fri Jan 28, 2022 12:26 pm
I'll let you work out the detail on how to apply to your code.
I’m trying to get my head around your code and the role of c(...) is quite obscure to me. Can’t seem to find it in the wiki or forum at all.
Would you mind explaining how this function works?

Thanks in advance!
Roses are grey, violets are grey...I'm dead and colorblind!

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Reading a text as json array

Post by aliasmask »

Torlan wrote:
Sat Jan 29, 2022 5:00 am
aliasmask wrote:
Fri Jan 28, 2022 12:26 pm
I'll let you work out the detail on how to apply to your code.
I’m trying to get my head around your code and the role of c(...) is quite obscure to me. Can’t seem to find it in the wiki or forum at all.
Would you mind explaining how this function works?

Thanks in advance!
C is just a shorter name for count() which is listed in the roll options (link in left menu). https://wiki.rptools.info/index.php/Cat ... oll_Option

It's basically a for loop going from 0 to the listed number, so

Code: Select all

[H, for(index,0,length(key)), code: { ... }]
would do the same thing.

User avatar
Torlan
Kobold
Posts: 15
Joined: Fri Jan 07, 2022 8:12 am

Re: Reading a text as json array

Post by Torlan »

aliasmask wrote:
Sat Jan 29, 2022 9:14 am
Torlan wrote:
Sat Jan 29, 2022 5:00 am
aliasmask wrote:
Fri Jan 28, 2022 12:26 pm
I'll let you work out the detail on how to apply to your code.
I’m trying to get my head around your code and the role of c(...) is quite obscure to me. Can’t seem to find it in the wiki or forum at all.
Would you mind explaining how this function works?

Thanks in advance!
C is just a shorter name for count() which is listed in the roll options (link in left menu). https://wiki.rptools.info/index.php/Cat ... oll_Option

It's basically a for loop going from 0 to the listed number, so

Code: Select all

[H, for(index,0,length(key)), code: { ... }]
would do the same thing.

Cool, thank you! :D
Roses are grey, violets are grey...I'm dead and colorblind!

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Reading a text as json array

Post by aliasmask »

I deleted my last post. Mod would never be big because I wrapped it to limit the value so the reverse method would work. So, we're all good.

Post Reply

Return to “Macros”