Spaces:
Running
Running
<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> | |