How JavaScript Uses Caesar Cryptography in Mixed Japanese-English Strings String

Asked 1 years ago, Updated 1 years ago, 28 views

I asked you a question because I couldn't use JavaScript to shift Japanese.

Although the following code can be used to make English uppercase and lowercase characters, we are looking for a way to use Japanese (hiragana, katakana, and kanji) at the same time within function(c).

return decryptee.replace(/[a-zA-Z]/g, function(c){
    return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);
});

I thought I could execute it with the code below, but I couldn't.(The code below is written in hiragana only)

if(/[a-zA-Z]/g.test(decryptee)){
    return decryptee.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);
    });
} else if (/[Ah]/g.test(decryptee)) {
    return decryptee.replace(/[a-n]/g, function(c){
        return String.fromCharCode((c.charCodeAt(0)<=12426?12426:12438)>=(c=.charCodeAt(0)+13)?c:c-85);
   });
}

I would appreciate it if you could let me know if anyone knows how to solve this problem, references, etc.

Thank you for your cooperation.

Addition: (June 18, 2020 14:30)
Correct code and clarify unclear points.
Regarding the replacement at the time of comparison, we have changed it as follows.

vars=c.charCodeAt(0);
        return String.fromCharCode(s<=90?90:122)>=(s+13)?s+13:s-13);

unknown points

  • How to simultaneously replace Japanese English sentences or replace English sentences after Japanese
  • How to Replace a Surrogate Pair

I would like to ask you two questions.

Here's an excerpt from the action.

 varanomalyDict=[
  {
    title: "cube",
    first:"<oybpxdhbgr pynff='gvzre'>>c>o>6 Listen to the Japanese cypress <fcna pynff='ernq'>Dun</fcna>Menso</o>>>>>>cna>>>>>>
   // Originally, there is second, third after this, and there are several contents, but we omit them.
  },
];
var constant = [
    "aaa",
    "bbb",
    "cc",
    "</c><c>"
];
varanomalys=[];
for(leta=0;a<anomalyDict.length;a++){
    any [a] = [
     anomalyDict[a]['first'],
     constant[3],
     constant[0],
     constant[3],
    ] .join("");
}
vardecrypt=function(decryptee){
    console.log("Decrypting...");
    return decryptee.replace(/[a-zA-Z]/g, function(c){
    vars = c.charCodeAt(0);
        return String.fromCharCode(s<=90?90:122)>=(s+13)?s+13:s-13);
    });
};
 decryptedAnomaly=decrypt(anomaly[anomaly]);

Addition: (June 18, 2020 19:10)
In terms of processing,

//Rot13decode
vardecrypt=function(decryptee){
    console.log("Decrypting...");
    return decryptee.replace(/[a-zA-Z]/g, function(c){
        vars = c.charCodeAt(0);
        return String.fromCharCode(s<=90?90:122)>=(s+13)?s+13:s-13);
    }).replace(/[Ah]/g, function(c){
        vars = c.charCodeAt(0);
        return String.fromCharCode(s<=12425?s-13:s-73);
    });
};

Therefore, we were able to replace both hiragana and English in the current code.
In the same way, I am thinking of replacing katakana and kanji.

Unknown point 2

  • How to Replace a Surrogate Pair

I'd like to ask you only.
Thank you for your cooperation.

javascript

2022-09-30 19:37

1 Answers

Perhaps you are using a mixture of alphabets and Japanese in the string you are testing.In this case, the replacement of the alphabet will be returned, so the replacement of hiragana will not be done.

The other weak point is that we are using substitution when making comparisons.JavaScript (ECMAScript) determines the order in which the arguments for the operator are evaluated, but writing code that relies on this tends to be a hotbed of bugs.Unless you want to do short coding, you should avoid it.

In addition, if we want to extend this process to the entire kanji in the future, we may have to think about how to define "n-character post" in kanji.Some kanji characters cannot be represented by a single UTF-16 charCode (salogate pair).Of course, you are free to choose not to use such characters.


2022-09-30 19:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.