Data update

This commit is contained in:
Ingy döt Net 2024-10-16 18:07:41 -07:00
parent 81fd053722
commit 52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions

View file

@ -0,0 +1,75 @@
BEGIN # Chaocipher, translated from the Kotlin with permutations as in Ada #
STRING lalphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ";
STRING ralphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC";
PROC chaocipher = ( STRING text in, BOOL encrypt, show steps )STRING:
BEGIN
STRING left := lalphabet[ AT 1 ];
STRING right := ralphabet[ AT 1 ];
STRING text = text in [ AT 1 ];
OP LENGTH = ( STRING s )INT: UPB s - LWB s + 1;
[ 1 : LENGTH text ]CHAR e text;
[ 1 : LENGTH left ]CHAR temp;
FOR i TO UPB text DO
IF show steps THEN print( ( left, " ", right, newline ) ) FI;
INT index;
IF encrypt THEN
VOID( char in string( text[ i ], index, right ) );
e text[ i ] := IF index >= LWB left AND index <= UPB left
THEN left[ index ]
ELSE "?"
FI
ELSE
VOID( char in string( text[ i ], index, left ) );
e text[ i ] := IF index >= LWB right AND index <= UPB right
THEN right[ index ]
ELSE "?"
FI
FI;
IF index > 1 THEN
CHAR store;
# the permutations are performed using array slices #
# rather than explicit loops #
# permute left #
INT offset = 26 - index;
temp[ 1 : offset + 1 ] := left[ index : index + offset ];
temp[ offset + 2 : 26 ] := left[ 1 : index - 1 ];
store := temp[ 2 ];
temp[ 2 : 13 ] := temp[ 3 : 14 ];
temp[ 14 ] := store;
left := temp;
# permute right #
temp[ 1 : offset + 1 ] := right[ index : index + offset ];
temp[ offset + 2 : 26 ] := right[ 1 : index - 1 ];
store := temp[ 1 ];
temp[ 1 : 25 ] := temp[ 2 : 26 ];
temp[ 26 ] := store;
store := temp[ 3 ];
temp[ 3 : 13 ] := temp[ 4 : 14 ];
temp[ 14 ] := store;
right := temp
FI
OD;
e text
END # chaocipher # ;
STRING plaintext = "WELLDONEISBETTERTHANWELLSAID";
print( ( "The original plaintext is : ", plaintext, newline ) );
print( ( newline, "The left and right alphabets after each permutation" ) );
print( ( " during encryption are :", newline, newline ) );
STRING ciphertext = chaocipher( plaintext, TRUE, TRUE );
print( ( newline, "The ciphertext is : ", ciphertext, newline ) );
STRING plaintext2 = chaocipher( ciphertext, FALSE, FALSE );
print( ( newline, "The recovered plaintext is : ", plaintext2 ) );
print( ( " ", IF plaintext = plaintext2 THEN "" ELSE "NOT " FI, "as expected", newline ) )
END

View file

