Data update
This commit is contained in:
parent
5150844a7d
commit
4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions
|
|
@ -8,7 +8,7 @@
|
|||
>r
|
||||
(
|
||||
\ Ignore anything below '.' as punctuation:
|
||||
dup '. n:> if
|
||||
dup '. n:> if|rebp;
|
||||
\ Do the conversion
|
||||
dup r@ n:+ swap
|
||||
\ Wrap appropriately
|
||||
|
|
|
|||
|
|
@ -1,58 +0,0 @@
|
|||
-- 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;
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
ia: to :integer `a`
|
||||
iA: to :integer `A`
|
||||
lowAZ: `a`..`z`
|
||||
uppAZ: `A`..`Z`
|
||||
ia: to :integer 'a'
|
||||
iA: to :integer 'A'
|
||||
lowAZ: 'a'..'z'
|
||||
uppAZ: 'A'..'Z'
|
||||
|
||||
caesar: function [s, xx][
|
||||
k: (not? null? attr 'decode)? -> 26-xx -> xx
|
||||
result: new ""
|
||||
result: ""
|
||||
loop s 'i [
|
||||
(in? i lowAZ)? -> 'result ++ to :char ia + (k + (to :integer i) - ia) % 26
|
||||
[
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
Caesar(string, n){
|
||||
Loop Parse, string
|
||||
{
|
||||
If (Asc(A_LoopField) >= Asc("A") and Asc(A_LoopField) <= Asc("Z"))
|
||||
out .= Chr(Mod(Asc(A_LoopField)-Asc("A")+n,26)+Asc("A"))
|
||||
Else If (Asc(A_LoopField) >= Asc("a") and Asc(A_LoopField) <= Asc("z"))
|
||||
out .= Chr(Mod(Asc(A_LoopField)-Asc("a")+n,26)+Asc("a"))
|
||||
Else out .= A_LoopField
|
||||
}
|
||||
return out
|
||||
Loop Parse, string
|
||||
{
|
||||
If (Asc(A_LoopField) >= Asc("A") and Asc(A_LoopField) <= Asc("Z"))
|
||||
out .= Chr(Mod(Asc(A_LoopField)-Asc("A")+n,26)+Asc("A"))
|
||||
Else If (Asc(A_LoopField) >= Asc("a") and Asc(A_LoopField) <= Asc("z"))
|
||||
out .= Chr(Mod(Asc(A_LoopField)-Asc("a")+n,26)+Asc("a"))
|
||||
Else out .= A_LoopField
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
MsgBox % Caesar("h i", 2) "`n" Caesar("Hi", 20)
|
||||
|
|
|
|||
|
|
@ -1,32 +0,0 @@
|
|||
$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
|
||||
|
|
@ -1,33 +1,33 @@
|
|||
beads 1 program 'Caesar cipher'
|
||||
calc main_init
|
||||
var str = "The five boxing wizards (🤖) jump quickly."
|
||||
log "Plain: {str}"
|
||||
str = Encrypt(str, 3)
|
||||
log "Encrypted: {str}"
|
||||
str = Decrypt(str, 3)
|
||||
log "Decrypted: {str}"
|
||||
var str = "The five boxing wizards (🤖) jump quickly."
|
||||
log "Plain: {str}"
|
||||
str = Encrypt(str, 3)
|
||||
log "Encrypted: {str}"
|
||||
str = Decrypt(str, 3)
|
||||
log "Decrypted: {str}"
|
||||
|
||||
// encrypt a string by shifting the letters over by a number of slots
|
||||
// pass through any characters that are not in the Roman alphabet
|
||||
calc Encrypt(
|
||||
input:str --- string to encrypt
|
||||
nshift --- number of characters to slide over
|
||||
) : str --- encrypted string
|
||||
|
||||
var newStr = ""
|
||||
loop from:1 to:str_len(input) count:myCount
|
||||
var unicode : num = from_char(subset(input, from:myCount, len:1))
|
||||
if unicode >= 65 and unicode <= 90
|
||||
unicode = mod((unicode - 65 + nshift), 26) + 65
|
||||
elif unicode >= 97 and unicode <= 122
|
||||
unicode = mod((unicode - 97 + nshift), 26) + 97
|
||||
newStr = newStr & to_char(unicode)
|
||||
return newStr
|
||||
input:str --- string to encrypt
|
||||
nshift --- number of characters to slide over
|
||||
) : str --- encrypted string
|
||||
|
||||
var newStr = ""
|
||||
loop from:1 to:str_len(input) count:myCount
|
||||
var unicode : num = from_char(subset(input, from:myCount, len:1))
|
||||
if unicode >= 65 and unicode <= 90
|
||||
unicode = mod((unicode - 65 + nshift), 26) + 65
|
||||
elif unicode >= 97 and unicode <= 122
|
||||
unicode = mod((unicode - 97 + nshift), 26) + 97
|
||||
newStr = newStr & to_char(unicode)
|
||||
return newStr
|
||||
|
||||
// undo the encryption
|
||||
calc Decrypt(
|
||||
input:str
|
||||
nshift
|
||||
) : str
|
||||
// we could also just shift by 26-nshift, same as going in reverse
|
||||
return Encrypt(input, -nshift)
|
||||
input:str
|
||||
nshift
|
||||
) : str
|
||||
// we could also just shift by 26-nshift, same as going in reverse
|
||||
return Encrypt(input, -nshift)
|
||||
|
|
|
|||
|
|
@ -11,15 +11,15 @@ public :
|
|||
|
||||
char operator( )( char c ) {
|
||||
if ( isspace( c ) )
|
||||
return ' ' ;
|
||||
return ' ' ;
|
||||
else {
|
||||
static std::string letters( "abcdefghijklmnopqrstuvwxyz" ) ;
|
||||
std::string::size_type found = letters.find(tolower( c )) ;
|
||||
int shiftedpos = ( static_cast<int>( found ) + shift ) % 26 ;
|
||||
if ( shiftedpos < 0 ) //in case of decryption possibly
|
||||
shiftedpos = 26 + shiftedpos ;
|
||||
char shifted = letters[shiftedpos] ;
|
||||
return shifted ;
|
||||
static std::string letters( "abcdefghijklmnopqrstuvwxyz" ) ;
|
||||
std::string::size_type found = letters.find(tolower( c )) ;
|
||||
int shiftedpos = ( static_cast<int>( found ) + shift ) % 26 ;
|
||||
if ( shiftedpos < 0 ) //in case of decryption possibly
|
||||
shiftedpos = 26 + shiftedpos ;
|
||||
char shifted = letters[shiftedpos] ;
|
||||
return shifted ;
|
||||
}
|
||||
}
|
||||
} ;
|
||||
|
|
@ -33,12 +33,12 @@ int main( ) {
|
|||
std::cin >> myshift ;
|
||||
std::cout << "Before encryption:\n" << input << std::endl ;
|
||||
std::transform ( input.begin( ) , input.end( ) , input.begin( ) ,
|
||||
MyTransform( myshift ) ) ;
|
||||
MyTransform( myshift ) ) ;
|
||||
std::cout << "encrypted:\n" ;
|
||||
std::cout << input << std::endl ;
|
||||
myshift *= -1 ; //decrypting again
|
||||
std::transform ( input.begin( ) , input.end( ) , input.begin( ) ,
|
||||
MyTransform( myshift ) ) ;
|
||||
MyTransform( myshift ) ) ;
|
||||
std::cout << "Decrypted again:\n" ;
|
||||
std::cout << input << std::endl ;
|
||||
return 0 ;
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
void rot(int c, char *str)
|
||||
{
|
||||
int l = strlen(str);
|
||||
int l = strlen(str);
|
||||
|
||||
const char* alpha_low = "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
|
|
@ -21,32 +21,32 @@ void rot(int c, char *str)
|
|||
|
||||
int i; /* loop var */
|
||||
|
||||
for (i = 0; i < l; i++) /* for each letter in string */
|
||||
{
|
||||
if( 0 == isalpha(str[i]) ) continue; /* not alphabet character */
|
||||
for (i = 0; i < l; i++) /* for each letter in string */
|
||||
{
|
||||
if( 0 == isalpha(str[i]) ) continue; /* not alphabet character */
|
||||
|
||||
idx = (int) (tolower(str[i]) - 'a') + c) % 26; /* compute index */
|
||||
|
||||
if( isupper(str[i]) )
|
||||
if( isupper(str[i]) )
|
||||
subst = alpha_high[idx];
|
||||
else
|
||||
subst = alpha_low[idx];
|
||||
|
||||
str[i] = subst;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
char str[] = "This is a top secret text message!";
|
||||
|
||||
printf("Original: %s\n", str);
|
||||
caesar(str);
|
||||
printf("Encrypted: %s\n", str);
|
||||
decaesar(str);
|
||||
printf("Decrypted: %s\n", str);
|
||||
|
||||
return 0;
|
||||
char str[] = "This is a top secret text message!";
|
||||
|
||||
printf("Original: %s\n", str);
|
||||
caesar(str);
|
||||
printf("Encrypted: %s\n", str);
|
||||
decaesar(str);
|
||||
printf("Decrypted: %s\n", str);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,36 +0,0 @@
|
|||
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.
|
||||
|
|
@ -1,84 +0,0 @@
|
|||
>>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.
|
||||
|
|
@ -2,57 +2,57 @@ alias modn [ mod (+ (mod $arg1 $arg2) $arg2) $arg2 ]
|
|||
//Cubescript's built-in mod will fail on negative numbers
|
||||
|
||||
alias cipher [
|
||||
push alpha [
|
||||
"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"
|
||||
"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"
|
||||
] [ push chars [] [
|
||||
loop i (strlen $arg1) [
|
||||
looplist n $alpha [
|
||||
if (! (listlen $chars)) [
|
||||
alias chars (? (> (listindex $n (substr $arg1 $i 1)) -1) $n [])
|
||||
]
|
||||
]
|
||||
alias arg1 (
|
||||
concatword (substr $arg1 0 $i) (
|
||||
? (> (listindex $chars (substr $arg1 $i 1)) -1) (
|
||||
at $chars (
|
||||
modn (+ (
|
||||
listindex $chars (substr $arg1 $i 1)
|
||||
) $arg2) (listlen $chars)
|
||||
)
|
||||
) (substr $arg1 $i 1)
|
||||
) (substr $arg1 (+ $i 1) (strlen $arg1))
|
||||
)
|
||||
alias chars []
|
||||
]
|
||||
] ]
|
||||
result $arg1
|
||||
push alpha [
|
||||
"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"
|
||||
"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"
|
||||
] [ push chars [] [
|
||||
loop i (strlen $arg1) [
|
||||
looplist n $alpha [
|
||||
if (! (listlen $chars)) [
|
||||
alias chars (? (> (listindex $n (substr $arg1 $i 1)) -1) $n [])
|
||||
]
|
||||
]
|
||||
alias arg1 (
|
||||
concatword (substr $arg1 0 $i) (
|
||||
? (> (listindex $chars (substr $arg1 $i 1)) -1) (
|
||||
at $chars (
|
||||
modn (+ (
|
||||
listindex $chars (substr $arg1 $i 1)
|
||||
) $arg2) (listlen $chars)
|
||||
)
|
||||
) (substr $arg1 $i 1)
|
||||
) (substr $arg1 (+ $i 1) (strlen $arg1))
|
||||
)
|
||||
alias chars []
|
||||
]
|
||||
] ]
|
||||
result $arg1
|
||||
]
|
||||
|
||||
alias decipher [
|
||||
push alpha [
|
||||
"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"
|
||||
"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"
|
||||
] [ push chars [] [
|
||||
loop i (strlen $arg1) [
|
||||
looplist n $alpha [
|
||||
if (! (listlen $chars)) [
|
||||
alias chars (? (> (listindex $n (substr $arg1 $i 1)) -1) $n [])
|
||||
]
|
||||
]
|
||||
alias arg1 (
|
||||
concatword (substr $arg1 0 $i) (
|
||||
? (> (listindex $chars (substr $arg1 $i 1)) -1) (
|
||||
at $chars (
|
||||
modn (- (
|
||||
listindex $chars (substr $arg1 $i 1)
|
||||
) $arg2 ) (listlen $chars)
|
||||
)
|
||||
) (substr $arg1 $i 1)
|
||||
) (substr $arg1 (+ $i 1) (strlen $arg1))
|
||||
)
|
||||
alias chars []
|
||||
]
|
||||
] ]
|
||||
result $arg1
|
||||
push alpha [
|
||||
"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"
|
||||
"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"
|
||||
] [ push chars [] [
|
||||
loop i (strlen $arg1) [
|
||||
looplist n $alpha [
|
||||
if (! (listlen $chars)) [
|
||||
alias chars (? (> (listindex $n (substr $arg1 $i 1)) -1) $n [])
|
||||
]
|
||||
]
|
||||
alias arg1 (
|
||||
concatword (substr $arg1 0 $i) (
|
||||
? (> (listindex $chars (substr $arg1 $i 1)) -1) (
|
||||
at $chars (
|
||||
modn (- (
|
||||
listindex $chars (substr $arg1 $i 1)
|
||||
) $arg2 ) (listlen $chars)
|
||||
)
|
||||
) (substr $arg1 $i 1)
|
||||
) (substr $arg1 (+ $i 1) (strlen $arg1))
|
||||
)
|
||||
alias chars []
|
||||
]
|
||||
] ]
|
||||
result $arg1
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,53 +1,53 @@
|
|||
class
|
||||
APPLICATION
|
||||
APPLICATION
|
||||
|
||||
inherit
|
||||
ARGUMENTS
|
||||
ARGUMENTS
|
||||
|
||||
create
|
||||
make
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
-- Run application.
|
||||
local
|
||||
s: STRING_32
|
||||
do
|
||||
s := "The tiny tiger totally taunted the tall Till."
|
||||
print ("%NString to encode: " + s)
|
||||
print ("%NEncoded string: " + encode (s, 12))
|
||||
print ("%NDecoded string (after encoding and decoding): " + decode (encode (s, 12), 12))
|
||||
end
|
||||
make
|
||||
-- Run application.
|
||||
local
|
||||
s: STRING_32
|
||||
do
|
||||
s := "The tiny tiger totally taunted the tall Till."
|
||||
print ("%NString to encode: " + s)
|
||||
print ("%NEncoded string: " + encode (s, 12))
|
||||
print ("%NDecoded string (after encoding and decoding): " + decode (encode (s, 12), 12))
|
||||
end
|
||||
|
||||
feature -- Basic operations
|
||||
feature -- Basic operations
|
||||
|
||||
decode (to_be_decoded: STRING_32; offset: INTEGER): STRING_32
|
||||
-- Decode `to be decoded' according to `offset'.
|
||||
do
|
||||
Result := encode (to_be_decoded, 26 - offset)
|
||||
end
|
||||
decode (to_be_decoded: STRING_32; offset: INTEGER): STRING_32
|
||||
-- Decode `to be decoded' according to `offset'.
|
||||
do
|
||||
Result := encode (to_be_decoded, 26 - offset)
|
||||
end
|
||||
|
||||
encode (to_be_encoded: STRING_32; offset: INTEGER): STRING_32
|
||||
-- Encode `to be encoded' according to `offset'.
|
||||
local
|
||||
l_offset: INTEGER
|
||||
l_char_code: INTEGER
|
||||
do
|
||||
create Result.make_empty
|
||||
l_offset := (offset \\ 26) + 26
|
||||
across to_be_encoded as tbe loop
|
||||
if tbe.item.is_alpha then
|
||||
if tbe.item.is_upper then
|
||||
l_char_code := ('A').code + (tbe.item.code - ('A').code + l_offset) \\ 26
|
||||
Result.append_character (l_char_code.to_character_32)
|
||||
else
|
||||
l_char_code := ('a').code + (tbe.item.code - ('a').code + l_offset) \\ 26
|
||||
Result.append_character (l_char_code.to_character_32)
|
||||
end
|
||||
else
|
||||
Result.append_character (tbe.item)
|
||||
end
|
||||
end
|
||||
end
|
||||
encode (to_be_encoded: STRING_32; offset: INTEGER): STRING_32
|
||||
-- Encode `to be encoded' according to `offset'.
|
||||
local
|
||||
l_offset: INTEGER
|
||||
l_char_code: INTEGER
|
||||
do
|
||||
create Result.make_empty
|
||||
l_offset := (offset \\ 26) + 26
|
||||
across to_be_encoded as tbe loop
|
||||
if tbe.item.is_alpha then
|
||||
if tbe.item.is_upper then
|
||||
l_char_code := ('A').code + (tbe.item.code - ('A').code + l_offset) \\ 26
|
||||
Result.append_character (l_char_code.to_character_32)
|
||||
else
|
||||
l_char_code := ('a').code + (tbe.item.code - ('a').code + l_offset) \\ 26
|
||||
Result.append_character (l_char_code.to_character_32)
|
||||
end
|
||||
else
|
||||
Result.append_character (tbe.item)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ extension encryptOp
|
|||
= new Encrypting(26 - key, self).summarize(new StringWriter());
|
||||
}
|
||||
|
||||
public program()
|
||||
public Program()
|
||||
{
|
||||
Console.printLine("Original text :",TestText);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,54 +0,0 @@
|
|||
--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})
|
||||
|
|
@ -8,7 +8,7 @@ function void caesar_encode(message:ptr key:uint) {
|
|||
temp = + temp key// shift lowercase letters
|
||||
if > temp 'z' {
|
||||
temp = - temp 26ub
|
||||
} ;
|
||||
} ;
|
||||
} {
|
||||
if in temp uppers {
|
||||
temp = + temp key// shift uppercase letters
|
||||
|
|
|
|||
|
|
@ -1,22 +1,22 @@
|
|||
CaesarCipher := function(s, n)
|
||||
local r, c, i, lower, upper;
|
||||
lower := "abcdefghijklmnopqrstuvwxyz";
|
||||
upper := "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
r := "";
|
||||
for c in s do
|
||||
i := Position(lower, c);
|
||||
if i <> fail then
|
||||
Add(r, lower[RemInt(i + n - 1, 26) + 1]);
|
||||
else
|
||||
i := Position(upper, c);
|
||||
if i <> fail then
|
||||
Add(r, upper[RemInt(i + n - 1, 26) + 1]);
|
||||
else
|
||||
Add(r, c);
|
||||
fi;
|
||||
fi;
|
||||
od;
|
||||
return r;
|
||||
local r, c, i, lower, upper;
|
||||
lower := "abcdefghijklmnopqrstuvwxyz";
|
||||
upper := "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
r := "";
|
||||
for c in s do
|
||||
i := Position(lower, c);
|
||||
if i <> fail then
|
||||
Add(r, lower[RemInt(i + n - 1, 26) + 1]);
|
||||
else
|
||||
i := Position(upper, c);
|
||||
if i <> fail then
|
||||
Add(r, upper[RemInt(i + n - 1, 26) + 1]);
|
||||
else
|
||||
Add(r, c);
|
||||
fi;
|
||||
fi;
|
||||
od;
|
||||
return r;
|
||||
end;
|
||||
|
||||
CaesarCipher("IBM", 25);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,6 @@
|
|||
(let original "The Quick Brown Fox Jumps Over The Lazy Dog."
|
||||
encrypted (caeser -1 original)
|
||||
decrypted (caeser 1 encrypted))
|
||||
(str "Original: " original "
|
||||
Encrypted: " encrypted "
|
||||
Decrypted: " decrypted)
|
||||
"Original: {original}
|
||||
Encrypted: {encrypted}
|
||||
Decrypted: {decrypted}"
|
||||
|
|
|
|||
|
|
@ -12,11 +12,11 @@ def encrypt(key):
|
|||
else if ($c >= 97 and $c <= 122) # 'a' to 'z'
|
||||
then .d = $c + $offset
|
||||
| if (.d > 122)
|
||||
then .d += -26
|
||||
else .
|
||||
end
|
||||
else .
|
||||
end
|
||||
then .d += -26
|
||||
else .
|
||||
end
|
||||
else .
|
||||
end
|
||||
end
|
||||
| .chars[$i] = .d )
|
||||
| .chars | implode
|
||||
|
|
|
|||
|
|
@ -1,30 +1,30 @@
|
|||
local function encrypt(text, key)
|
||||
return text:gsub("%a", function(t)
|
||||
local base = (t:lower() == t and string.byte('a') or string.byte('A'))
|
||||
return text:gsub("%a", function(t)
|
||||
local base = (t:lower() == t and string.byte('a') or string.byte('A'))
|
||||
|
||||
local r = t:byte() - base
|
||||
r = r + key
|
||||
r = r%26 -- works correctly even if r is negative
|
||||
r = r + base
|
||||
return string.char(r)
|
||||
end)
|
||||
local r = t:byte() - base
|
||||
r = r + key
|
||||
r = r%26 -- works correctly even if r is negative
|
||||
r = r + base
|
||||
return string.char(r)
|
||||
end)
|
||||
end
|
||||
|
||||
local function decrypt(text, key)
|
||||
return encrypt(text, -key)
|
||||
return encrypt(text, -key)
|
||||
end
|
||||
|
||||
caesar = {
|
||||
encrypt = encrypt,
|
||||
decrypt = decrypt,
|
||||
encrypt = encrypt,
|
||||
decrypt = decrypt,
|
||||
}
|
||||
|
||||
-- test
|
||||
do
|
||||
local text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz"
|
||||
local encrypted = caesar.encrypt(text, 7)
|
||||
local decrypted = caesar.decrypt(encrypted, 7)
|
||||
print("Original text: ", text)
|
||||
print("Encrypted text: ", encrypted)
|
||||
print("Decrypted text: ", decrypted)
|
||||
local text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz"
|
||||
local encrypted = caesar.encrypt(text, 7)
|
||||
local decrypted = caesar.decrypt(encrypted, 7)
|
||||
print("Original text: ", text)
|
||||
print("Encrypted text: ", encrypted)
|
||||
print("Decrypted text: ", decrypted)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -16,6 +16,6 @@ Print Cipher$(B$,-10)
|
|||
|
||||
n=1 ' n negative or positive or zero
|
||||
for i=65 to 65+25
|
||||
a=(1+(i -64)+n+24) mod 26 + 65
|
||||
? chr$(a),
|
||||
a=(1+(i -64)+n+24) mod 26 + 65
|
||||
? chr$(a),
|
||||
next
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
function s = cipherCaesar(s, key)
|
||||
s = char( mod(s - 'A' + key, 25 ) + 'A');
|
||||
end;
|
||||
end;
|
||||
function s = decipherCaesar(s, key)
|
||||
s = char( mod(s - 'A' - key, 25 ) + 'A');
|
||||
end;
|
||||
|
|
|
|||
|
|
@ -6,35 +6,35 @@ fun readfile () = readfile []
|
|||
readfile ` ln :: x
|
||||
end
|
||||
|
||||
local
|
||||
val lower_a = ord #"a";
|
||||
val lower_z = ord #"z";
|
||||
val upper_a = ord #"A";
|
||||
val upper_z = ord #"Z";
|
||||
local
|
||||
val lower_a = ord #"a";
|
||||
val lower_z = ord #"z";
|
||||
val upper_a = ord #"A";
|
||||
val upper_z = ord #"Z";
|
||||
|
||||
fun which
|
||||
(c_upper c) = (upper_a, upper_z)
|
||||
| _ = (lower_a, lower_z)
|
||||
;
|
||||
|
||||
fun scale
|
||||
(c, az) where (c > #1 az) = scale( (#0 az + (c - #1 az - 1)), az)
|
||||
| (c, az) = c
|
||||
|
||||
fun which
|
||||
(c_upper c) = (upper_a, upper_z)
|
||||
| _ = (lower_a, lower_z)
|
||||
;
|
||||
|
||||
fun scale
|
||||
(c, az) where (c > #1 az) = scale( (#0 az + (c - #1 az - 1)), az)
|
||||
| (c, az) = c
|
||||
|
||||
in
|
||||
fun encipher
|
||||
([], offset, t) = implode ` rev t
|
||||
| (x :: xs, offset, t) where (c_alphabetic x) = encipher (xs, offset, (chr ` scale (ord x + offset, which x)) :: t)
|
||||
| (x :: xs, offset, t) = encipher (xs, offset, x :: t)
|
||||
| (s, offset) = if (offset < 0) then
|
||||
encipher (explode s, 26 + (offset rem 26), [])
|
||||
else
|
||||
encipher (explode s, offset rem 26, [])
|
||||
fun encipher
|
||||
([], offset, t) = implode ` rev t
|
||||
| (x :: xs, offset, t) where (c_alphabetic x) = encipher (xs, offset, (chr ` scale (ord x + offset, which x)) :: t)
|
||||
| (x :: xs, offset, t) = encipher (xs, offset, x :: t)
|
||||
| (s, offset) = if (offset < 0) then
|
||||
encipher (explode s, 26 + (offset rem 26), [])
|
||||
else
|
||||
encipher (explode s, offset rem 26, [])
|
||||
end
|
||||
|
||||
fun default
|
||||
(false, y) = y
|
||||
| (x, _) = x
|
||||
(false, y) = y
|
||||
| (x, _) = x
|
||||
|
||||
;
|
||||
map println ` map (fn s = encipher (s,ston ` default (argv 0, "1"))) ` readfile ();
|
||||
|
|
|
|||
|
|
@ -1,35 +1,35 @@
|
|||
def caesar_encode(plaintext, shift)
|
||||
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
lowercase = "abcdefghijklmnopqrstuvwxyz"
|
||||
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
lowercase = "abcdefghijklmnopqrstuvwxyz"
|
||||
|
||||
cipher = ""
|
||||
for char in plaintext
|
||||
if char in uppercase
|
||||
cipher += uppercase[uppercase[char] - (26 - shift)]
|
||||
else if char in lowercase
|
||||
cipher += lowercase[lowercase[char] - (26 - shift)]
|
||||
else
|
||||
cipher += char
|
||||
end
|
||||
end
|
||||
cipher = ""
|
||||
for char in plaintext
|
||||
if char in uppercase
|
||||
cipher += uppercase[uppercase[char] - (26 - shift)]
|
||||
else if char in lowercase
|
||||
cipher += lowercase[lowercase[char] - (26 - shift)]
|
||||
else
|
||||
cipher += char
|
||||
end
|
||||
end
|
||||
|
||||
return cipher
|
||||
return cipher
|
||||
end
|
||||
|
||||
def caesar_decode(cipher, shift)
|
||||
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
lowercase = "abcdefghijklmnopqrstuvwxyz"
|
||||
uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
lowercase = "abcdefghijklmnopqrstuvwxyz"
|
||||
|
||||
plaintext = ""
|
||||
for char in cipher
|
||||
if char in uppercase
|
||||
plaintext += uppercase[uppercase[char] - shift]
|
||||
else if char in lowercase
|
||||
plaintext += lowercase[lowercase[char] - shift]
|
||||
else
|
||||
plaintext += char
|
||||
end
|
||||
end
|
||||
plaintext = ""
|
||||
for char in cipher
|
||||
if char in uppercase
|
||||
plaintext += uppercase[uppercase[char] - shift]
|
||||
else if char in lowercase
|
||||
plaintext += lowercase[lowercase[char] - shift]
|
||||
else
|
||||
plaintext += char
|
||||
end
|
||||
end
|
||||
|
||||
return plaintext
|
||||
return plaintext
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,75 +0,0 @@
|
|||
# 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
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +1,33 @@
|
|||
:- use_module(library(clpfd)).
|
||||
|
||||
caesar :-
|
||||
L1 = "The five boxing wizards jump quickly",
|
||||
writef("Original : %s\n", [L1]),
|
||||
L1 = "The five boxing wizards jump quickly",
|
||||
writef("Original : %s\n", [L1]),
|
||||
|
||||
% encryption of the sentence
|
||||
encoding(3, L1, L2) ,
|
||||
writef("Encoding : %s\n", [L2]),
|
||||
% encryption of the sentence
|
||||
encoding(3, L1, L2) ,
|
||||
writef("Encoding : %s\n", [L2]),
|
||||
|
||||
% deciphering on the encoded sentence
|
||||
encoding(3, L3, L2),
|
||||
writef("Decoding : %s\n", [L3]).
|
||||
% deciphering on the encoded sentence
|
||||
encoding(3, L3, L2),
|
||||
writef("Decoding : %s\n", [L3]).
|
||||
|
||||
% encoding/decoding of a sentence
|
||||
encoding(Key, L1, L2) :-
|
||||
maplist(caesar_cipher(Key), L1, L2).
|
||||
maplist(caesar_cipher(Key), L1, L2).
|
||||
|
||||
caesar_cipher(_, 32, 32) :- !.
|
||||
|
||||
caesar_cipher(Key, V1, V2) :-
|
||||
V #= Key + V1,
|
||||
V #= Key + V1,
|
||||
|
||||
% we verify that we are in the limits of A-Z and a-z.
|
||||
((V1 #=< 0'Z #/\ V #> 0'Z) #\/ (V1 #=< 0'z #/\ V #> 0'z)
|
||||
#\/
|
||||
(V1 #< 0'A #/\ V2 #>= 0'A)#\/ (V1 #< 0'a #/\ V2 #>= 0'a)) #==> A,
|
||||
% we verify that we are in the limits of A-Z and a-z.
|
||||
((V1 #=< 0'Z #/\ V #> 0'Z) #\/ (V1 #=< 0'z #/\ V #> 0'z)
|
||||
#\/
|
||||
(V1 #< 0'A #/\ V2 #>= 0'A)#\/ (V1 #< 0'a #/\ V2 #>= 0'a)) #==> A,
|
||||
|
||||
% if we are not in these limits A is 1, otherwise 0.
|
||||
V2 #= V - A * 26,
|
||||
% if we are not in these limits A is 1, otherwise 0.
|
||||
V2 #= V - A * 26,
|
||||
|
||||
% compute values of V1 and V2
|
||||
label([A, V1, V2]).
|
||||
% compute values of V1 and V2
|
||||
label([A, V1, V2]).
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
def caesar(s, k, decode = False):
|
||||
if decode: k = 26 - k
|
||||
return "".join([chr((ord(i) - 65 + k) % 26 + 65)
|
||||
for i in s.upper()
|
||||
if ord(i) >= 65 and ord(i) <= 90 ])
|
||||
if decode: k = 26 - k
|
||||
return "".join([chr((ord(i) - 65 + k) % 26 + 65)
|
||||
for i in s.upper()
|
||||
if ord(i) >= 65 and ord(i) <= 90 ])
|
||||
|
||||
msg = "The quick brown fox jumped over the lazy dogs"
|
||||
print msg
|
||||
|
|
|
|||
|
|
@ -2,26 +2,26 @@
|
|||
# Input: <number 0..25>\ntext to encode
|
||||
|
||||
/^[0-9]+$/ {
|
||||
# validate a number and translate it to analog form
|
||||
s/$/;9876543210dddddddddd/
|
||||
s/([0-9]);.*\1.{10}(.?)/\2/
|
||||
s/2/11/
|
||||
s/1/dddddddddd/g
|
||||
/[3-9]|d{25}d+/ {
|
||||
s/.*/Error: Key must be <= 25/
|
||||
q
|
||||
}
|
||||
# append from-table
|
||||
s/$/\nabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/
|
||||
# .. and to-table
|
||||
s/$/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/
|
||||
# rotate to-table, lower and uppercase independently, removing one `d' at a time
|
||||
: rotate
|
||||
s/^d(.*\n[^Z]+Z)(.)(.{25})(.)(.{25})/\1\3\2\5\4/
|
||||
t rotate
|
||||
s/\n//
|
||||
h
|
||||
d
|
||||
# validate a number and translate it to analog form
|
||||
s/$/;9876543210dddddddddd/
|
||||
s/([0-9]);.*\1.{10}(.?)/\2/
|
||||
s/2/11/
|
||||
s/1/dddddddddd/g
|
||||
/[3-9]|d{25}d+/ {
|
||||
s/.*/Error: Key must be <= 25/
|
||||
q
|
||||
}
|
||||
# append from-table
|
||||
s/$/\nabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/
|
||||
# .. and to-table
|
||||
s/$/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/
|
||||
# rotate to-table, lower and uppercase independently, removing one `d' at a time
|
||||
: rotate
|
||||
s/^d(.*\n[^Z]+Z)(.)(.{25})(.)(.{25})/\1\3\2\5\4/
|
||||
t rotate
|
||||
s/\n//
|
||||
h
|
||||
d
|
||||
}
|
||||
|
||||
# use \n to mark character to convert
|
||||
|
|
@ -29,9 +29,9 @@ s/^/\n/
|
|||
# append conversion table to pattern space
|
||||
G
|
||||
: loop
|
||||
# look up converted character and place it instead of old one
|
||||
s/\n(.)(.*\n.*\1.{51}(.))/\n\3\2/
|
||||
# advance \n even if prev. command fails, thus skip non-alphabetical characters
|
||||
/\n\n/! s/\n([^\n])/\1\n/
|
||||
# look up converted character and place it instead of old one
|
||||
s/\n(.)(.*\n.*\1.{51}(.))/\n\3\2/
|
||||
# advance \n even if prev. command fails, thus skip non-alphabetical characters
|
||||
/\n\n/! s/\n([^\n])/\1\n/
|
||||
t loop
|
||||
s/\n\n.*//
|
||||
|
|
|
|||
|
|
@ -5,28 +5,28 @@ lowerAlphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|||
upperAlphabet := "abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
caesarEncrypt(ch, key) :=
|
||||
let
|
||||
correctAlphabet :=
|
||||
lowerAlphabet when some(ch = lowerAlphabet)
|
||||
else
|
||||
upperAlphabet;
|
||||
|
||||
index := Sequence::firstIndexOf(correctAlphabet, ch);
|
||||
|
||||
newIndex := (index + key - 1) mod 26 + 1;
|
||||
in
|
||||
ch when not(some(ch = lowerAlphabet) or some(ch = upperAlphabet))
|
||||
else
|
||||
correctAlphabet[newIndex];
|
||||
|
||||
let
|
||||
correctAlphabet :=
|
||||
lowerAlphabet when some(ch = lowerAlphabet)
|
||||
else
|
||||
upperAlphabet;
|
||||
|
||||
index := Sequence::firstIndexOf(correctAlphabet, ch);
|
||||
|
||||
newIndex := (index + key - 1) mod 26 + 1;
|
||||
in
|
||||
ch when not(some(ch = lowerAlphabet) or some(ch = upperAlphabet))
|
||||
else
|
||||
correctAlphabet[newIndex];
|
||||
|
||||
caesarDecrypt(ch, key) := caesarEncrypt(ch, 26 - key);
|
||||
|
||||
main(args(2)) :=
|
||||
let
|
||||
key := Conversion::stringToInt(args[2]);
|
||||
encrypted := caesarEncrypt(args[1], key);
|
||||
decrypted := caesarDecrypt(encrypted, key);
|
||||
in
|
||||
"Input: \t" ++ args[1] ++ "\n" ++
|
||||
"Encrypted:\t" ++ encrypted ++ "\n" ++
|
||||
"Decrypted:\t" ++ decrypted;
|
||||
let
|
||||
key := Conversion::stringToInt(args[2]);
|
||||
encrypted := caesarEncrypt(args[1], key);
|
||||
decrypted := caesarDecrypt(encrypted, key);
|
||||
in
|
||||
"Input: \t" ++ args[1] ++ "\n" ++
|
||||
"Encrypted:\t" ++ encrypted ++ "\n" ++
|
||||
"Decrypted:\t" ++ decrypted;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
function caesar(s, k) {
|
||||
u = ascii(s)
|
||||
i = selectindex(u:>=65 :& u:<=90)
|
||||
if (length(i)>0) u[i] = mod(u[i]:+(k-65), 26):+65
|
||||
i = selectindex(u:>=97 :& u:<=122)
|
||||
if (length(i)>0) u[i] = mod(u[i]:+(k-97), 26):+97
|
||||
return(char(u))
|
||||
u = ascii(s)
|
||||
i = selectindex(u:>=65 :& u:<=90)
|
||||
if (length(i)>0) u[i] = mod(u[i]:+(k-65), 26):+65
|
||||
i = selectindex(u:>=97 :& u:<=122)
|
||||
if (length(i)>0) u[i] = mod(u[i]:+(k-97), 26):+97
|
||||
return(char(u))
|
||||
}
|
||||
|
||||
caesar("layout", 20)
|
||||
|
|
|
|||
|
|
@ -3,21 +3,21 @@ package require Tcl 8.6; # Or TclOO package for 8.5
|
|||
oo::class create Caesar {
|
||||
variable encryptMap decryptMap
|
||||
constructor shift {
|
||||
for {set i 0} {$i < 26} {incr i} {
|
||||
# Play fast and loose with string/list duality for shorter code
|
||||
append encryptMap [format "%c %c %c %c " \
|
||||
[expr {$i+65}] [expr {($i+$shift)%26+65}] \
|
||||
[expr {$i+97}] [expr {($i+$shift)%26+97}]]
|
||||
append decryptMap [format "%c %c %c %c " \
|
||||
[expr {$i+65}] [expr {($i-$shift)%26+65}] \
|
||||
[expr {$i+97}] [expr {($i-$shift)%26+97}]]
|
||||
}
|
||||
for {set i 0} {$i < 26} {incr i} {
|
||||
# Play fast and loose with string/list duality for shorter code
|
||||
append encryptMap [format "%c %c %c %c " \
|
||||
[expr {$i+65}] [expr {($i+$shift)%26+65}] \
|
||||
[expr {$i+97}] [expr {($i+$shift)%26+97}]]
|
||||
append decryptMap [format "%c %c %c %c " \
|
||||
[expr {$i+65}] [expr {($i-$shift)%26+65}] \
|
||||
[expr {$i+97}] [expr {($i-$shift)%26+97}]]
|
||||
}
|
||||
}
|
||||
|
||||
method encrypt text {
|
||||
string map $encryptMap $text
|
||||
string map $encryptMap $text
|
||||
}
|
||||
method decrypt text {
|
||||
string map $decryptMap $text
|
||||
string map $decryptMap $text
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
function replace(input: string, key: number) : string {
|
||||
return input.replace(/([a-z])/g,
|
||||
($1) => String.fromCharCode(($1.charCodeAt(0) + key + 26 - 97) % 26 + 97)
|
||||
).replace(/([A-Z])/g,
|
||||
($1) => String.fromCharCode(($1.charCodeAt(0) + key + 26 - 65) % 26 + 65));
|
||||
return input.replace(/([a-z])/g,
|
||||
($1) => String.fromCharCode(($1.charCodeAt(0) + key + 26 - 97) % 26 + 97)
|
||||
).replace(/([A-Z])/g,
|
||||
($1) => String.fromCharCode(($1.charCodeAt(0) + key + 26 - 65) % 26 + 65));
|
||||
}
|
||||
|
||||
// test
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
decl string mode
|
||||
while (not (or (= mode "encode") (= mode "decode")))
|
||||
out "encode/decode: " console
|
||||
set mode (lower (in string console))
|
||||
out "encode/decode: " console
|
||||
set mode (lower (in string console))
|
||||
end while
|
||||
|
||||
decl string message
|
||||
|
|
@ -12,17 +12,17 @@ decl int key
|
|||
out "key: " console
|
||||
set key (in int console)
|
||||
if (or (> key 26) (< key 0))
|
||||
out endl "invalid key" endl console
|
||||
stop
|
||||
out endl "invalid key" endl console
|
||||
stop
|
||||
end if
|
||||
|
||||
if (= mode "decode")
|
||||
set key (int (- 26 key))
|
||||
set key (int (- 26 key))
|
||||
end if
|
||||
|
||||
for (decl int i) (< i (size message)) (inc i)
|
||||
if (and (> (ord message<i>) 64) (< (ord message<i>) 91))
|
||||
out (chr (int (+ (mod (int (+ (- (ord message<i>) 65) key)) 26) 65))) console
|
||||
end if
|
||||
if (and (> (ord message<i>) 64) (< (ord message<i>) 91))
|
||||
out (chr (int (+ (mod (int (+ (- (ord message<i>) 65) key)) 26) 65))) console
|
||||
end if
|
||||
end for
|
||||
out endl console
|
||||
|
|
|
|||
|
|
@ -1,37 +1,37 @@
|
|||
import rand
|
||||
|
||||
const (
|
||||
lo_abc = 'abcdefghijklmnopqrstuvwxyz'
|
||||
up_abc = 'ABCDEFGHIJKLMNIPQRSTUVWXYZ'
|
||||
lo_abc = 'abcdefghijklmnopqrstuvwxyz'
|
||||
up_abc = 'ABCDEFGHIJKLMNIPQRSTUVWXYZ'
|
||||
)
|
||||
|
||||
fn main() {
|
||||
key := rand.int_in_range(2, 25) or {13}
|
||||
encrypted := caesar_encrypt('The five boxing wizards jump quickly', key)
|
||||
println(encrypted)
|
||||
println(caesar_decrypt(encrypted, key))
|
||||
key := rand.int_in_range(2, 25) or {13}
|
||||
encrypted := caesar_encrypt('The five boxing wizards jump quickly', key)
|
||||
println(encrypted)
|
||||
println(caesar_decrypt(encrypted, key))
|
||||
}
|
||||
|
||||
fn caesar_encrypt(str string, key int) string {
|
||||
offset := key % 26
|
||||
if offset == 0 {return str}
|
||||
mut nchr := u8(0)
|
||||
mut chr_arr := []u8{}
|
||||
for chr in str {
|
||||
if chr.ascii_str() in up_abc.split('') {
|
||||
nchr = chr + u8(offset)
|
||||
if nchr > u8(90) {nchr -= 26}
|
||||
}
|
||||
else if chr.ascii_str() in lo_abc.split('') {
|
||||
nchr = chr + u8(offset)
|
||||
if nchr > u8(122) {nchr -= 26}
|
||||
}
|
||||
else {nchr = chr}
|
||||
chr_arr << nchr
|
||||
}
|
||||
return chr_arr.bytestr()
|
||||
offset := key % 26
|
||||
if offset == 0 {return str}
|
||||
mut nchr := u8(0)
|
||||
mut chr_arr := []u8{}
|
||||
for chr in str {
|
||||
if chr.ascii_str() in up_abc.split('') {
|
||||
nchr = chr + u8(offset)
|
||||
if nchr > u8(90) {nchr -= 26}
|
||||
}
|
||||
else if chr.ascii_str() in lo_abc.split('') {
|
||||
nchr = chr + u8(offset)
|
||||
if nchr > u8(122) {nchr -= 26}
|
||||
}
|
||||
else {nchr = chr}
|
||||
chr_arr << nchr
|
||||
}
|
||||
return chr_arr.bytestr()
|
||||
}
|
||||
|
||||
fn caesar_decrypt(str string, key int) string {
|
||||
return caesar_encrypt(str, 26 - key)
|
||||
return caesar_encrypt(str, 26 - key)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,35 +0,0 @@
|
|||
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
|
||||
|
|
@ -6,6 +6,6 @@ while(Cur_Pos < Block_End) {
|
|||
if (Cur_Char >= 'A') {
|
||||
Ins_Char((Cur_Char - #11 + 26 + #10) % 26 + #11, OVERWRITE)
|
||||
} else {
|
||||
Char(1)
|
||||
Char(1)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,33 +1,33 @@
|
|||
set letters="ABCDEFGHIJKLMNOPQRSTUVWXYZ"::split();
|
||||
|
||||
func caesar(text,shift:number=1){
|
||||
set res:string="";
|
||||
for(i=0;?text-1;1){
|
||||
set t=text::at(i);
|
||||
set n=(letters::find(t::upper())+shift)%?letters;
|
||||
while(n<0){
|
||||
n=?letters+n;
|
||||
}
|
||||
set l=letters[n];
|
||||
(t::upper()==t)|=>l=l::lower()
|
||||
res+=l;
|
||||
}
|
||||
send res;
|
||||
set res:string="";
|
||||
for(i=0;?text-1;1){
|
||||
set t=text::at(i);
|
||||
set n=(letters::find(t::upper())+shift)%?letters;
|
||||
while(n<0){
|
||||
n=?letters+n;
|
||||
}
|
||||
set l=letters[n];
|
||||
(t::upper()==t)|=>l=l::lower()
|
||||
res+=l;
|
||||
}
|
||||
send res;
|
||||
}
|
||||
|
||||
func decode(text,shift:number=1){
|
||||
set res:string="";
|
||||
for(i=0;?text-1;1){
|
||||
set t=text::at(i);
|
||||
set n=(letters::find(t::upper())-shift)%?letters;
|
||||
while(n<0){
|
||||
n=?letters+n;
|
||||
}
|
||||
set l=letters[n];
|
||||
(t::upper()==t)|=>l=l::lower()
|
||||
res+=l;
|
||||
}
|
||||
send res;
|
||||
set res:string="";
|
||||
for(i=0;?text-1;1){
|
||||
set t=text::at(i);
|
||||
set n=(letters::find(t::upper())-shift)%?letters;
|
||||
while(n<0){
|
||||
n=?letters+n;
|
||||
}
|
||||
set l=letters[n];
|
||||
(t::upper()==t)|=>l=l::lower()
|
||||
res+=l;
|
||||
}
|
||||
send res;
|
||||
}
|
||||
|
||||
set e=caesar("Hi",20);
|
||||
|
|
|
|||
|
|
@ -11,25 +11,25 @@ print encrypted$ : print
|
|||
print criptex$(encrypted$, key$, DECIPHER)
|
||||
|
||||
sub criptex$(text$, key$, mode)
|
||||
local i, k, delta, longtext, longPattern, longkey, pattern$, res$
|
||||
|
||||
pattern$ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 .,;:()-"
|
||||
|
||||
longPattern = len(pattern$)
|
||||
longtext = len(text$)
|
||||
longkey = len(key$)
|
||||
|
||||
for i = 1 to longtext
|
||||
k = k + 1 : if k > longkey k = 1
|
||||
delta = instr(pattern$, mid$(text$, i, 1))
|
||||
delta = delta + (mode * instr(pattern$, mid$(key$, k, 1)))
|
||||
if delta > longPattern then
|
||||
delta = delta - longPattern
|
||||
elseif delta < 1 then
|
||||
delta = longPattern + delta
|
||||
end if
|
||||
res$ = res$ + mid$(pattern$, delta, 1)
|
||||
next i
|
||||
local i, k, delta, longtext, longPattern, longkey, pattern$, res$
|
||||
|
||||
return res$
|
||||
pattern$ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 .,;:()-"
|
||||
|
||||
longPattern = len(pattern$)
|
||||
longtext = len(text$)
|
||||
longkey = len(key$)
|
||||
|
||||
for i = 1 to longtext
|
||||
k = k + 1 : if k > longkey k = 1
|
||||
delta = instr(pattern$, mid$(text$, i, 1))
|
||||
delta = delta + (mode * instr(pattern$, mid$(key$, k, 1)))
|
||||
if delta > longPattern then
|
||||
delta = delta - longPattern
|
||||
elseif delta < 1 then
|
||||
delta = longPattern + delta
|
||||
end if
|
||||
res$ = res$ + mid$(pattern$, delta, 1)
|
||||
next i
|
||||
|
||||
return res$
|
||||
end sub
|
||||
|
|
|
|||
|
|
@ -1,57 +1,57 @@
|
|||
module Caesar;
|
||||
const
|
||||
size = 25;
|
||||
size = 25;
|
||||
|
||||
type
|
||||
Operation = (code,decode);
|
||||
Operation = (code,decode);
|
||||
|
||||
procedure C_D(s:string;k:integer;op: Operation): string;
|
||||
var
|
||||
i,key: integer;
|
||||
resp: string;
|
||||
n,c: char;
|
||||
i,key: integer;
|
||||
resp: string;
|
||||
n,c: char;
|
||||
begin
|
||||
resp := "";
|
||||
if op = Operation.decode then key := k else key := (26 - k) end;
|
||||
for i := 0 to len(s) - 1 do
|
||||
c := cap(s[i]);
|
||||
if (c >= 'A') & (c <= 'Z') then
|
||||
resp := resp +
|
||||
string(char(integer('A') + ((integer(c) - integer('A') + key )) mod 26));
|
||||
else
|
||||
resp := resp + string(c)
|
||||
end;
|
||||
end;
|
||||
return resp
|
||||
resp := "";
|
||||
if op = Operation.decode then key := k else key := (26 - k) end;
|
||||
for i := 0 to len(s) - 1 do
|
||||
c := cap(s[i]);
|
||||
if (c >= 'A') & (c <= 'Z') then
|
||||
resp := resp +
|
||||
string(char(integer('A') + ((integer(c) - integer('A') + key )) mod 26));
|
||||
else
|
||||
resp := resp + string(c)
|
||||
end;
|
||||
end;
|
||||
return resp
|
||||
end C_D;
|
||||
|
||||
procedure {public} Cipher(s:string;k:integer):string;
|
||||
var
|
||||
i: integer;
|
||||
resp: string;
|
||||
n,c: char;
|
||||
i: integer;
|
||||
resp: string;
|
||||
n,c: char;
|
||||
begin
|
||||
return C_D(s,k,Operation.code)
|
||||
return C_D(s,k,Operation.code)
|
||||
end Cipher;
|
||||
|
||||
procedure {public} Decipher(s:string;k:integer):string;
|
||||
var
|
||||
i: integer;
|
||||
resp: string;
|
||||
n,c: char;
|
||||
i: integer;
|
||||
resp: string;
|
||||
n,c: char;
|
||||
begin
|
||||
return C_D(s,k,Operation.decode)
|
||||
return C_D(s,k,Operation.decode)
|
||||
end Decipher;
|
||||
|
||||
var
|
||||
txt,cipher,decipher: string;
|
||||
txt,cipher,decipher: string;
|
||||
|
||||
begin
|
||||
txt := "HI";cipher := Caesar.Cipher(txt,2);decipher := Caesar.Decipher(cipher,2);
|
||||
writeln(txt," -c-> ",cipher," -d-> ",decipher);
|
||||
txt := "ZA";cipher := Caesar.Cipher(txt,2);decipher := Caesar.Decipher(cipher,2);
|
||||
writeln(txt," -c-> ",cipher," -d-> ",decipher);
|
||||
txt := "The five boxing wizards jump quickly";
|
||||
cipher := Caesar.Cipher(txt,2);decipher := Caesar.Decipher(cipher,2);
|
||||
writeln(txt," -c-> ",cipher," -d-> ",decipher)
|
||||
txt := "HI";cipher := Caesar.Cipher(txt,2);decipher := Caesar.Decipher(cipher,2);
|
||||
writeln(txt," -c-> ",cipher," -d-> ",decipher);
|
||||
txt := "ZA";cipher := Caesar.Cipher(txt,2);decipher := Caesar.Decipher(cipher,2);
|
||||
writeln(txt," -c-> ",cipher," -d-> ",decipher);
|
||||
txt := "The five boxing wizards jump quickly";
|
||||
cipher := Caesar.Cipher(txt,2);decipher := Caesar.Decipher(cipher,2);
|
||||
writeln(txt," -c-> ",cipher," -d-> ",decipher)
|
||||
end Caesar.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue