javascript - 如何从我的秘密消息字符串中形成一个矩形
问题描述
我正在创建一个密码应用程序,其中
"If man was meant to stay on the ground, god would have given us roots."
归一化为:
"ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots"
规范化的文本将形成一个矩形(r × c),其中c是列数,r是行数,使得c >= r和c - r <= 1,
因此,例如,规范化的文本是 54 个字符长,用c = 8 和r = 7指示一个矩形:
"ifmanwas"
"meanttos"
"tayonthe"
"groundgo"
"dwouldha"
"vegivenu"
"sroots "
然后通过向下读取从左到右的列来获得编码消息,并用于形成这样的编码块
"imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau"
编码的消息/密码文本形成一个矩形。
"imtgdvs"
"fearwer"
"mayoogo"
"anouuio"
"ntnnlvt"
"wttddes"
"aohghn "
"sseoau "
我已经对纯文本进行了规范化 ✅</p>
我无法制作规范化文本的矩形❌</p>
我没有将编码的代码分块分隔为单个字符串❌</p>
我已将编码消息写在一个矩形中✅</p>
const string = document.querySelector('#message');
const error = document.querySelector('#alert');
const encodeMessage = () => {
const message = string.value;
const normalisedText = message.replace(/[^\w]/g, "").toLowerCase();
const textCount = normalisedText.length;
const wordCount = message.split(" ").length;
const cols = Math.ceil(Math.sqrt(textCount));
const rows = Math.ceil(textCount / cols);
if (wordCount < 2 || message.length < 50) {
error.innerHTML = `You have ${textCount} character(s) and ${wordCount} word(s). Input must be more than one word and at Least 50 characters!`;
return false;
}
let cypherText = "";
for (let i = 0; i < cols; i ++) {
for (let j = i; j < normalisedText.length; j += cols) {
cypherText += normalisedText[j];
}
cypherText += '\n';
}
// document.querySelector('#normalized_rectangle').innerHTML = normalisedText in rectangle;
// document.querySelector('#encoded_chunks').innerHTML = cypherText as a single text with space in between;
document.querySelector('#encoded_rectangle').innerHTML = cypherText;
return cypherText;
}
<form>
<input type="text" placeholder="Type your secret message" id="message">
<button type="button" class="button" onclick="encodeMessage()">Encode message</button>
</form>
<p id="alert"></p>
<div class="box">
<h3>Normalised Rectangle</h3>
<p id="normalized_rectangle"></p>
</div>
<div class="box">
<h3>Encoded Chunks</h3>
<p id="encoded_chunks">
</p>
</div>
<div class="box">
<h3>Encoded Rectangle</h3>
<p id="encoded_rectangle">
</p>
</div>
解决方案
循环遍历字符串并使用子字符串制作长度为 8 的字符串,然后将其推入数组中,再次在数组上循环并制作新字符串
text = "ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots";
c = 8;
r = 7;
arr = [];
cipherText = "";
plainText = "";
index = 0;
for (i = 0; i < r; i++) {
arr.push(text.substring(index, index + 8))
index = index + 8;
plainText = plainText + arr[i]+"\n";
}
for (j = 0; j < c; j++) {
str = ""
for (k = 0; k < r; k++) {
if(arr[k][j] != undefined) str = str + arr[k][j];
}
cipherText = cipherText + str +"\t";
}
console.log(plainText);
console.log(cipherText);
推荐阅读
- sql - 当我的“WHERE CASE WHEN”评估为假时,我的 sql HAVING 子句似乎没有运行
- html - 我想使用键值管道而不从对象接收 id
- swiftui - 从环境变量初始化的 var 预览问题
- c# - 使用 C# 向生产者发送消息时如何在 kafka 消息中添加标头属性?
- c# - 有没有办法使用动画 gif 作为 ImageBrush 的 ImageSource?
- macos - 如何在 Mac OSX 上使用 IDLE 在 Python 3.7 中监控按键
- node.js - 如何真正使用 Postman 的授权?
- python - Html 图标不会显示在我网站上的某些页面上,但会显示在其他页面上
- c# - 不允许加载本地资源:ASP.NET Core 中的 file://
- flutter - 在颤动中显示一个数字作为幂或另一个数字