Reading a text as json array
Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice
Reading a text as json array
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
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
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?
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
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
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!
Re: Reading a text as json array
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.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: Reading a text as json array
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).aliasmask wrote: ↑Fri Jan 28, 2022 9:27 amSurely 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.
Here is a brief example:
Code: Select all
ORIGINAL MESSAGE : "HELLO"
KEY : "NOT"
Thus, encrypting the first letter of the message will result in:
Code: Select all
"H" (positional value = 8) + "N" (positional value = 14) = 22 --> "V"
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!
Re: Reading a text as json array
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?
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: Reading a text as json array
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
Roses are grey, violets are grey...I'm dead and colorblind!
Re: Reading a text as json array
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]
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: Reading a text as json array
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]
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: Reading a text as json array
I'll let you work out the detail on how to apply to your code.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: Reading a text as json array
Thanks mate, you’re just great!aliasmask wrote: ↑Fri Jan 28, 2022 12:25 pmokay, 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]
I’ll wade through it and get the thing done, hopefully.
Roses are grey, violets are grey...I'm dead and colorblind!
Re: Reading a text as json array
Fast work dude, nice!
"Joe's Ugly Hacks - Get 'em while their hot! Guaranteed ugliest hacks around or your money back!"
Re: Reading a text as json array
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.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: Reading a text as json array
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!
Re: Reading a text as json array
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: { ... }]
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: Reading a text as json array
aliasmask wrote: ↑Sat Jan 29, 2022 9:14 amC 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
would do the same thing.Code: Select all
[H, for(index,0,length(key)), code: { ... }]
Cool, thank you!
Roses are grey, violets are grey...I'm dead and colorblind!
Re: Reading a text as json array
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.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools