Jump to content

Caesar cipher: Difference between revisions

→‎{{header|Ada}}: Fix the example, which was totally broken
m (syntax highlighting fixed)
(→‎{{header|Ada}}: Fix the example, which was totally broken)
Line 383:
</pre>
=={{header|Ada}}==
<syntaxhighlight lang="ada">with-- Caesar Cipher Implementation in Ada.Text_IO;
 
with Ada.Text_IO; use Ada.Text_IO;
 
procedure Caesar is
 
-- Base function to encrypt a character
type modulo26 is modulo 26;
function Cipher(Char_To_Encrypt: Character; Shift: Integer; Base: Character) return Character is
 
function modulo26 (Character Base_Pos : Character;constant Natural Output:= Character'Pos(Base) return modulo26 is;
begin
return modulo26 (Character'PosVal((Character)+Character'Pos(OutputChar_To_Encrypt) + Shift - Base_Pos) mod 26 + Base_Pos);
end modulo26Cipher;
 
-- Function to encrypt a character
function Character(Val: in modulo26; Output: Character)
function Encrypt_Char(Char_To_Encrypt: Character; Shift: Integer) return Character is
begin
case Playn(I)Char_To_Encrypt is
return Character'Val(Integer(Val)+Character'Pos(Output));
when 'A' .. 'Z' =>
end Character;
-- Encrypt uppercase letters
 
return Cipher (Char_To_Encrypt, Shift, 'A');
function crypt (Playn: String; Key: modulo26) return String is
Ciph: String(Playn when 'Range);a'..'z' =>
-- Encrypt lowercase letters
return Cipher (Char_To_Encrypt, Shift, 'a');
when others =>
-- Leave other characters unchanged
return Char_To_Encrypt;
end case;
end Encrypt_Char;
 
-- Function to decrypt a character
function Decrypt_Char(Char_To_Decrypt: Character; Shift: Integer) return Character is
begin
return Encrypt_Char(Char_To_Decrypt, -Shift);
for I in Playn'Range loop
end Decrypt_Char;
case Playn(I) is
when 'A' .. 'Z' =>
Ciph(I) := Character(modulo26(Playn(I)+Key), 'A');
when 'a' .. 'z' =>
Ciph(I) := Character(modulo26(Playn(I)+Key), 'a');
when others =>
Ciph(I) := Playn(I);
end case;
end loop;
return Ciph;
end crypt;
 
TextMessage: constant String := Ada.Text_IO.Get_Line;
KeyShift: modulo26Positive := 3; -- Default key from "Commentarii de Bello Gallico" shift cipher
-- Shift value (can be any positive integer)
 
Encrypted_Message: String(Message'Range);
begin -- encryption main program
Decrypted_Message: String(Message'Range);
begin
-- Encrypt the message
for I in PlaynMessage'Range loop
Encrypted_Message(I) := Encrypt_Char(Message(I), Shift);
end Characterloop;
 
-- Decrypt the encrypted message
Ada.Text_IO.Put_Line("Playn ------------>" & Text);
for I in Message'Range loop
Text := crypt(Text, Key);
Decrypted_Message(I) := Decrypt_Char(Encrypted_Message(I), Shift);
Ada.Text_IO.Put_Line("Ciphertext ----------->" & Text);
end loop;
Ada.Text_IO.Put_Line("Decrypted Ciphertext ->" & crypt(Text, -Key));
 
-- Display results
end Caesar;</syntaxhighlight>
Put_Line("Plaintext: " & Message);
Ada.Text_IO.Put_Line("Ciphertext: ----------->" & TextEncrypted_Message);
Ada.Text_IO.Put_Line("Decrypted Ciphertext: ->" & crypt(Text, -Key)Decrypted_Message);
 
end Caesar;
end Caesar;</syntaxhighlight>
{{out}}
<pre>> ./caesar
The five boxing wizards jump quickly
Plaintext: ------------>The five boxing wizards jump quickly
Ciphertext: ----------->Wkh ilyh eralqj zlcdugv mxps txlfnob
Decrypted Ciphertext: ->The five boxing wizards jump quickly</pre>
</pre>
 
=={{header|ALGOL 68}}==
{{trans|Ada|Note: This specimen retains the original [[#Ada|Ada]] coding style.}}
14

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.