Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
|
|
@ -58,4 +58,3 @@ Suggest a way in which the cipher could be modified so that ALL 26 letters can b
|
|||
;Related task
|
||||
[[Playfair_cipher|Playfair cipher]]
|
||||
<br><br>
|
||||
|
||||
|
|
|
|||
394
Task/Bifid-cipher/AArch64-Assembly/bifid-cipher.aarch64
Normal file
394
Task/Bifid-cipher/AArch64-Assembly/bifid-cipher.aarch64
Normal file
|
|
@ -0,0 +1,394 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* program bifid64.s */
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
|
||||
.equ MESSSIZE, 2000
|
||||
.equ BUFFERSIZE, 2000
|
||||
/*******************************************/
|
||||
/* Macros */
|
||||
/*******************************************/
|
||||
//.include "../../ficmacros64.inc" // for developer debugging
|
||||
|
||||
|
||||
/*******************************************/
|
||||
/* Initialized data */
|
||||
/*******************************************/
|
||||
.data
|
||||
szMessDebutPgm: .asciz "Program 64 bits start. \n"
|
||||
szCarriageReturn: .asciz "\n"
|
||||
szMessFinOK: .asciz "Program normal end. \n"
|
||||
szMessError: .asciz "\nError Buffer too small!!!\n"
|
||||
szMessString: .asciz "String :\n"
|
||||
szMessEncrip: .asciz "\nEncrypted :\n"
|
||||
szMessDecrip: .asciz "\nDecrypted :\n"
|
||||
//szString1: .asciz "ABCDEFGHIJKLMNOPQRSTUVWXYZ!.?abcdefghijklmnopqrstuvwxyz"
|
||||
//szString1: .asciz "attackatdawn"
|
||||
szString1: .asciz "ATTACKATDAWN"
|
||||
//szString1: .asciz "ABCD"
|
||||
szString2: .asciz "FLEEATONCE"
|
||||
szString3: .asciz "The invasion will start on the first of January"
|
||||
|
||||
szPolybe1: .ascii "ABCDE"
|
||||
.ascii "FGHIK"
|
||||
.ascii "LMNOP"
|
||||
.ascii "QRSTU"
|
||||
.asciz "VWXYZ"
|
||||
|
||||
szPolybe2: .ascii "BGWKZ"
|
||||
.ascii "QPNDS"
|
||||
.ascii "IOAXE"
|
||||
.ascii "FCLUM"
|
||||
.asciz "THYVR"
|
||||
|
||||
/*******************************************/
|
||||
/* UnInitialized data */
|
||||
/*******************************************/
|
||||
.bss
|
||||
.align 4
|
||||
tabiposX: .skip 8 * MESSSIZE
|
||||
tabiposY: .skip 8 * MESSSIZE
|
||||
sBuffex1: .skip BUFFERSIZE
|
||||
sBuffex2: .skip BUFFERSIZE
|
||||
/*******************************************/
|
||||
/* code section */
|
||||
/*******************************************/
|
||||
.text
|
||||
.global main
|
||||
main:
|
||||
ldr x0,qAdrszMessDebutPgm
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszMessString // display message
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszString1 // display string
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszString1 // string address
|
||||
ldr x1,qAdrszPolybe1 // Polybe key
|
||||
ldr x2,qAdrsBuffex1 // encrypted buffer result
|
||||
bl encrypt
|
||||
cmp x0,#0
|
||||
ble 99f
|
||||
ldr x0,qAdrszMessEncrip
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsBuffex1 // display encrypted buffer
|
||||
bl affichageMess
|
||||
|
||||
|
||||
ldr x0,qAdrsBuffex1 // encrypted buffer
|
||||
ldr x1,qAdrszPolybe1 // Polybe key
|
||||
ldr x2,qAdrsBuffex2 // decrypted buffer
|
||||
bl decrypt
|
||||
ldr x0,qAdrszMessDecrip
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsBuffex2 // display decrypted buffer
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszCarriageReturn
|
||||
bl affichageMess
|
||||
|
||||
ldr x0,qAdrszMessString // display message
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszString2 // display string
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszString2 // string address
|
||||
ldr x1,qAdrszPolybe2 // Polybe key
|
||||
ldr x2,qAdrsBuffex1 // encrypted buffer result
|
||||
bl encrypt
|
||||
cmp x0,#0
|
||||
ble 99f
|
||||
ldr x0,qAdrszMessEncrip
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsBuffex1 // display encrypted buffer
|
||||
bl affichageMess
|
||||
|
||||
|
||||
ldr x0,qAdrsBuffex1 // encrypted buffer
|
||||
ldr x1,qAdrszPolybe2 // Polybe key
|
||||
ldr x2,qAdrsBuffex2 // decrypted buffer
|
||||
bl decrypt
|
||||
ldr x0,qAdrszMessDecrip
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsBuffex2 // display decrypted buffer
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszCarriageReturn
|
||||
bl affichageMess
|
||||
|
||||
ldr x0,qAdrszMessString // display message
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszString1 // display string
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszString1 // string address
|
||||
ldr x1,qAdrszPolybe2 // Polybe key
|
||||
ldr x2,qAdrsBuffex1 // encrypted buffer result
|
||||
bl encrypt
|
||||
cmp x0,#0
|
||||
ble 99f
|
||||
ldr x0,qAdrszMessEncrip
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsBuffex1 // display encrypted buffer
|
||||
bl affichageMess
|
||||
|
||||
|
||||
ldr x0,qAdrsBuffex1 // encrypted buffer
|
||||
ldr x1,qAdrszPolybe2 // Polybe key
|
||||
ldr x2,qAdrsBuffex2 // decrypted buffer
|
||||
bl decrypt
|
||||
ldr x0,qAdrszMessDecrip
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsBuffex2 // display decrypted buffer
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszCarriageReturn
|
||||
bl affichageMess
|
||||
|
||||
ldr x0,qAdrszMessString // display message
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszString3 // display string
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszString3 // string address
|
||||
ldr x1,qAdrszPolybe1 // Polybe key
|
||||
ldr x2,qAdrsBuffex1 // encrypted buffer result
|
||||
bl encrypt
|
||||
cmp x0,#0
|
||||
ble 99f
|
||||
ldr x0,qAdrszMessEncrip
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsBuffex1 // display encrypted buffer
|
||||
bl affichageMess
|
||||
|
||||
|
||||
ldr x0,qAdrsBuffex1 // encrypted buffer
|
||||
ldr x1,qAdrszPolybe1 // Polybe key
|
||||
ldr x2,qAdrsBuffex2 // decrypted buffer
|
||||
bl decrypt
|
||||
ldr x0,qAdrszMessDecrip
|
||||
bl affichageMess
|
||||
ldr x0,qAdrsBuffex2 // display decrypted buffer
|
||||
bl affichageMess
|
||||
ldr x0,qAdrszCarriageReturn
|
||||
bl affichageMess
|
||||
|
||||
ldr x0,qAdrszMessFinOK
|
||||
bl affichageMess
|
||||
b 100f
|
||||
99:
|
||||
ldr x0,qAdrszMessError // error
|
||||
bl affichageMess
|
||||
mov x0, #1
|
||||
100: // standard end of the program
|
||||
mov x0, #0 // return code
|
||||
mov x8,EXIT
|
||||
svc 0 // perform system call
|
||||
qAdrszMessString: .quad szMessString
|
||||
qAdrszMessDecrip: .quad szMessDecrip
|
||||
qAdrszMessEncrip: .quad szMessEncrip
|
||||
qAdrszString1: .quad szString1
|
||||
qAdrszPolybe1: .quad szPolybe1
|
||||
qAdrszString2: .quad szString2
|
||||
qAdrszString3: .quad szString3
|
||||
qAdrszPolybe2: .quad szPolybe2
|
||||
qAdrsBuffex1: .quad sBuffex1
|
||||
qAdrsBuffex2: .quad sBuffex2
|
||||
qAdrszMessDebutPgm: .quad szMessDebutPgm
|
||||
qAdrszMessFinOK: .quad szMessFinOK
|
||||
qAdrszCarriageReturn: .quad szCarriageReturn
|
||||
qAdrszMessError: .quad szMessError
|
||||
/******************************************************************/
|
||||
/* encrypt strings */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the string1 */
|
||||
/* r1 contains Polybe area address
|
||||
/* r2 contains the address of the encrypted string */
|
||||
/* r0 return buffer lenght */
|
||||
encrypt:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x3,[sp,-16]! //
|
||||
stp x4,x5,[sp,-16]! //
|
||||
stp x6,x7,[sp,-16]! //
|
||||
stp x8,x9,[sp,-16]! //
|
||||
stp x10,x11,[sp,-16]! //
|
||||
mov x3,#0 // counter byte string 1
|
||||
mov x5,#0 // counter byte buffer
|
||||
1:
|
||||
ldrb w7,[x0,x3] // load byte string 1
|
||||
cmp x7,#0 // zero final ?
|
||||
beq 4f
|
||||
cmp x7,#65 // < A ?
|
||||
cinc x3,x3,lt
|
||||
blt 1b
|
||||
cmp x7,#90 // > Z
|
||||
ble 2f
|
||||
|
||||
cmp x7,#97 // < a ?
|
||||
cinc x3,x3,lt
|
||||
blt 1b
|
||||
cmp x7,#122 //> z
|
||||
cinc x3,x3,gt
|
||||
bgt 1b
|
||||
sub x7,x7,#32 // convert minuscul to majuscule
|
||||
2:
|
||||
mov x11,'I'
|
||||
cmp x7,#'J' // change J to I
|
||||
csel x7,x11,x7,eq
|
||||
ldr x11,qAdrtabiposX
|
||||
ldr x12,qAdrtabiposY
|
||||
mov x6,#0
|
||||
mov x9,#5
|
||||
3: // loop to search char in polybe square
|
||||
ldrb w8,[x1,x6] // load byte polybe
|
||||
cmp x8,x7 // equal ?
|
||||
cinc x6,x6,ne
|
||||
bne 3b
|
||||
udiv x10,x6,x9 // compute coordonnés
|
||||
msub x8,x10,x9,x6
|
||||
strb w10,[x11,x5] // save X position
|
||||
strb w8,[x12,x5] // save Y position
|
||||
add x5,x5,#1 // increment indice
|
||||
add x3,x3,#1
|
||||
b 1b // and loop
|
||||
|
||||
4:
|
||||
mov x6,#0
|
||||
mov x8,x5
|
||||
mov x5,#0
|
||||
mov x3,#5
|
||||
5: // convert position line 1 in char
|
||||
ldrb w9,[x11,x6]
|
||||
add x6,x6,#1
|
||||
cmp x6,x8 // first line end ?
|
||||
csel x6,xzr,x6,ge
|
||||
bge 7f // jmp for second line
|
||||
|
||||
ldrb w7,[x11,x6] // compute rank with position X and Y
|
||||
mul x9,x3,x9
|
||||
add x9,x9,x7
|
||||
ldrb w9,[x1,x9] // load polybe char
|
||||
strb w9,[x2,x5]
|
||||
add x5,x5,#1
|
||||
|
||||
add x6,x6,#1
|
||||
cmp x6,x8
|
||||
blt 5b
|
||||
|
||||
mov x6,#0
|
||||
6: // convert position line 2 in char
|
||||
ldrb w9,[x12,x6]
|
||||
add x6,x6,#1
|
||||
7:
|
||||
ldrb w7,[x12,x6] // compute rank with position X and Y
|
||||
mul x9,x3,x9
|
||||
add x9,x9,x7
|
||||
ldrb w9,[x1,x9] // load polybe char
|
||||
strb w9,[x2,x5]
|
||||
add x5,x5,#1
|
||||
add x6,x6,#1
|
||||
cmp x6,x8
|
||||
blt 6b
|
||||
|
||||
mov x9,#0 // zero final
|
||||
strb w9,[x2,x5]
|
||||
mov x0,x5
|
||||
100:
|
||||
ldp x10,x11,[sp],16 // restau registers
|
||||
ldp x8,x9,[sp],16 //
|
||||
ldp x6,x7,[sp],16 //
|
||||
ldp x4,x5,[sp],16 //
|
||||
ldp x2,x3,[sp],16 //
|
||||
ldp x1,lr,[sp],16 //
|
||||
ret
|
||||
qAdrtabiposX: .quad tabiposX
|
||||
qAdrtabiposY: .quad tabiposY
|
||||
/******************************************************************/
|
||||
/* decrypt strings */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the encrypted string1 */
|
||||
/* r1 contains Polybe area address
|
||||
/* r2 contains the address of the decrypted string */
|
||||
/* r0 return buffer lenght */
|
||||
decrypt:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
stp x2,x3,[sp,-16]! //
|
||||
stp x4,x5,[sp,-16]! //
|
||||
stp x6,x7,[sp,-16]! //
|
||||
stp x8,x9,[sp,-16]! //
|
||||
stp x10,x11,[sp,-16]! //
|
||||
mov x3,#0 // counter byte string 1
|
||||
mov x5,#0 // counter byte result buffer
|
||||
1:
|
||||
ldrb w7,[x0,x3] // load byte string 1
|
||||
cmp x7,#0 // zero final ?
|
||||
csel x0,x5,x0,eq
|
||||
beq 4f
|
||||
|
||||
cmp x7,#65 // < A ?
|
||||
cinc x3,x3,lt
|
||||
blt 1b
|
||||
cmp x7,#90 // > Z
|
||||
ble 2f
|
||||
|
||||
cmp x7,#97 // < a ?
|
||||
cinc x3,x3,lt
|
||||
blt 1b
|
||||
cmp x7,#122 //> z
|
||||
cinc x3,x3,gt
|
||||
bgt 1b
|
||||
sub x7,x7,#32 // convert minuscul to majuscule
|
||||
2:
|
||||
ldr x11,qAdrtabiposX
|
||||
mov x6,#0
|
||||
mov x9,#5
|
||||
3: // loop to search char in polybe square
|
||||
ldrb w8,[x1,x6] // load byte polybe
|
||||
cmp x8,x7
|
||||
cinc x6,x6,ne
|
||||
bne 3b
|
||||
|
||||
udiv x10,x6,x9
|
||||
msub x8,x10,x9,x6
|
||||
strb w10,[x11,x5]
|
||||
add x5,x5,#1
|
||||
strb w8,[x11,x5]
|
||||
add x5,x5,#1
|
||||
|
||||
add x3,x3,#1
|
||||
b 1b
|
||||
|
||||
4:
|
||||
mov x6,#0 // start indice first line
|
||||
lsr x8,x5,#1
|
||||
mov x10,x8 // start indice second line
|
||||
mov x5,#0
|
||||
mov x3,#5
|
||||
|
||||
5:
|
||||
ldrb w9,[x11,x6] // load position
|
||||
ldrb w7,[x11,x10]
|
||||
mul x9,x3,x9 // compute rank
|
||||
add x9,x9,x7
|
||||
ldrb w9,[x1,x9] // load polybe byte
|
||||
strb w9,[x2,x5] // and store to result
|
||||
add x5,x5,#1
|
||||
add x10,x10,#1
|
||||
add x6,x6,#1
|
||||
cmp x6,x8 // end ?
|
||||
blt 5b
|
||||
|
||||
mov x9,#0 // 0 final
|
||||
strb w9,[x2,x5]
|
||||
mov x0,x5
|
||||
100:
|
||||
ldp x10,x11,[sp],16 // restaur registers
|
||||
ldp x8,x9,[sp],16 //
|
||||
ldp x6,x7,[sp],16 //
|
||||
ldp x4,x5,[sp],16 //
|
||||
ldp x2,x3,[sp],16 //
|
||||
ldp x1,lr,[sp],16 //
|
||||
ret
|
||||
|
||||
/***************************************************/
|
||||
/* ROUTINES INCLUDE */
|
||||
/***************************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeARM64.inc"
|
||||
141
Task/Bifid-cipher/ALGOL-68/bifid-cipher.alg
Normal file
141
Task/Bifid-cipher/ALGOL-68/bifid-cipher.alg
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
BEGIN # Bifid cipher #
|
||||
|
||||
# mode to hold the details of a Bifid cipher #
|
||||
MODE BIFID = STRUCT( STRING key # the Polybius square, linearised #
|
||||
, STRING pre from # pre-translation, "from" string #
|
||||
, STRING pre to # pre-tramslatiion "to" string #
|
||||
);
|
||||
|
||||
# string utilities #
|
||||
|
||||
# returns the length of s #
|
||||
OP LENGTH = ( STRING s )INT: ( UPB s - LWB s ) + 1;
|
||||
# returns the index of c in s, or LWB s - 1 if it is not present #
|
||||
PRIO INDEXOF = 1;
|
||||
OP INDEXOF = ( STRING s, CHAR c )INT:
|
||||
BEGIN
|
||||
INT index := LWB s - 1;
|
||||
VOID( char in string( c, index, s ) ); # discard the result #
|
||||
index
|
||||
END # INDEXOF # ;
|
||||
# returns text with the characters in from chars replaced with the #
|
||||
# corresponding characters in to chars #
|
||||
PROC translate = ( STRING text, from chars, to chars )STRING:
|
||||
IF LWB from chars /= LWB to chars OR UPB from chars /= UPB to chars
|
||||
THEN """from"" and ""to"" have different bounds (translate)"
|
||||
ELIF from chars = ""
|
||||
THEN text
|
||||
ELSE # the from and to strings have the same bounds #
|
||||
STRING result := text;
|
||||
FOR r pos FROM LWB result TO UPB result DO
|
||||
INT t pos = from chars INDEXOF result[ r pos ];
|
||||
IF t pos >= LWB from chars THEN result[ r pos ] := to chars[ t pos ] FI
|
||||
OD;
|
||||
result
|
||||
FI # translate # ;
|
||||
# end string utilities #
|
||||
|
||||
# returns text encrypted according to cipher #
|
||||
PRIO ENCRYPT = 9;
|
||||
OP ENCRYPT = ( STRING text, BIFID cipher )STRING:
|
||||
IF STRING key = ( key OF cipher )[ AT 1 ];
|
||||
INT key length = LENGTH key;
|
||||
INT sq width = ENTIER sqrt( key length );
|
||||
sq width * sq width /= key length
|
||||
THEN "**** Bifid key is not a square"
|
||||
ELSE # the key is a square #
|
||||
STRING msg1 = translate( text, pre from OF cipher, pre to OF cipher )[ AT 1 ];
|
||||
STRING msg := "";
|
||||
FOR m pos FROM LWB msg1 TO UPB msg1 DO
|
||||
IF msg1[ m pos ] /= " " THEN msg +:= msg1[ m pos ] FI
|
||||
OD;
|
||||
INT m max = LENGTH msg;
|
||||
[ 1 : 2 * m max ]INT coordinates;
|
||||
FOR m pos TO m max DO
|
||||
INT c index := key INDEXOF msg[ m pos ];
|
||||
IF c index >= LWB key THEN c index -:= 1 FI;
|
||||
coordinates[ m pos ] := c index OVER sq width;
|
||||
coordinates[ m pos + m max ] := c index MOD sq width
|
||||
OD;
|
||||
STRING result := "";
|
||||
FOR c pos BY 2 TO UPB coordinates DO
|
||||
result +:= key[ ( coordinates[ c pos ] * sq width )
|
||||
+ coordinates[ c pos + 1 ]
|
||||
+ 1
|
||||
]
|
||||
OD;
|
||||
result
|
||||
FI # ENCRYPT # ;
|
||||
|
||||
# returns text decrypted according to cipher #
|
||||
PRIO DECRYPT = 9;
|
||||
OP DECRYPT = ( STRING text, BIFID cipher )STRING:
|
||||
IF STRING key = ( key OF cipher )[ AT 1 ];
|
||||
INT key length = LENGTH key;
|
||||
INT sq width = ENTIER sqrt( key length );
|
||||
sq width * sq width /= key length
|
||||
THEN "**** Bifid key is not a square"
|
||||
ELSE # the key is a square #
|
||||
STRING msg = text[ AT 1 ];
|
||||
INT m max = LENGTH msg;
|
||||
[ 1 : 2 * m max ]INT coordinates;
|
||||
INT c pos := 0;
|
||||
FOR m pos TO m max DO
|
||||
INT c index := key INDEXOF msg[ m pos ];
|
||||
IF c index >= LWB key THEN c index -:= 1 FI;
|
||||
coordinates[ c pos +:= 1 ] := c index OVER sq width;
|
||||
coordinates[ c pos +:= 1 ] := c index MOD sq width
|
||||
OD;
|
||||
STRING result := "";
|
||||
FOR i TO m max DO
|
||||
result +:= key[ ( coordinates[ i ] * sq width )
|
||||
+ coordinates[ i + m max ]
|
||||
+ 1
|
||||
]
|
||||
OD;
|
||||
result
|
||||
FI # DECRYPT # ;
|
||||
|
||||
# tests the ENCRYPT and DECRYPT operators #
|
||||
PROC test bifid = ( BIFID cipher, STRING text )VOID:
|
||||
BEGIN
|
||||
STRING code = text ENCRYPT cipher;
|
||||
print( ( text, " -> ", code, newline ) );
|
||||
print( ( code, " -> ", code DECRYPT cipher, newline ) )
|
||||
END # text bifid # ;
|
||||
|
||||
# Bifid ciphers for tests #
|
||||
|
||||
BIFID bifid 5 abc = ( "ABCDEFGHIKLMNOPQRSTUVWXYZ"
|
||||
, "Jabcdefghijklmnopqrstuvwxyz"
|
||||
, "IABCDEFGHIIKLMNOPQRSTUVWXYZ"
|
||||
);
|
||||
BIFID bifid 5 bgw = ( "BGWKZQPNDSIOAXEFCLUMTHYVR"
|
||||
, "Jabcdefghijklmnopqrstuvwxyz"
|
||||
, "IABCDEFGHIIKLMNOPQRSTUVWXYZ"
|
||||
);
|
||||
BIFID bifid 6 abc = ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
, "abcdefghijklmnopqrstuvwxyz"
|
||||
, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
);
|
||||
BIFID bifid 8 abc = ( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\/"
|
||||
, " "
|
||||
, "/"
|
||||
);
|
||||
|
||||
# basic task - using 5x5 Polybius squares #
|
||||
test bifid( bifid 5 abc, "ATTACKATDAWN" );
|
||||
print( ( newline ) );
|
||||
test bifid( bifid 5 bgw, "FLEEATONCE" );
|
||||
test bifid( bifid 5 bgw, "ATTACKATDAWN" );
|
||||
test bifid( bifid 5 bgw, "The invasion will begin on the first of January" );
|
||||
print( ( newline ) );
|
||||
|
||||
# bonus - using a 6x6 square #
|
||||
test bifid( bifid 6 abc, "The invasion will begin on the first of January" );
|
||||
print( ( newline ) );
|
||||
|
||||
# bonus - using an 8x8 square #
|
||||
test bifid( bifid 8 abc, "The invasion will begin on the first of January" )
|
||||
|
||||
END
|
||||
376
Task/Bifid-cipher/ARM-Assembly/bifid-cipher.arm
Normal file
376
Task/Bifid-cipher/ARM-Assembly/bifid-cipher.arm
Normal file
|
|
@ -0,0 +1,376 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program bifid.s */
|
||||
/* REMARK 1 : this program use routines in a include file
|
||||
see task Include a file language arm assembly
|
||||
for the routine affichageMess conversion10
|
||||
see at end of this program the instruction include */
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes */
|
||||
/*******************************************/
|
||||
.include "../constantes.inc"
|
||||
|
||||
.equ MESSSIZE, 2000
|
||||
.equ BUFFERSIZE, 2000
|
||||
/*******************************************/
|
||||
/* Macros */
|
||||
/*******************************************/
|
||||
//.include "../../ficmacros32.inc" @ for developer debugging
|
||||
|
||||
|
||||
/*******************************************/
|
||||
/* Initialized data */
|
||||
/*******************************************/
|
||||
.data
|
||||
szMessDebutPgm: .asciz "Program 32 bits start. \n"
|
||||
szCarriageReturn: .asciz "\n"
|
||||
szMessFinOK: .asciz "Program normal end. \n"
|
||||
szMessError: .asciz "\nError Buffer too small!!!\n"
|
||||
szMessString: .asciz "String :\n"
|
||||
szMessEncrip: .asciz "\nEncrypted :\n"
|
||||
szMessDecrip: .asciz "\nDecrypted :\n"
|
||||
//szString1: .asciz "ABCDEFGHIJKLMNOPQRSTUVWXYZ!.?abcdefghijklmnopqrstuvwxyz"
|
||||
//szString1: .asciz "attackatdawn"
|
||||
szString1: .asciz "ATTACKATDAWN"
|
||||
//szString1: .asciz "ABCD"
|
||||
szString2: .asciz "FLEEATONCE"
|
||||
szString3: .asciz "L'invasion commencera le premier janvier"
|
||||
|
||||
szPolybe1: .ascii "ABCDE"
|
||||
.ascii "FGHIK"
|
||||
.ascii "LMNOP"
|
||||
.ascii "QRSTU"
|
||||
.asciz "VWXYZ"
|
||||
|
||||
szPolybe2: .ascii "BGWKZ"
|
||||
.ascii "QPNDS"
|
||||
.ascii "IOAXE"
|
||||
.ascii "FCLUM"
|
||||
.asciz "THYVR"
|
||||
|
||||
/*******************************************/
|
||||
/* UnInitialized data */
|
||||
/*******************************************/
|
||||
.bss
|
||||
.align 4
|
||||
tabiposX: .skip 4 * MESSSIZE
|
||||
tabiposY: .skip 4 * MESSSIZE
|
||||
sBuffer1: .skip BUFFERSIZE
|
||||
sBuffer2: .skip BUFFERSIZE
|
||||
/*******************************************/
|
||||
/* code section */
|
||||
/*******************************************/
|
||||
.text
|
||||
.global main
|
||||
main:
|
||||
ldr r0,iAdrszMessDebutPgm
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszMessString @ display message
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString1 @ display string
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString1 @ string address
|
||||
ldr r1,iAdrszPolybe1 @ Polybe key
|
||||
ldr r2,iAdrsBuffer1 @ encrypted buffer result
|
||||
bl encrypt
|
||||
cmp r0,#0
|
||||
ble 99f
|
||||
ldr r0,iAdrszMessEncrip
|
||||
bl affichageMess
|
||||
ldr r0,iAdrsBuffer1 @ display encrypted buffer
|
||||
bl affichageMess
|
||||
|
||||
|
||||
ldr r0,iAdrsBuffer1 @ encrypted buffer
|
||||
ldr r1,iAdrszPolybe1 @ Polybe key
|
||||
ldr r2,iAdrsBuffer2 @ decrypted buffer
|
||||
bl decrypt
|
||||
ldr r0,iAdrszMessDecrip
|
||||
bl affichageMess
|
||||
ldr r0,iAdrsBuffer2 @ display decrypted buffer
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszCarriageReturn
|
||||
bl affichageMess
|
||||
|
||||
ldr r0,iAdrszMessString @ display message
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString2 @ display string
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString2 @ string address
|
||||
ldr r1,iAdrszPolybe2 @ Polybe key
|
||||
ldr r2,iAdrsBuffer1 @ encrypted buffer result
|
||||
bl encrypt
|
||||
cmp r0,#0
|
||||
ble 99f
|
||||
ldr r0,iAdrszMessEncrip
|
||||
bl affichageMess
|
||||
ldr r0,iAdrsBuffer1 @ display encrypted buffer
|
||||
bl affichageMess
|
||||
|
||||
|
||||
ldr r0,iAdrsBuffer1 @ encrypted buffer
|
||||
ldr r1,iAdrszPolybe2 @ Polybe key
|
||||
ldr r2,iAdrsBuffer2 @ decrypted buffer
|
||||
bl decrypt
|
||||
ldr r0,iAdrszMessDecrip
|
||||
bl affichageMess
|
||||
ldr r0,iAdrsBuffer2 @ display decrypted buffer
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszCarriageReturn
|
||||
bl affichageMess
|
||||
|
||||
ldr r0,iAdrszMessString @ display message
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString1 @ display string
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString1 @ string address
|
||||
ldr r1,iAdrszPolybe2 @ Polybe key
|
||||
ldr r2,iAdrsBuffer1 @ encrypted buffer result
|
||||
bl encrypt
|
||||
cmp r0,#0
|
||||
ble 99f
|
||||
ldr r0,iAdrszMessEncrip
|
||||
bl affichageMess
|
||||
ldr r0,iAdrsBuffer1 @ display encrypted buffer
|
||||
bl affichageMess
|
||||
|
||||
|
||||
ldr r0,iAdrsBuffer1 @ encrypted buffer
|
||||
ldr r1,iAdrszPolybe2 @ Polybe key
|
||||
ldr r2,iAdrsBuffer2 @ decrypted buffer
|
||||
bl decrypt
|
||||
ldr r0,iAdrszMessDecrip
|
||||
bl affichageMess
|
||||
ldr r0,iAdrsBuffer2 @ display decrypted buffer
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszCarriageReturn
|
||||
bl affichageMess
|
||||
|
||||
ldr r0,iAdrszMessString @ display message
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString3 @ display string
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszString3 @ string address
|
||||
ldr r1,iAdrszPolybe1 @ Polybe key
|
||||
ldr r2,iAdrsBuffer1 @ encrypted buffer result
|
||||
bl encrypt
|
||||
cmp r0,#0
|
||||
ble 99f
|
||||
ldr r0,iAdrszMessEncrip
|
||||
bl affichageMess
|
||||
ldr r0,iAdrsBuffer1 @ display encrypted buffer
|
||||
bl affichageMess
|
||||
|
||||
|
||||
ldr r0,iAdrsBuffer1 @ encrypted buffer
|
||||
ldr r1,iAdrszPolybe1 @ Polybe key
|
||||
ldr r2,iAdrsBuffer2 @ decrypted buffer
|
||||
bl decrypt
|
||||
ldr r0,iAdrszMessDecrip
|
||||
bl affichageMess
|
||||
ldr r0,iAdrsBuffer2 @ display decrypted buffer
|
||||
bl affichageMess
|
||||
ldr r0,iAdrszCarriageReturn
|
||||
bl affichageMess
|
||||
|
||||
ldr r0,iAdrszMessFinOK
|
||||
bl affichageMess
|
||||
b 100f
|
||||
99:
|
||||
ldr r0,iAdrszMessError @ error
|
||||
bl affichageMess
|
||||
mov r0, #1
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
mov r7, #EXIT @ request to exit program
|
||||
svc 0 @ perform system call
|
||||
iAdrszMessString: .int szMessString
|
||||
iAdrszMessDecrip: .int szMessDecrip
|
||||
iAdrszMessEncrip: .int szMessEncrip
|
||||
iAdrszString1: .int szString1
|
||||
iAdrszPolybe1: .int szPolybe1
|
||||
iAdrszString2: .int szString2
|
||||
iAdrszString3: .int szString3
|
||||
iAdrszPolybe2: .int szPolybe2
|
||||
iAdrsBuffer1: .int sBuffer1
|
||||
iAdrsBuffer2: .int sBuffer2
|
||||
iAdrszMessDebutPgm: .int szMessDebutPgm
|
||||
iAdrszMessFinOK: .int szMessFinOK
|
||||
iAdrszCarriageReturn: .int szCarriageReturn
|
||||
iAdrszMessError: .int szMessError
|
||||
/******************************************************************/
|
||||
/* encrypt strings */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the string1 */
|
||||
/* r1 contains Polybe area address
|
||||
/* r2 contains the address of the encrypted string */
|
||||
/* r0 return buffer lenght */
|
||||
encrypt:
|
||||
push {r3-r12,lr} @ save registers
|
||||
mov r3,#0 @ counter byte string 1
|
||||
mov r5,#0 @ counter byte buffer
|
||||
|
||||
1:
|
||||
ldrb r7,[r0,r3] @ load byte string 1
|
||||
cmp r7,#0 @ zero final ?
|
||||
beq 4f
|
||||
cmp r7,#65 @ < A ?
|
||||
addlt r3,#1
|
||||
blt 1b
|
||||
cmp r7,#90 @ > Z
|
||||
ble 2f
|
||||
|
||||
cmp r7,#97 @ < a ?
|
||||
addlt r3,#1
|
||||
blt 1b
|
||||
cmp r7,#122 @> z
|
||||
addgt r3,#1
|
||||
bgt 1b
|
||||
sub r7,#32 @ convert minuscul to majuscule
|
||||
2:
|
||||
cmp r7,#'J' @ change J to I
|
||||
moveq r7,#'I'
|
||||
ldr r11,iAdrtabiposX
|
||||
ldr r12,iAdrtabiposY
|
||||
mov r6,#0
|
||||
mov r9,#5
|
||||
3: @ loop to search char in polybe square
|
||||
ldrb r8,[r1,r6] @ load byte polybe
|
||||
cmp r8,r7 @ equal ?
|
||||
addne r6,#1
|
||||
bne 3b
|
||||
udiv r10,r6,r9 @ compute coordonnés
|
||||
mls r8,r10,r9,r6
|
||||
|
||||
strb r10,[r11,r5] @ save X position
|
||||
strb r8,[r12,r5] @ save Y position
|
||||
add r5,r5,#1 @ increment indice
|
||||
add r3,r3,#1
|
||||
b 1b @ and loop
|
||||
|
||||
4:
|
||||
mov r6,#0
|
||||
mov r8,r5
|
||||
mov r5,#0
|
||||
mov r3,#5
|
||||
5: @ convert position line 1 in char
|
||||
ldrb r9,[r11,r6]
|
||||
add r6,r6,#1
|
||||
cmp r6,r8 @ first line end ?
|
||||
movge r6,#0
|
||||
bge 7f @ jmp for second line
|
||||
|
||||
ldrb r7,[r11,r6] @ compute rank with position X and Y
|
||||
mul r9,r3,r9
|
||||
add r9,r9,r7
|
||||
ldrb r9,[r1,r9] @ load polybe char
|
||||
strb r9,[r2,r5]
|
||||
add r5,r5,#1
|
||||
|
||||
add r6,r6,#1
|
||||
cmp r6,r8
|
||||
blt 5b
|
||||
|
||||
mov r6,#0
|
||||
6: @ convert position line 2 in char
|
||||
ldrb r9,[r12,r6]
|
||||
add r6,r6,#1
|
||||
7:
|
||||
ldrb r7,[r12,r6] @ compute rank with position X and Y
|
||||
mul r9,r3,r9
|
||||
add r9,r9,r7
|
||||
ldrb r9,[r1,r9] @ load polybe char
|
||||
strb r9,[r2,r5]
|
||||
add r5,r5,#1
|
||||
add r6,r6,#1
|
||||
cmp r6,r8
|
||||
blt 6b
|
||||
|
||||
mov r9,#0 @ zero final
|
||||
strb r9,[r2,r5]
|
||||
mov r0,r5
|
||||
100:
|
||||
pop {r3-r12,pc} @ restaur registers
|
||||
iAdrtabiposX: .int tabiposX
|
||||
iAdrtabiposY: .int tabiposY
|
||||
/******************************************************************/
|
||||
/* decrypt strings */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the encrypted string1 */
|
||||
/* r1 contains Polybe area address
|
||||
/* r2 contains the address of the decrypted string */
|
||||
/* r0 return buffer lenght */
|
||||
decrypt:
|
||||
push {r3-r11,lr} @ save registers
|
||||
mov r3,#0 @ counter byte string 1
|
||||
mov r5,#0 @ counter byte result buffer
|
||||
1:
|
||||
ldrb r7,[r0,r3] @ load byte string 1
|
||||
cmp r7,#0 @ zero final ?
|
||||
moveq r0,r5
|
||||
beq 4f
|
||||
|
||||
cmp r7,#65 @ < A ?
|
||||
addlt r3,#1
|
||||
blt 1b
|
||||
cmp r7,#90 @ > Z
|
||||
ble 2f
|
||||
|
||||
cmp r7,#97 @ < a ?
|
||||
addlt r3,#1
|
||||
blt 1b
|
||||
cmp r7,#122 @> z
|
||||
addgt r3,#1
|
||||
bgt 1b
|
||||
sub r7,#32 @ convert minuscul to majuscule
|
||||
2:
|
||||
ldr r11,iAdrtabiposX
|
||||
mov r6,#0
|
||||
mov r9,#5
|
||||
3: @ loop to search char in polybe square
|
||||
ldrb r8,[r1,r6] @ load byte polybe
|
||||
cmp r8,r7
|
||||
addne r6,#1
|
||||
bne 3b
|
||||
|
||||
udiv r10,r6,r9
|
||||
mls r8,r10,r9,r6
|
||||
strb r10,[r11,r5]
|
||||
add r5,r5,#1
|
||||
strb r8,[r11,r5]
|
||||
add r5,r5,#1
|
||||
|
||||
add r3,#1
|
||||
b 1b
|
||||
|
||||
4:
|
||||
mov r6,#0 @ start indice first line
|
||||
lsr r8,r5,#1
|
||||
mov r10,r8 @ start indice second line
|
||||
mov r5,#0
|
||||
mov r3,#5
|
||||
|
||||
5:
|
||||
ldrb r9,[r11,r6] @ load position
|
||||
ldrb r7,[r11,r10]
|
||||
mul r9,r3,r9 @ compute rank
|
||||
add r9,r9,r7
|
||||
ldrb r9,[r1,r9] @ load polybe byte
|
||||
strb r9,[r2,r5] @ and store to result
|
||||
add r5,r5,#1
|
||||
add r10,r10,#1
|
||||
add r6,r6,#1
|
||||
cmp r6,r8 @ end ?
|
||||
blt 5b
|
||||
|
||||
mov r9,#0 @ 0 final
|
||||
strb r9,[r2,r5]
|
||||
mov r0,r5
|
||||
100:
|
||||
pop {r3-r11,pc} @ restaur registers
|
||||
|
||||
|
||||
/***************************************************/
|
||||
/* ROUTINES INCLUDE */
|
||||
/***************************************************/
|
||||
.include "../affichage.inc"
|
||||
|
|
@ -8,7 +8,7 @@ model
|
|||
int y, x ← 0
|
||||
for each text symbol in symbols
|
||||
me.grid[y][x] ← symbol
|
||||
me.map[symbol] ← int%int(y => x).named("y", "x")
|
||||
me.map[symbol] ← int%int(y ⇒ x).named("y", "x")
|
||||
x++
|
||||
if x æ side
|
||||
x ← 0
|
||||
|
|
@ -16,8 +16,8 @@ model
|
|||
end
|
||||
end
|
||||
end
|
||||
fun asText = text by block
|
||||
text result = ""
|
||||
fun asText ← text by block
|
||||
text result
|
||||
for int y ← 0; y < me.grid.length; y++
|
||||
for int x ← 0; x < me.grid[y].length; x++
|
||||
result.append(" " + me.grid[y][x])
|
||||
|
|
@ -64,19 +64,19 @@ model
|
|||
end
|
||||
type Main
|
||||
List pairs ← Pair[
|
||||
text%PolybiusSquare("ATTACKATDAWN" => PolybiusSquare(5, "ABCDEFGHIKLMNOPQRSTUVWXYZ")).named("sample", "square"),
|
||||
text%PolybiusSquare("FLEEATONCE" => PolybiusSquare(5, "BGWKZQPNDSIOAXEFCLUMTHYVR")).named("sample", "square"),
|
||||
text%PolybiusSquare("ATTACKATDAWN" => PolybiusSquare(5, "BGWKZQPNDSIOAXEFCLUMTHYVR")).named("sample", "square"),
|
||||
text%PolybiusSquare("The invasion will start on the first of January".upper().replace(" ", "") =>
|
||||
text%PolybiusSquare("ATTACKATDAWN" ⇒ PolybiusSquare(5, "ABCDEFGHIKLMNOPQRSTUVWXYZ")).named("sample", "square"),
|
||||
text%PolybiusSquare("FLEEATONCE" ⇒ PolybiusSquare(5, "BGWKZQPNDSIOAXEFCLUMTHYVR")).named("sample", "square"),
|
||||
text%PolybiusSquare("ATTACKATDAWN" ⇒ PolybiusSquare(5, "BGWKZQPNDSIOAXEFCLUMTHYVR")).named("sample", "square"),
|
||||
text%PolybiusSquare("The invasion will start on the first of January".upper().replace(" ", "") ⇒
|
||||
PolybiusSquare(6, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")).named("sample", "square")]
|
||||
for each Pair pair in pairs
|
||||
writeLine("Using Polybius square:")
|
||||
write(pair.square)
|
||||
BifidCipher cipher ← BifidCipher(pair.square)
|
||||
write("Encrypting '" + pair.sample + "'")
|
||||
write("Encrypting '", pair.sample, "'")
|
||||
text encrypted ← cipher.encrypt(pair.sample)
|
||||
write(" => '" + encrypted + "'")
|
||||
write(" ⇒ '", encrypted, "'")
|
||||
text decrypted ← cipher.decrypt(encrypted)
|
||||
writeLine(" => '" + decrypted + "'")
|
||||
writeLine(" ⇒ '", decrypted, "'")
|
||||
writeLine()
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,29 +1,4 @@
|
|||
func$ crypt enc msg$ key$ .
|
||||
n = len msg$
|
||||
for i to len msg$
|
||||
c$ = substr msg$ i 1
|
||||
j = strpos key$ c$ - 1
|
||||
h[] &= j div 5
|
||||
h[] &= j mod 5
|
||||
.
|
||||
if enc = 1
|
||||
for i = 1 step 4 to 2 * n - 3
|
||||
j = h[i] * 5 + h[i + 2] + 1
|
||||
r$ &= substr key$ j 1
|
||||
.
|
||||
for i = 2 step 4 to 2 * n - 2
|
||||
j = h[i] * 5 + h[i + 2] + 1
|
||||
r$ &= substr key$ j 1
|
||||
.
|
||||
else
|
||||
for i = 1 to n
|
||||
j = h[i] * 5 + h[i + n] + 1
|
||||
r$ &= substr key$ j 1
|
||||
.
|
||||
.
|
||||
return r$
|
||||
.
|
||||
func$ conv s$ .
|
||||
proc prepare . s$ .
|
||||
for e$ in strchars s$
|
||||
h = strcode e$
|
||||
if h >= 97
|
||||
|
|
@ -36,13 +11,36 @@ func$ conv s$ .
|
|||
r$ &= strchar h
|
||||
.
|
||||
.
|
||||
return r$
|
||||
swap s$ r$
|
||||
.
|
||||
func$ encr msg$ key$ .
|
||||
return crypt 1 conv msg$ key$
|
||||
prepare msg$
|
||||
len h[] len msg$ * 2
|
||||
for i to len msg$
|
||||
c$ = substr msg$ i 1
|
||||
j = strpos key$ c$ - 1
|
||||
h[i] = j div 5
|
||||
h[len msg$ + i] = j mod 5
|
||||
.
|
||||
for i = 1 step 2 to len h[] - 1
|
||||
j = h[i] * 5 + h[i + 1] + 1
|
||||
r$ &= substr key$ j 1
|
||||
.
|
||||
return r$
|
||||
.
|
||||
func$ decr msg$ key$ .
|
||||
return crypt 0 msg$ key$
|
||||
for i to len msg$
|
||||
c$ = substr msg$ i 1
|
||||
j = strpos key$ c$ - 1
|
||||
h[] &= j div 5
|
||||
h[] &= j mod 5
|
||||
.
|
||||
mid = len h[] div 2
|
||||
for i = 1 to mid
|
||||
j = h[i] * 5 + h[i + mid] + 1
|
||||
r$ &= substr key$ j 1
|
||||
.
|
||||
return r$
|
||||
.
|
||||
key$ = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
|
||||
h$ = encr "ATTACKATDAWN" key$
|
||||
|
|
@ -59,7 +57,6 @@ h$ = encr "ATTACKATDAWN" key$
|
|||
print h$
|
||||
print decr h$ key$
|
||||
print ""
|
||||
#
|
||||
h$ = encr "The invasion will start on the first of January" key$
|
||||
print h$
|
||||
print decr h$ key$
|
||||
|
|
|
|||
17
Task/Bifid-cipher/F-Sharp/bifid-cipher.fs
Normal file
17
Task/Bifid-cipher/F-Sharp/bifid-cipher.fs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// Bifid cipher. Nigel Galloway: September 17th., 2024
|
||||
let polybius alphabet n g=let ng=Seq.allPairs [1..n] [1..g] in (Seq.zip ng alphabet|>Map.ofSeq,Seq.zip alphabet ng|>Map.ofSeq)
|
||||
let fN (i,g) (e:Map<(int*int),char>)=i@g|>List.chunkBySize 2|>List.map(fun n->e[(n[0],n[1])])|>Array.ofList|>System.String
|
||||
let fG (n:Map<char,int*int>) g=g|>Seq.map(fun g->n[g])|>List.ofSeq|>List.unzip
|
||||
let encrypt (n:Map<(int * int),char>,g:Map<char,(int * int)>) text=fN (fG g text) n
|
||||
let decrypt (n:Map<(int * int),char>,g:Map<char,(int * int)>) text=
|
||||
let p,q=let p,q=fG g text in List.map2(fun p q->[p;q]) p q|>List.concat|>List.splitAt p.Length
|
||||
List.zip p q |>List.map(fun g->n[g])|>Array.ofList|>System.String
|
||||
|
||||
let p1=polybius "abcdefghiklmnopqrstuvwxyz" 5 5
|
||||
let s="attackatdawn" in let e=encrypt p1 s in printfn $"%s{s} -> %s{e} -> %s{decrypt p1 e}"
|
||||
let s="fleeatonce" in let e=encrypt p1 s in printfn $"%s{s} -> %s{e} -> %s{decrypt p1 e}"
|
||||
let p2=polybius "bgwkzqpndsioaxefclumthyvr" 5 5
|
||||
let s="attackatdawn" in let e=encrypt p2 s in printfn $"%s{s} -> %s{e} -> %s{decrypt p2 e}"
|
||||
let s="fleeatonce" in let e=encrypt p2 s in printfn $"%s{s} -> %s{e} -> %s{decrypt p2 e}"
|
||||
let p3=polybius "abcdefghijklmnopqrstuvwxyz0123456789" 6 6
|
||||
let s="theinvasionwillstarton1january2025" in let e=encrypt p3 s in printfn $"%s{s} -> %s{e} -> %s{decrypt p3 e}"
|
||||
81
Task/Bifid-cipher/FreeBASIC/bifid-cipher.basic
Normal file
81
Task/Bifid-cipher/FreeBASIC/bifid-cipher.basic
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
Dim Shared As String encrypted, decrypted
|
||||
|
||||
Sub BifidEncode(Byref polybius As String, Byref mensaje As String)
|
||||
Dim As String filas, columas
|
||||
Dim As Integer c, i, j, k, longi
|
||||
|
||||
encrypted = Ucase(mensaje)
|
||||
For i = 1 To Len(encrypted)
|
||||
If Mid(encrypted, i, 1) = Chr(10) Then Mid(encrypted, i, 1) = Chr(9)
|
||||
Next i
|
||||
|
||||
|
||||
i = 0 : j = 0
|
||||
While i < Len(encrypted)
|
||||
c = Asc(Mid(encrypted, i + 1, 1))
|
||||
i += 1
|
||||
k = Instr(polybius, Chr(c)) - 1
|
||||
If k >= 0 Then
|
||||
filas &= Chr(k \ 5 + Asc("0"))
|
||||
columas &= Chr(k Mod 5 + Asc("0"))
|
||||
j += 1
|
||||
End If
|
||||
Wend
|
||||
|
||||
longi = j
|
||||
filas &= columas
|
||||
|
||||
encrypted = ""
|
||||
For i = 0 To longi - 1
|
||||
k = (Asc(Mid(filas, i * 2 + 1, 1)) - Asc("0")) * 5 + (Asc(Mid(filas, i * 2 + 2, 1)) - Asc("0"))
|
||||
encrypted &= Mid(polybius, k + 1, 1)
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Sub BifidDecode(Byref polybius As String, Byref mensaje As String)
|
||||
Dim As String filas, columas
|
||||
Dim As Integer c, k, i, j, longi
|
||||
|
||||
i = 0 : j = 0
|
||||
While i < Len(mensaje)
|
||||
c = Asc(Mid(mensaje, i + 1, 1))
|
||||
i += 1
|
||||
k = Instr(polybius, Chr(c)) - 1
|
||||
filas &= Chr(k \ 5 + Asc("0"))
|
||||
filas &= Chr(k Mod 5 + Asc("0"))
|
||||
j += 2
|
||||
Wend
|
||||
|
||||
longi = i
|
||||
columas = Right(filas, longi)
|
||||
filas = Left(filas, longi)
|
||||
|
||||
decrypted = ""
|
||||
For i = 0 To longi - 1
|
||||
k = (Asc(Mid(filas, i + 1, 1)) - Asc("0")) * 5 + (Asc(Mid(columas, i + 1, 1)) - Asc("0"))
|
||||
decrypted &= Mid(polybius, k + 1, 1)
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
Dim As String polys(3), msgs(3)
|
||||
|
||||
polys(0) = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
|
||||
polys(1) = "BGWKZQPNDSIOAXEFCLUMTHYVR"
|
||||
polys(2) = "BGWKZQPNDSIOAXEFCLUMTHYVR"
|
||||
polys(3) = "PLAYFIREXMBCDGHKNOQSTUVWZ"
|
||||
|
||||
msgs(0) = "ATTACKATDAWN"
|
||||
msgs(1) = "FLEEATONCE"
|
||||
msgs(2) = "ATTACKATDAWN"
|
||||
msgs(3) = "The invasion will start on the first of January"
|
||||
|
||||
For i As Integer = 0 To Ubound(msgs)
|
||||
BifidEncode(polys(i), msgs(i))
|
||||
BifidDecode(polys(i), encrypted)
|
||||
Print "Mensaje : "; msgs(i)
|
||||
Print "Encrypted : "; encrypted
|
||||
Print "Decrypted : "; decrypted
|
||||
If i < Ubound(msgs) Then Print
|
||||
Next i
|
||||
|
||||
Sleep
|
||||
28
Task/Bifid-cipher/PascalABC.NET/bifid-cipher.pas
Normal file
28
Task/Bifid-cipher/PascalABC.NET/bifid-cipher.pas
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// Bifid cipher. Nigel Galloway: September 18th., 2024
|
||||
function polybius(alphabet:string;n,g:integer):(Dictionary<char,(integer,integer)>,Dictionary<(integer,integer),char>);
|
||||
begin
|
||||
var ng:=Cartesian(1..n,1..g);
|
||||
Result:=(Dict(Zip(alphabet,ng)),Dict(Zip(ng,alphabet)));
|
||||
end;
|
||||
function fN(e:Dictionary<char,(integer,integer)>;n:string):(sequence of integer,sequence of integer):=(n.Substring(1).Scan(e[n[1]],(n,g)->e[g])).UnZipTuple;
|
||||
function encrypt(e:(Dictionary<char,(integer,integer)>,Dictionary<(integer,integer),char>);text:string):string;
|
||||
begin
|
||||
var fG:((sequence of integer,sequence of integer),Dictionary<(integer,integer),char>)->string:=(n,e)->(n[0]+n[1]).Batch(2).Aggregate('',(n,g)->n+e[(g[0],g[1])]);
|
||||
Result:=fG(fN(e[0],text),e[1]);
|
||||
end;
|
||||
function decrypt(e:(Dictionary<char,(integer,integer)>,Dictionary<(integer,integer),char>);text:string):string;
|
||||
begin
|
||||
var fG:(sequence of (integer,integer),Dictionary<(integer,integer),char>)->string:=(n,e)->n.Aggregate('',(n,g)->n+e[(g[0],g[1])]);
|
||||
var t:=fN(e[0],text); var z:=t[0].Interleave(t[1]).SplitAt(t[0].count);
|
||||
Result:=fG(z[0].ZipTuple(z[1]),e[1]);
|
||||
end;
|
||||
begin
|
||||
var p:=polybius('abcdefghiklmnopqrstuvwxyz',5,5); var s,e:string;
|
||||
s:='fleeatonce'; e:=encrypt(p,s); println($'{s} -> {e} -> {decrypt(p,e)}');
|
||||
s:='attackatdawn'; e:=encrypt(p,s); println($'{s} -> {e} -> {decrypt(p,e)}');
|
||||
p:=polybius('bgwkzqpndsioaxefclumthyvr',5,5);
|
||||
e:=encrypt(p,s); println($'{s} -> {e} -> {decrypt(p,e)}');
|
||||
s:='fleeatonce'; e:=encrypt(p,s); println($'{s} -> {e} -> {decrypt(p,e)}');
|
||||
p:=polybius('abcdefghijklmnopqrstuvwxyz0123456789',6,6);
|
||||
s:='theinvasionwillstarton1january2025'; e:=encrypt(p,s); println($'{s} -> {e} -> {decrypt(p,e)}');
|
||||
end.
|
||||
101
Task/Bifid-cipher/XPL0/bifid-cipher.xpl0
Normal file
101
Task/Bifid-cipher/XPL0/bifid-cipher.xpl0
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
string 0; \use zero-terminated strings
|
||||
def MaxLen = 100; \more than length of longest message
|
||||
|
||||
func StrLen(A); \Return number of characters in an ASCIIZ string
|
||||
char A;
|
||||
int I;
|
||||
for I:= 0 to MaxLen do
|
||||
if A(I) = 0 then return I;
|
||||
|
||||
proc StrUpper(S); \In string S, convert lowercase characters to uppercase
|
||||
char S;
|
||||
int I;
|
||||
for I:= 0 to StrLen(S)-1 do
|
||||
if S(I)>=^a & S(I)<=^z then S(I):= S(I) & $DF;
|
||||
|
||||
proc StrReplace(S, C1, C2); \In string S, convert character C1 to C2
|
||||
char S, C1, C2;
|
||||
int I;
|
||||
for I:= 0 to StrLen(S)-1 do
|
||||
if S(I) = C1 then S(I):= C2;
|
||||
|
||||
func IndexOf(S, C); \Return offset of character C in string S
|
||||
char S, C;
|
||||
int I;
|
||||
[for I:= 0 to StrLen(S)-1 do
|
||||
if S(I) = C then return I;
|
||||
return -1;
|
||||
];
|
||||
|
||||
char Encrypted(MaxLen), Decrypted(MaxLen);
|
||||
|
||||
proc Encrypt(Polybius, Message); \Copy encrypted Message into Encrypted
|
||||
char Polybius, Message;
|
||||
char Rows(MaxLen*2), Cols(MaxLen);
|
||||
int C, IX, I, J, Len;
|
||||
[Len:= StrLen(Message);
|
||||
for I:= 0 to Len do \copy Message into Encrypted
|
||||
Encrypted(I):= Message(I);
|
||||
StrUpper(Encrypted);
|
||||
StrReplace(Encrypted, ^J, ^I);
|
||||
I:= 0; J:= 0;
|
||||
loop [C:= Encrypted(I);
|
||||
I:= I+1;
|
||||
if C = 0 then quit;
|
||||
IX:= IndexOf(Polybius, C);
|
||||
if IX >= 0 then \ignores spaces, etc.
|
||||
[Rows(J):= IX/5;
|
||||
Cols(J):= rem(0);
|
||||
J:= J+1;
|
||||
];
|
||||
];
|
||||
Len:= J; \length of encrypted message
|
||||
for I:= 0 to Len-1 do \append Cols onto end of Rows
|
||||
Rows(I+Len):= Cols(I);
|
||||
for I:= 0 to Len-1 do
|
||||
[IX:= Rows(I*2)*5 + Rows(I*2+1);
|
||||
Encrypted(I):= Polybius(IX);
|
||||
];
|
||||
Encrypted(I):= 0; \terminate string
|
||||
];
|
||||
|
||||
proc Decrypt(Polybius, Message); \Copy decrypted Message into Decrypted
|
||||
char Polybius, Message;
|
||||
char Rows(MaxLen*2), Cols(MaxLen);
|
||||
int C, IX, I, J, Len;
|
||||
[I:= 0; J:= 0;
|
||||
loop [C:= Message(I);
|
||||
if C = 0 then quit;
|
||||
I:= I+1;
|
||||
IX:= IndexOf(Polybius, C);
|
||||
Rows(J):= IX/5;
|
||||
Rows(J+1):= rem(0);
|
||||
J:= J+2;
|
||||
];
|
||||
Len:= I;
|
||||
Cols:= Rows + Len;
|
||||
for I:= 0 to Len-1 do
|
||||
[IX:= Rows(I)*5 + Cols(I);
|
||||
Decrypted(I):= Polybius(IX);
|
||||
];
|
||||
Decrypted(I):= 0;
|
||||
];
|
||||
|
||||
int Polys, Msgs, I;
|
||||
[Polys:= ["ABCDEFGHIKLMNOPQRSTUVWXYZ",
|
||||
"BGWKZQPNDSIOAXEFCLUMTHYVR",
|
||||
"BGWKZQPNDSIOAXEFCLUMTHYVR",
|
||||
"PLAYFIREXMBCDGHKNOQSTUVWZ"];
|
||||
Msgs:= ["ATTACKATDAWN",
|
||||
"FLEEATONCE",
|
||||
"ATTACKATDAWN",
|
||||
"The invasion will start on the first of January"];
|
||||
for I:= 0 to 4-1 do
|
||||
[Encrypt(Polys(I), Msgs(I));
|
||||
Decrypt(Polys(I), Encrypted);
|
||||
Text(0, "Message : "); Text(0, Msgs(I)); CrLf(0);
|
||||
Text(0, "Encrypted : "); Text(0, Encrypted); CrLf(0);
|
||||
Text(0, "Decrypted : "); Text(0, Decrypted); CrLf(0);
|
||||
if I < 4-1 then CrLf(0);
|
||||
];
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue