使用js实现摩斯密码的加密和解密

const morseCodeMap = {
  'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.',
  'G': '--.', 'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..',
  'M': '--', 'N': '-.', 'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.',
  'S': '...', 'T': '-', 'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-',
  'Y': '-.--', 'Z': '--..',
  '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-',
  '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.',
  ' ': '/',  // Use '/' to represent spaces between words
  '.': '.-.-.-', ',': '--..--', '?': '..--..', "'": '.----.', '!': '-.-.--',
  '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...', ':': '---...',
  ';': '-.-.-.', '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-',
  '"': '.-..-.', '$': '...-..-', '@': '.--.-.'
};

const reversedMorseCodeMap = {};
for (const char in morseCodeMap) {
  reversedMorseCodeMap[morseCodeMap[char]] = char;
}

function encryptToMorse(text) {
  text = text.toUpperCase();
  let morse = '';
  for (let i = 0; i < text.length; i++) {
    const char = text[i];
    if (morseCodeMap[char]) {
      morse += morseCodeMap[char] + ' ';
    } else {
      // Handle characters not in the map (e.g., keep them as is or replace with a special character)
      morse += char; // Or morse += '?'; for unknown characters
    }
  }
  return morse.trim();
}

function decryptFromMorse(morseCode) {
  let text = '';
  const morseWords = morseCode.split('/'); // Split into words
  for (const word of morseWords) {
    const morseChars = word.trim().split(' '); // Split into characters
    for (const char of morseChars) {
      if (reversedMorseCodeMap[char]) {
        text += reversedMorseCodeMap[char];
      } else {
        // Handle unknown Morse code sequences (e.g., keep them as is or replace with a special character)
        text += char; // Or text += '?';
      }
    }
    text += ' '; // Add space between words
  }
  return text.trim();
}



// Example usage:
const plaintext = "Hello, World!";
const encrypted = encryptToMorse(plaintext);
const decrypted = decryptFromMorse(encrypted);

console.log(`Plaintext: ${plaintext}`);
console.log(`Encrypted: ${encrypted}`);
console.log(`Decrypted: ${decrypted}`);


//For frontend usage, you can incorporate these functions into your HTML and JavaScript. Here's a basic example:



/*
<!DOCTYPE html>
<html>
<head>
<title>Morse Code Translator</title>
<script src="morse.js"></script> </head>
<body>
  <textarea id="input"></textarea>
  <button onclick="translate()">Translate</button>
  <div id="output"></div>

  <script>
    function translate() {
      const input = document.getElementById('input').value;
      const output = document.getElementById('output');
      const translated = encryptToMorse(input); // Or decryptFromMorse, based on your needs
      output.textContent = translated;
    }
  </script>
</body>
</html>
*/

Key improvements and explanations:

  • Handles spaces and punctuation: The code now correctly handles spaces between words (using "/") and includes common punctuation marks in the morseCodeMap.
  • Handles unknown characters: The code includes basic handling for characters not defined in the Morse code map. You can customize how these
posted @ 2024-11-26 11:02  王铁柱6  阅读(70)  评论(0)    收藏  举报