@ -3,49 +3,49 @@ enum
int ENCRYPT, DECRYPT
end
type Chaocipher
text L_ALPHABET = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
text R_ALPHABET = "PTLNBQDEOYSFAVZKGJRIHWXUMC"
fun exec = text by text value, Chaocipher:Mode mode, logic showSteps
text L_ALPHABET "HXUCZVAMDSLKPEFJRIGTWOBNYQ"
text R_ALPHABET "PTLNBQDEOYSFAVZKGJRIHWXUMC"
fun exec text by text value, Chaocipher:Mode mode, logic showSteps
^|since texts are mutable, we can operate directly on them without the need of Lists|^
text left = *L_ALPHABET # by using the valueOf operator we are sure that the string is copied
text right = *R_ALPHABET
text eText = text(" ", value.length)
text temp = text(" ", 26)
for int i = 0; i < value.length; ++i
text left *L_ALPHABET # by using the valueOf operator we are sure that the string is copied
text right *R_ALPHABET
text eText text(" ", value.length)
text temp text(" ", 26)
for int i 0; i < value.length; ++i
if showSteps do writeLine(left + " " + right) end
int index = 0
if mode == Chaocipher:Mode.ENCRYPT
index = right.find(value[i])
eText[i] = left[index]
int index 0
if mode æ Chaocipher:Mode.ENCRYPT
index right.find(value[i])
eText[i] left[index]
else
index = left.find(value[i])
eText[i] = right[index]
index left.find(value[i])
eText[i] right[index]
end
if i == value.length - 1 do break end
if i æ value.length - 1 do break end
# permute left
for int j = index; j < 26; ++j do temp[j - index] = left[j] end
for int j = 0; j < index; ++j do temp[26 - index + j] = left[j] end
var store = temp[1]
for int j = 2; j < 14; ++j do temp[j - 1] = temp[j] end
temp[13] = store
left = *temp
for int j ← index; j < 26; ++j do temp[j - index] ← left[j] end
for int j ← 0; j < index; ++j do temp[26 - index + j] ← left[j] end
var store temp[1]
for int j ← 2; j < 14; ++j do temp[j - 1] ← temp[j] end
temp[13] store
left *temp
# permute right
for int j = index; j < 26; ++j do temp[j - index] = right[j] end
for int j = 0; j < index; ++j do temp[26 - index + j] = right[j] end
store = temp[0]
for int j = 1; j < 26; ++j do temp[j - 1] = temp[j] end
temp[25] = store
store = temp[2]
for int j = 3; j < 14; ++j do temp[j - 1] = temp[j] end
temp[13] = store
right = *temp
for int j ← index; j < 26; ++j do temp[j - index] ← right[j] end
for int j ← 0; j < index; ++j do temp[26 - index + j] ← right[j] end
store temp[0]
for int j ← 1; j < 26; ++j do temp[j - 1] ← temp[j] end
temp[25] store
store temp[2]
for int j ← 3; j < 14; ++j do temp[j - 1] ← temp[j] end
temp[13] store
right *temp
end
return eText
end
var plainText = "WELLDONEISBETTERTHANWELLSAID"
writeLine("The original plaintext is : " + plainText)
writeLine(EOL + "The left and right alphabets after each permutation during encryption are :" + EOL)
var cipherText = exec(plainText, Chaocipher:Mode.ENCRYPT, true)
writeLine(EOL + "The ciphertext is : " + cipherText)
var plainText2 = exec(cipherText, Chaocipher:Mode.DECRYPT, false)
writeLine(EOL + "The recovered plaintext is : " + plainText2)
var plainText "WELLDONEISBETTERTHANWELLSAID"
writeLine("The original plaintext is : ", plainText)
writeLine(EOL, "The left and right alphabets after each permutation during encryption are :", EOL)
var cipherText exec(plainText, Chaocipher:Mode.ENCRYPT, true)
writeLine(EOL, "The ciphertext is : ", cipherText)
var plainText2 exec(cipherText, Chaocipher:Mode.DECRYPT, false)
writeLine(EOL, "The recovered plaintext is : ", plainText2)

View file

@ -0,0 +1,58 @@
# If using jaq, then see remark above regarding `inform/1`
# Convenience function for emitting progress messages:
def inform(msg):
(msg | tostring | stderr | empty), .;
# $encrypt should be one of true (for encryption) or false (for decryption)
def Chao($encrypt; $showSteps):
. as $text
| length as $len
| if any(explode[]; . > 128)
then "Text contains non-ASCII characters." | error
else {left: "HXUCZVAMDSLKPEFJRIGTWOBNYQ",
right: "PTLNBQDEOYSFAVZKGJRIHWXUMC",
eText: "",
temp: [range(0;26) | ""]
}
| reduce range(0; $len) as $i (.;
if $showSteps then inform("\(.left) \(.right)\n") end
| if $encrypt
then .index = (.left|index($text[$i:$i+1]))
| .eText += .right[.index:.index+1]
else .index = (.right|index($text[$i:$i+1]))
| .eText += .left[.index:.index+1]
end
| if $i == $len - 1 then .
else # permute left
reduce range(.index; 26) as $j (.; .temp[$j-.index] = .left[$j:$j+1])
| reduce range(0; .index) as $j (.; .temp[26-.index+$j] = .left[$j:$j+1])
| .store = .temp[1]
| reduce range(2; 14) as $j (.; .temp[$j-1] = .temp[$j])
| .temp[13] = .store
| .left = (.temp | join(""))
# permute right
| reduce range(.index; 26) as $j (.; .temp[$j-.index] = .right[$j:$j+1])
| reduce range(0; .index) as $j (.; .temp[26-.index+$j] = .right[$j:$j+1])
| .store = .temp[0]
| reduce range(1; 26) as $j (.; .temp[$j-1] = .temp[$j])
| .temp[25] = .store
| .store = .temp[2]
| reduce range(3; 14) as $j (.; .temp[$j-1] = .temp[$j])
| .temp[13] = .store
| .right = (.temp | join(""))
end )
| .eText
end ;
# Input: plaintext
def task:
"The original plaintext is : \(.)",
"The left and right alphabets after each permutation during encryption are:",
(Chao(true; true)
| "The ciphertext is : \(.)",
"The recovered plaintext is : \(Chao(false; false))" );
"WELLDONEISBETTERTHANWELLSAID" | task