Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
58
Task/Caesar-cipher/Ada/caesar-cipher.adb
Normal file
58
Task/Caesar-cipher/Ada/caesar-cipher.adb
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
-- Caesar Cipher Implementation in Ada
|
||||
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Caesar is
|
||||
|
||||
-- Base function to encrypt a character
|
||||
function Cipher(Char_To_Encrypt: Character; Shift: Integer; Base: Character) return Character is
|
||||
Base_Pos : constant Natural := Character'Pos(Base);
|
||||
begin
|
||||
return Character'Val((Character'Pos(Char_To_Encrypt) + Shift - Base_Pos) mod 26 + Base_Pos);
|
||||
end Cipher;
|
||||
|
||||
-- Function to encrypt a character
|
||||
function Encrypt_Char(Char_To_Encrypt: Character; Shift: Integer) return Character is
|
||||
begin
|
||||
case Char_To_Encrypt is
|
||||
when 'A'..'Z' =>
|
||||
-- Encrypt uppercase letters
|
||||
return Cipher (Char_To_Encrypt, Shift, 'A');
|
||||
when '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);
|
||||
end Decrypt_Char;
|
||||
|
||||
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);
|
||||
Decrypted_Message: String(Message'Range);
|
||||
begin
|
||||
-- Encrypt the message
|
||||
for I in Message'Range loop
|
||||
Encrypted_Message(I) := Encrypt_Char(Message(I), Shift);
|
||||
end loop;
|
||||
|
||||
-- Decrypt the encrypted message
|
||||
for I in Message'Range loop
|
||||
Decrypted_Message(I) := Decrypt_Char(Encrypted_Message(I), Shift);
|
||||
end loop;
|
||||
|
||||
-- Display results
|
||||
Put_Line("Plaintext: " & Message);
|
||||
Put_Line("Ciphertext: " & Encrypted_Message);
|
||||
Put_Line("Decrypted Ciphertext: " & Decrypted_Message);
|
||||
|
||||
end Caesar;
|
||||
32
Task/Caesar-cipher/AutoIt/caesar-cipher.au3
Normal file
32
Task/Caesar-cipher/AutoIt/caesar-cipher.au3
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
$Caesar = Caesar("Hi", 2, True)
|
||||
MsgBox(0, "Caesar", $Caesar)
|
||||
Func Caesar($String, $int, $encrypt = True)
|
||||
If Not IsNumber($int) Or Not StringIsDigit($int) Then Return SetError(1, 0, 0)
|
||||
If $int < 1 Or $int > 25 Then Return SetError(2, 0, 0)
|
||||
Local $sLetters, $x
|
||||
$String = StringUpper($String)
|
||||
$split = StringSplit($String, "")
|
||||
For $i = 1 To $split[0]
|
||||
If Asc($split[$i]) - 64 > 26 Or Asc($split[$i]) - 64 < 1 Then
|
||||
$sLetters &= $split[$i]
|
||||
ContinueLoop
|
||||
EndIf
|
||||
If $encrypt = True Then
|
||||
$move = Asc($split[$i]) - 64 + $int
|
||||
Else
|
||||
$move = Asc($split[$i]) - 64 - $int
|
||||
EndIf
|
||||
If $move > 26 Then
|
||||
$move -= 26
|
||||
ElseIf $move < 1 Then
|
||||
$move += 26
|
||||
EndIf
|
||||
While $move
|
||||
$x = Mod($move, 26)
|
||||
If $x = 0 Then $x = 26
|
||||
$sLetters &= Chr($x + 64)
|
||||
$move = ($move - $x) / 26
|
||||
WEnd
|
||||
Next
|
||||
Return $sLetters
|
||||
EndFunc ;==>Caesar
|
||||
36
Task/Caesar-cipher/COBOL/caesar-cipher-1.cob
Normal file
36
Task/Caesar-cipher/COBOL/caesar-cipher-1.cob
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. CAESAR.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 MSG PIC X(50)
|
||||
VALUE "The quick brown fox jumped over the lazy dog.".
|
||||
01 OFFSET PIC 9(4) VALUE 7 USAGE BINARY.
|
||||
01 FROM-CHARS PIC X(52).
|
||||
01 TO-CHARS PIC X(52).
|
||||
01 TABL.
|
||||
02 PIC X(26) VALUE "abcdefghijklmnopqrstuvwxyz".
|
||||
02 PIC X(26) VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||
02 PIC X(26) VALUE "abcdefghijklmnopqrstuvwxyz".
|
||||
02 PIC X(26) VALUE "ABCDEFGHIJKLMNOPQRSTUVWXYZ".
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
BEGIN.
|
||||
DISPLAY MSG
|
||||
PERFORM ENCRYPT
|
||||
DISPLAY MSG
|
||||
PERFORM DECRYPT
|
||||
DISPLAY MSG
|
||||
STOP RUN.
|
||||
|
||||
ENCRYPT.
|
||||
MOVE TABL (1:52) TO FROM-CHARS
|
||||
MOVE TABL (1 + OFFSET:52) TO TO-CHARS
|
||||
INSPECT MSG CONVERTING FROM-CHARS TO TO-CHARS.
|
||||
|
||||
DECRYPT.
|
||||
MOVE TABL (1 + OFFSET:52) TO FROM-CHARS
|
||||
MOVE TABL (1:52) TO TO-CHARS
|
||||
INSPECT MSG CONVERTING FROM-CHARS TO TO-CHARS.
|
||||
|
||||
END PROGRAM CAESAR.
|
||||
84
Task/Caesar-cipher/COBOL/caesar-cipher-2.cob
Normal file
84
Task/Caesar-cipher/COBOL/caesar-cipher-2.cob
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
>>SOURCE FORMAT IS FREE
|
||||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. caesar-cipher.
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
REPOSITORY.
|
||||
FUNCTION encrypt
|
||||
FUNCTION decrypt.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 plaintext PIC X(50).
|
||||
01 offset USAGE BINARY-CHAR.
|
||||
01 encrypted-str PIC X(50).
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
DISPLAY "Enter a message to encrypt: " WITH NO ADVANCING
|
||||
ACCEPT plaintext
|
||||
DISPLAY "Enter the amount to shift by: " WITH NO ADVANCING
|
||||
ACCEPT offset
|
||||
MOVE encrypt(offset, plaintext) TO encrypted-str
|
||||
DISPLAY "Encrypted: " encrypted-str
|
||||
DISPLAY "Decrypted: " decrypt(offset, encrypted-str).
|
||||
|
||||
END PROGRAM caesar-cipher.
|
||||
|
||||
IDENTIFICATION DIVISION.
|
||||
FUNCTION-ID. encrypt.
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
REPOSITORY.
|
||||
FUNCTION ALL INTRINSIC.
|
||||
|
||||
DATA DIVISION.
|
||||
LOCAL-STORAGE SECTION.
|
||||
01 i USAGE INDEX.
|
||||
01 a USAGE BINARY-CHAR.
|
||||
LINKAGE SECTION.
|
||||
01 offset USAGE BINARY-CHAR.
|
||||
01 str PIC X(50).
|
||||
01 encrypted-str PIC X(50).
|
||||
|
||||
PROCEDURE DIVISION USING offset, str RETURNING encrypted-str.
|
||||
PERFORM VARYING i FROM 1 BY 1 UNTIL i > LENGTH(str)
|
||||
IF str(i:1) IS NOT ALPHABETIC OR str(i:1) = SPACE
|
||||
MOVE str(i:1) TO encrypted-str(i:1)
|
||||
EXIT PERFORM CYCLE
|
||||
END-IF
|
||||
IF str(i:1) IS ALPHABETIC-UPPER
|
||||
MOVE ORD("A") TO a
|
||||
ELSE
|
||||
MOVE ORD("a") TO a
|
||||
END-IF
|
||||
MOVE CHAR(MOD(ORD(str(i:1)) - a + offset, 26) + a)
|
||||
TO encrypted-str(i:1)
|
||||
END-PERFORM
|
||||
EXIT FUNCTION.
|
||||
|
||||
END FUNCTION encrypt.
|
||||
|
||||
IDENTIFICATION DIVISION.
|
||||
FUNCTION-ID. decrypt.
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
REPOSITORY.
|
||||
FUNCTION encrypt.
|
||||
|
||||
DATA DIVISION.
|
||||
LOCAL-STORAGE SECTION.
|
||||
01 decrypt-offset USAGE BINARY-CHAR.
|
||||
LINKAGE SECTION.
|
||||
01 offset USAGE BINARY-CHAR.
|
||||
01 str PIC X(50).
|
||||
01 decrypted-str PIC X(50).
|
||||
|
||||
PROCEDURE DIVISION USING offset, str RETURNING decrypted-str.
|
||||
SUBTRACT offset FROM 26 GIVING decrypt-offset
|
||||
MOVE encrypt(decrypt-offset, str) TO decrypted-str
|
||||
EXIT FUNCTION.
|
||||
|
||||
END FUNCTION decrypt.
|
||||
54
Task/Caesar-cipher/Euphoria/caesar-cipher.eu
Normal file
54
Task/Caesar-cipher/Euphoria/caesar-cipher.eu
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
--caesar cipher for Rosetta Code wiki
|
||||
--User:Lnettnay
|
||||
|
||||
--usage eui caesar ->default text, key and encode flag
|
||||
--usage eui caesar 'Text with spaces and punctuation!' 5 D
|
||||
--If text has imbedded spaces must use apostophes instead of quotes so all punctuation works
|
||||
--key = integer from 1 to 25, defaults to 13
|
||||
--flag = E (Encode) or D (Decode), defaults to E
|
||||
--no error checking is done on key or flag
|
||||
|
||||
include std/get.e
|
||||
include std/types.e
|
||||
|
||||
|
||||
sequence cmd = command_line()
|
||||
sequence val
|
||||
-- default text for encryption
|
||||
sequence text = "The Quick Brown Fox Jumps Over The Lazy Dog."
|
||||
atom key = 13 -- default to Rot-13
|
||||
sequence flag = "E" -- default to Encrypt
|
||||
atom offset
|
||||
atom num_letters = 26 -- number of characters in alphabet
|
||||
|
||||
--get text
|
||||
if length(cmd) >= 3 then
|
||||
text = cmd[3]
|
||||
end if
|
||||
|
||||
--get key value
|
||||
if length(cmd) >= 4 then
|
||||
val = value(cmd[4])
|
||||
key = val[2]
|
||||
end if
|
||||
|
||||
--get Encrypt/Decrypt flag
|
||||
if length(cmd) = 5 then
|
||||
flag = cmd[5]
|
||||
if compare(flag, "D") = 0 then
|
||||
key = 26 - key
|
||||
end if
|
||||
end if
|
||||
|
||||
for i = 1 to length(text) do
|
||||
if t_alpha(text[i]) then
|
||||
if t_lower(text[i]) then
|
||||
offset = 'a'
|
||||
else
|
||||
offset = 'A'
|
||||
end if
|
||||
text[i] = remainder(text[i] - offset + key, num_letters) + offset
|
||||
end if
|
||||
end for
|
||||
|
||||
printf(1,"%s\n",{text})
|
||||
|
|
@ -2,7 +2,7 @@ val rot = fn(s, key) {
|
|||
cp2s map(s2cp(s), by=fn(c) { rotate(rotate(c, distance=key, range='a'..'z'), distance=key, range='A'..'Z') })
|
||||
}
|
||||
|
||||
val s = "A quick brown fox jumped over something, you know."
|
||||
val s = "A quick brown fox jumped over something."
|
||||
val key = 3
|
||||
|
||||
writeln " original: ", s
|
||||
|
|
|
|||
75
Task/Caesar-cipher/PowerShell/caesar-cipher.ps1
Normal file
75
Task/Caesar-cipher/PowerShell/caesar-cipher.ps1
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
# Author: M. McNabb
|
||||
function Get-CaesarCipher
|
||||
{
|
||||
Param
|
||||
(
|
||||
[Parameter(
|
||||
Mandatory=$true,ValueFromPipeline=$true)]
|
||||
[string]
|
||||
$Text,
|
||||
|
||||
[ValidateRange(1,25)]
|
||||
[int]
|
||||
$Key = 1,
|
||||
|
||||
[switch]
|
||||
$Decode
|
||||
)
|
||||
|
||||
begin
|
||||
{
|
||||
$LowerAlpha = [char]'a'..[char]'z'
|
||||
$UpperAlpha = [char]'A'..[char]'Z'
|
||||
}
|
||||
|
||||
process
|
||||
{
|
||||
$Chars = $Text.ToCharArray()
|
||||
|
||||
function encode
|
||||
{
|
||||
param
|
||||
(
|
||||
$Char,
|
||||
$Alpha = [char]'a'..[char]'z'
|
||||
)
|
||||
$Index = $Alpha.IndexOf([int]$Char)
|
||||
$NewIndex = ($Index + $Key) - $Alpha.Length
|
||||
$Alpha[$NewIndex]
|
||||
}
|
||||
|
||||
function decode
|
||||
{
|
||||
param
|
||||
(
|
||||
$Char,
|
||||
$Alpha = [char]'a'..[char]'z'
|
||||
)
|
||||
$Index = $Alpha.IndexOf([int]$Char)
|
||||
$int = $Index - $Key
|
||||
if ($int -lt 0) {$NewIndex = $int + $Alpha.Length}
|
||||
else {$NewIndex = $int}
|
||||
$Alpha[$NewIndex]
|
||||
}
|
||||
|
||||
foreach ($Char in $Chars)
|
||||
{
|
||||
if ([int]$Char -in $LowerAlpha)
|
||||
{
|
||||
if ($Decode) {$Char = decode $Char}
|
||||
else {$Char = encode $Char}
|
||||
}
|
||||
elseif ([int]$Char -in $UpperAlpha)
|
||||
{
|
||||
if ($Decode) {$Char = decode $Char $UpperAlpha}
|
||||
else {$Char = encode $Char $UpperAlpha}
|
||||
}
|
||||
|
||||
$Char = [char]$Char
|
||||
[string]$OutText += $Char
|
||||
}
|
||||
|
||||
$OutText
|
||||
$OutText = $null
|
||||
}
|
||||
}
|
||||
37
Task/Caesar-cipher/Rebol/caesar-cipher.rebol
Normal file
37
Task/Caesar-cipher/Rebol/caesar-cipher.rebol
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Caesar cipher"
|
||||
file: %Caesar_cipher.r3
|
||||
url: https://rosettacode.org/wiki/Caesar_cipher
|
||||
needs: 3.0.0
|
||||
note: {Based on Red language version}
|
||||
]
|
||||
|
||||
caesar: function/with [
|
||||
src [string!]
|
||||
key [integer!]
|
||||
][
|
||||
parse/case src [
|
||||
any [
|
||||
change s: [lower (o: #"a") | upper (o: #"A")] (rot s/1 key o)
|
||||
| skip
|
||||
]
|
||||
]
|
||||
src
|
||||
][
|
||||
lower: charset [#"a" - #"z"]
|
||||
upper: charset [#"A" - #"Z"]
|
||||
rot: func [
|
||||
char [char!]
|
||||
key [number!]
|
||||
ofs [char!]
|
||||
][
|
||||
to char! key + char - ofs // 26 + ofs
|
||||
]
|
||||
]
|
||||
|
||||
encrypt: :caesar
|
||||
decrypt: func spec-of :caesar [caesar src negate key]
|
||||
|
||||
;; DEMO:
|
||||
print encrypt "Ceasar Cipher" 4 ;== Giewev Gmtliv
|
||||
print decrypt "Giewev Gmtliv" 4 ;== Ceasar Cipher
|
||||
46
Task/Caesar-cipher/SheerPower-4GL/caesar-cipher.4gl
Normal file
46
Task/Caesar-cipher/SheerPower-4GL/caesar-cipher.4gl
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
declare string plaintext$, ciphertext$, decrypted$
|
||||
|
||||
plaintext$ = 'The Quick Brown Fox Jumps Over The Lazy Dog'
|
||||
|
||||
print 'Plaintext: '; plaintext$
|
||||
|
||||
ciphertext$ = caesar$(plaintext$, 13)
|
||||
print 'ROT13: '; ciphertext$
|
||||
|
||||
decrypted$ = caesar$(ciphertext$, 13)
|
||||
print 'Decrypted: '; decrypted$
|
||||
|
||||
print
|
||||
print 'Shift 3 example (classic Caesar):'
|
||||
ciphertext$ = caesar$('Attack at dawn', 3)
|
||||
print 'Encrypted: '; ciphertext$
|
||||
print 'Decrypted: '; caesar$(ciphertext$, 23)
|
||||
|
||||
! =============================================================================
|
||||
! Routine: caesar$(text$, shift%)
|
||||
!
|
||||
! Shift each letter in text$ by shift% positions. A–Z and a–z wrap
|
||||
! within their own ranges; all other characters are copied unchanged.
|
||||
! =============================================================================
|
||||
routine caesar$(text$, shift%)
|
||||
declare string result$, ch$
|
||||
declare integer i%, code%
|
||||
|
||||
result$ = ''
|
||||
for i% = 1 to len(text$)
|
||||
ch$ = mid$(text$, i%, 1)
|
||||
code% = ord(ch$)
|
||||
|
||||
if code% >= 65 and code% <= 90 then
|
||||
! Uppercase A–Z: rotate within 65–90
|
||||
result$ = result$ + chr$((code% - 65 + shift%) mod 26 + 65)
|
||||
elseif code% >= 97 and code% <= 122 then
|
||||
! Lowercase a–z: rotate within 97–122
|
||||
result$ = result$ + chr$((code% - 97 + shift%) mod 26 + 97)
|
||||
else
|
||||
! Non-letter: pass through unchanged
|
||||
result$ = result$ + ch$
|
||||
end if
|
||||
next i%
|
||||
caesar$ = result$
|
||||
end routine
|
||||
7
Task/Caesar-cipher/Uiua/caesar-cipher.uiua
Normal file
7
Task/Caesar-cipher/Uiua/caesar-cipher.uiua
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
k ← 3
|
||||
m ← -:26 k
|
||||
Rot ← +@A◿26+k-@A
|
||||
Rrot ← +@A◿26+m-@A
|
||||
&sc
|
||||
&p. ⍥(¯≡(⍥Rot=1±.))2
|
||||
&p ⍥(¯≡(⍥Rrot=1±.))2
|
||||
|
|
@ -1,9 +1,7 @@
|
|||
import rand
|
||||
|
||||
const (
|
||||
lo_abc = 'abcdefghijklmnopqrstuvwxyz'
|
||||
up_abc = 'ABCDEFGHIJKLMNIPQRSTUVWXYZ'
|
||||
)
|
||||
const lo_abc = 'abcdefghijklmnopqrstuvwxyz'
|
||||
const up_abc = 'ABCDEFGHIJKLMNIPQRSTUVWXYZ'
|
||||
|
||||
fn main() {
|
||||
key := rand.int_in_range(2, 25) or {13}
|
||||
|
|
|
|||
35
Task/Caesar-cipher/VBScript/caesar-cipher.vbs
Normal file
35
Task/Caesar-cipher/VBScript/caesar-cipher.vbs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
str = "IT WAS THE BEST OF TIMES, IT WAS THE WORST OF TIMES."
|
||||
|
||||
Wscript.Echo str
|
||||
Wscript.Echo Rotate(str,5)
|
||||
Wscript.Echo Rotate(Rotate(str,5),-5)
|
||||
|
||||
'Rotate (Caesar encrypt/decrypt) test <numpos> positions.
|
||||
' numpos < 0 - rotate left
|
||||
' numpos > 0 - rotate right
|
||||
'Left rotation is converted to equivalent right rotation
|
||||
|
||||
Function Rotate (text, numpos)
|
||||
|
||||
dim dic: set dic = CreateObject("Scripting.Dictionary")
|
||||
dim ltr: ltr = Split("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")
|
||||
dim rot: rot = (26 + numpos Mod 26) Mod 26 'convert all to right rotation
|
||||
dim ch
|
||||
dim i
|
||||
|
||||
for i = 0 to ubound(ltr)
|
||||
dic(ltr(i)) = ltr((rot+i) Mod 26)
|
||||
next
|
||||
|
||||
Rotate = ""
|
||||
|
||||
for i = 1 to Len(text)
|
||||
ch = Mid(text,i,1)
|
||||
if dic.Exists(ch) Then
|
||||
Rotate = Rotate & dic(ch)
|
||||
else
|
||||
Rotate = Rotate & ch
|
||||
end if
|
||||
next
|
||||
|
||||
End Function
|
||||
Loading…
Add table
Add a link
Reference in a new issue