|
Criptografia usando código morse |
Started by Boller, Mar., 28 2025 5:26 AM - No answer |
| |
| | | |
|
| |
Registered member 4,520 messages |
|
Posted on March, 28 2025 - 5:26 AM |
Hi,
Here’s the same Morse code encryption and decryption example written in WLanguage, but with English variable names and comments: // Morse Code Dictionary Initialization
PROCEDURE InitializeMorse()
GLOBAL gsMorse is a string gsMorse = [ "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=----."," =/" ]
// Function to encrypt text to Morse code
PROCEDURE EncryptToMorse(psText is a string) : string result is a string letter is a string i is an integer psText = UpperCase(psText)
FOR i = 1 TO Length(psText) letter = ExtractString(psText, i, 1) IF letter <> "" THEN index is an integer = Position(gsMorse, letter + "=") IF index > 0 THEN endPos is an integer = Position(gsMorse, ",", index) IF endPos = 0 THEN endPos = Length(gsMorse) + 1 END result += ExtractString(gsMorse, index + 2, endPos - index - 2) + " " ELSE result += letter + " " END END END RETURN Trim(result)
// Function to decrypt Morse code to text
PROCEDURE DecryptFromMorse(psMorse is a string) : string result is a string words is a buffer = SplitString(psMorse, " ") i is an integer
FOR i = 1 TO Length(words) IF words[i] <> "" THEN index is an integer = Position(gsMorse, "=" + words[i]) IF index > 0 THEN result += ExtractString(gsMorse, index - 1, 1) ELSE result += words[i] END END END RETURN result
// Test procedure for encryption/decryption PROCEDURE TestEncryption() originalText is a string = "HELLO WORLD" encryptedText is a string decryptedText is a string
InitializeMorse() encryptedText = EncryptToMorse(originalText) decryptedText = DecryptFromMorse(encryptedText)
Info("Original: " + originalText + CR + ... "Encrypted: " + encryptedText + CR + ... "Decrypted: " + decryptedText) Explanation of the Code: 1 InitializeMorse(): ◦ Sets up a global string (gsMorse) containing the Morse code mappings for letters, numbers, and space. ◦ Each pair is in the format “character=code”, separated by commas. 2 EncryptToMorse(): ◦ Converts the input text to uppercase. ◦ Iterates through each character: ▪ Looks up its Morse code in the dictionary. ▪ Appends the Morse code (or the original character if not found) to the result, followed by a space. ◦ Returns the trimmed Morse string. 3 DecryptFromMorse(): ◦ Splits the Morse string into individual codes using spaces. ◦ For each code: ▪ Finds the corresponding character in the dictionary. ▪ Builds the final text string. 4 TestEncryption(): ◦ Tests the functions with the example “HELLO WORLD”. ◦ Displays the original text, encrypted Morse code, and decrypted text. Expected Output: For “HELLO WORLD”: • Encrypted: .... . .-.. .-.. --- / .-- --- .-. .-.. -.. • Decrypted: HELLO WORLD Notes: • The space between words is represented by / in Morse code. • Unrecognized characters are preserved as-is in the output. • This is a basic encryption system; for enhanced security, you could combine it with additional techniques (e.g., character shifting or keys). Let me know if you need further modifications or a more advanced version!
-- Adriano José Boller ______________________________________________ Consultor e Representante Oficial da PcSoft no Brasil +55 (41) 99949 1800 adrianoboller@gmail.com skype: adrianoboller http://wxinformatica.com.br/ |
| |
| |
| | | |
|
| | | | |
| | |
|