|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Vigenère +/- Clé</title> |
| 6 | + <style> |
| 7 | + body { |
| 8 | + font-family: monospace; |
| 9 | + font-size: 14px; |
| 10 | + margin: 20px; |
| 11 | + background: #f5f5f5; |
| 12 | + } |
| 13 | + .container { |
| 14 | + max-width: 800px; |
| 15 | + margin: 0 auto; |
| 16 | + } |
| 17 | + h1 { |
| 18 | + text-align: center; |
| 19 | + } |
| 20 | + label { |
| 21 | + display: block; |
| 22 | + margin-top: 15px; |
| 23 | + font-weight: bold; |
| 24 | + } |
| 25 | + textarea, input { |
| 26 | + width: 100%; |
| 27 | + padding: 10px; |
| 28 | + font-family: monospace; |
| 29 | + font-size: 14px; |
| 30 | + border: 1px solid #999; |
| 31 | + box-sizing: border-box; |
| 32 | + } |
| 33 | + textarea { |
| 34 | + height: 80px; |
| 35 | + resize: vertical; |
| 36 | + } |
| 37 | + input { |
| 38 | + height: 40px; |
| 39 | + } |
| 40 | + .result { |
| 41 | + margin-top: 15px; |
| 42 | + padding: 15px; |
| 43 | + background: #fff; |
| 44 | + border: 2px solid #333; |
| 45 | + } |
| 46 | + .result h3 { |
| 47 | + margin: 0 0 10px 0; |
| 48 | + } |
| 49 | + .result p { |
| 50 | + word-wrap: break-word; |
| 51 | + margin: 0; |
| 52 | + } |
| 53 | + #resultPlus { |
| 54 | + background: #d4edda; |
| 55 | + border-color: #28a745; |
| 56 | + } |
| 57 | + #resultMinus { |
| 58 | + background: #fff3cd; |
| 59 | + border-color: #ffc107; |
| 60 | + } |
| 61 | + </style> |
| 62 | +</head> |
| 63 | +<body> |
| 64 | + |
| 65 | +<div class="container"> |
| 66 | + <h1>Vigenère +/- Clé</h1> |
| 67 | + |
| 68 | + <label for="texte">Texte chiffré :</label> |
| 69 | + <textarea id="texte" placeholder="Entrez le texte chiffré..."></textarea> |
| 70 | + |
| 71 | + <label for="cle">Clé :</label> |
| 72 | + <input type="text" id="cle" placeholder="Ex: CLE ou LEC"> |
| 73 | + |
| 74 | + <div class="result" id="resultPlus"> |
| 75 | + <h3>+ Clé (chiffrement)</h3> |
| 76 | + <p id="outputPlus">...</p> |
| 77 | + </div> |
| 78 | + |
| 79 | + <div class="result" id="resultMinus"> |
| 80 | + <h3>- Clé (déchiffrement)</h3> |
| 81 | + <p id="outputMinus">...</p> |
| 82 | + </div> |
| 83 | +</div> |
| 84 | + |
| 85 | +<script> |
| 86 | +function getShift(letter) { |
| 87 | + return letter.toUpperCase().charCodeAt(0) - 65; |
| 88 | +} |
| 89 | + |
| 90 | +function calculate() { |
| 91 | + const texte = document.getElementById('texte').value; |
| 92 | + const cle = document.getElementById('cle').value.toUpperCase().replace(/[^A-Z]/g, ''); |
| 93 | + |
| 94 | + if (!cle || cle.length === 0) { |
| 95 | + document.getElementById('outputPlus').textContent = '...'; |
| 96 | + document.getElementById('outputMinus').textContent = '...'; |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + let resultPlus = ''; |
| 101 | + let resultMinus = ''; |
| 102 | + let keyIndex = 0; |
| 103 | + |
| 104 | + for (let i = 0; i < texte.length; i++) { |
| 105 | + const c = texte[i]; |
| 106 | + |
| 107 | + if (c.match(/[a-zA-Z]/)) { |
| 108 | + const isUpper = c === c.toUpperCase(); |
| 109 | + const val = c.toUpperCase().charCodeAt(0) - 65; |
| 110 | + const shift = getShift(cle[keyIndex % cle.length]); |
| 111 | + |
| 112 | + const plusVal = (val + shift) % 26; |
| 113 | + const minusVal = (val - shift + 26) % 26; |
| 114 | + |
| 115 | + const plusChar = String.fromCharCode(plusVal + 65); |
| 116 | + const minusChar = String.fromCharCode(minusVal + 65); |
| 117 | + |
| 118 | + resultPlus += isUpper ? plusChar : plusChar.toLowerCase(); |
| 119 | + resultMinus += isUpper ? minusChar : minusChar.toLowerCase(); |
| 120 | + |
| 121 | + keyIndex++; |
| 122 | + } else { |
| 123 | + resultPlus += c; |
| 124 | + resultMinus += c; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + document.getElementById('outputPlus').textContent = resultPlus; |
| 129 | + document.getElementById('outputMinus').textContent = resultMinus; |
| 130 | +} |
| 131 | + |
| 132 | +document.getElementById('texte').addEventListener('input', calculate); |
| 133 | +document.getElementById('cle').addEventListener('input', calculate); |
| 134 | +</script> |
| 135 | + |
| 136 | +</body> |
| 137 | +</html> |
0 commit comments