Spaces:
Running
Running
File size: 2,863 Bytes
f472903 8687baa f472903 3628cfa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文字列のエンコード/デコード</title>
<script>
function getXORValue() {
const xorInput = document.getElementById("xorValue");
return parseInt(xorInput.value) || 0; // 数値が無効な場合は0
}
function encodeString(str) {
const xorValue = getXORValue();
let encoded = str
.split("")
.map((char, ind) => {
if (ind % 2 !== 0) {
return String.fromCharCode(char.charCodeAt() ^ xorValue);
}
return char;
})
.join("");
return encodeURIComponent(encoded);
}
function decodeString(encodedStr) {
const xorValue = getXORValue();
let decodedStr = decodeURIComponent(encodedStr);
return decodedStr
.split("")
.map((char, ind) => {
if (ind % 2 !== 0) {
return String.fromCharCode(char.charCodeAt() ^ xorValue);
}
return char;
})
.join("");
}
function updateEncode() {
const inputStr = document.getElementById("inputStr").value;
const encodedStr = encodeString(inputStr);
document.getElementById("encodedStr").value = encodedStr;
}
function updateDecode() {
const encodedStr = document.getElementById("encodedStr").value;
const decodedStr = decodeString(encodedStr);
document.getElementById("inputStr").value = decodedStr;
}
window.onload = function() {
document.getElementById("inputStr").addEventListener("input", updateEncode);
document.getElementById("encodedStr").addEventListener("input", updateDecode);
document.getElementById("xorValue").addEventListener("input", () => {
updateEncode();
updateDecode();
});
}
</script>
</head>
<body>
<h1>文字列のエンコード/デコード</h1>
<p>example: https://soiz1-nebula.hf.space/~/uv/ ...</p>
<div>
<h3>エンコード / デコード</h3>
<label for="xorValue">XOR値 (数値):</label>
<input type="number" id="xorValue" value="3" />
<br><br>
<label for="inputStr">元の文字列:</label><br>
<textarea id="inputStr" rows="4" cols="50"></textarea>
<br><br>
<label for="encodedStr">エンコードされた文字列:</label><br>
<textarea id="encodedStr" rows="4" cols="50"></textarea>
</div>
</body>
</html>
|