Caesar cipher: Difference between revisions
Content added Content deleted
m (syntax highlighting fixed) |
(→{{header|Ada}}: Fix the example, which was totally broken) |
||
Line 383: | Line 383: | ||
</pre> |
</pre> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<syntaxhighlight lang="ada"> |
<syntaxhighlight lang="ada">-- Caesar Cipher Implementation in Ada |
||
with Ada.Text_IO; use Ada.Text_IO; |
|||
procedure Caesar is |
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 |
|||
Base_Pos : constant Natural := Character'Pos(Base); |
|||
begin |
begin |
||
return |
return Character'Val((Character'Pos(Char_To_Encrypt) + Shift - Base_Pos) mod 26 + Base_Pos); |
||
end |
end Cipher; |
||
-- 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 |
begin |
||
⚫ | |||
return Character'Val(Integer(Val)+Character'Pos(Output)); |
|||
⚫ | |||
⚫ | |||
-- Encrypt uppercase letters |
|||
return Cipher (Char_To_Encrypt, Shift, 'A'); |
|||
function crypt (Playn: String; Key: modulo26) return String is |
|||
when 'a'..'z' => |
|||
-- Encrypt lowercase letters |
|||
return Cipher (Char_To_Encrypt, Shift, 'a'); |
|||
⚫ | |||
-- Leave other characters unchanged |
|||
return Char_To_Encrypt; |
|||
⚫ | |||
end Encrypt_Char; |
|||
-- Function to decrypt a character |
|||
function Decrypt_Char(Char_To_Decrypt: Character; Shift: Integer) return Character is |
|||
begin |
begin |
||
return Encrypt_Char(Char_To_Decrypt, -Shift); |
|||
⚫ | |||
end Decrypt_Char; |
|||
⚫ | |||
⚫ | |||
Ciph(I) := Character(modulo26(Playn(I)+Key), 'A'); |
|||
when 'a' .. 'z' => |
|||
Ciph(I) := Character(modulo26(Playn(I)+Key), 'a'); |
|||
⚫ | |||
Ciph(I) := Playn(I); |
|||
⚫ | |||
⚫ | |||
return Ciph; |
|||
end crypt; |
|||
Message: constant String := Ada.Text_IO.Get_Line; |
|||
Shift: Positive := 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 |
|||
⚫ | |||
Encrypted_Message(I) := Encrypt_Char(Message(I), Shift); |
|||
⚫ | |||
-- 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); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
-- Display results |
|||
⚫ | |||
Put_Line("Plaintext: " & Message); |
|||
⚫ | |||
⚫ | |||
end Caesar; |
|||
⚫ | |||
{{out}} |
{{out}} |
||
<pre>> ./caesar |
<pre>> ./caesar |
||
The five boxing wizards jump quickly |
The five boxing wizards jump quickly |
||
Plaintext |
Plaintext: The five boxing wizards jump quickly |
||
Ciphertext |
Ciphertext: Wkh ilyh eralqj zlcdugv mxps txlfnob |
||
Decrypted Ciphertext |
Decrypted Ciphertext: The five boxing wizards jump quickly |
||
</pre> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{trans|Ada|Note: This specimen retains the original [[#Ada|Ada]] coding style.}} |
{{trans|Ada|Note: This specimen retains the original [[#Ada|Ada]] coding style.}} |