ADFGVX cipher: Difference between revisions
Content deleted Content added
Alextretyak (talk | contribs) |
→{{header|Uiua}}: Changed the algorithm |
||
(21 intermediate revisions by 8 users not shown) | |||
Line 1:
{{
;Description
Line 28:
{{trans|Nim}}
<
F encrypt(plainText, polybius, key)
Line 94:
V plainText = decrypt(cipherText, polybius, key)
print("\nDecrypted : "plainText)</
{{out}}
Line 116:
Decrypted : ATTACKAT1200AM
</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program adfgvx64.s */
/* remark 1 : At each launch, the random values are identical.
To change them, modify the value of the seed (graine) */
/* remark 2 : this program not run in android with termux
because the call system stats is not find */
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ SIZE, 6
.equ SIZEC, SIZE * SIZE
.equ KEYSIZE, 9
.equ FSTAT, 80
.equ O_RDWR, 0x0002 // open for reading and writing
/*******************************************/
/* Structures **/
/*******************************************/
/* structure de type stat 64 bits : infos fichier */
.struct 0
Stat_dev_t: // ID of device containing file
.struct Stat_dev_t + 8
Stat_ino_t: // inode
.struct Stat_ino_t + 4
Stat_mode_t: // File type and mode
.struct Stat_mode_t + 4
Stat_nlink_t: // Number of hard links
.struct Stat_nlink_t + 4
Stat_uid_t: // User ID of owner
.struct Stat_uid_t + 8
Stat_gid_t: // Group ID of owner
.struct Stat_gid_t + 8
Stat_rdev_t: // Device ID (if special file)
.struct Stat_rdev_t + 8
Stat_size_deb: // la taille est sur 8 octets si gros fichiers
.struct Stat_size_deb + 4
Stat_size_t: // Total size, in bytes
.struct Stat_size_t + 4
Stat_blksize_t: // Block size for filesystem I/O
.struct Stat_blksize_t + 4
Stat_blkcnt_t: // Number of 512B blocks allocated
.struct Stat_blkcnt_t + 4
Stat_atime: // date et heure fichier
.struct Stat_atime + 8
Stat_mtime: // date et heure modif fichier
.struct Stat_atime + 8
Stat_ctime: // date et heure creation fichier
.struct Stat_atime + 8
Stat_Fin:
/*********************************/
/* Initialized data */
/*********************************/
.data
szText: .asciz "ATTACKAT1200AM"
//szText: .asciz "ABCDEFGHIJ"
szMessOpen: .asciz "File open error.\n"
szMessStat: .asciz "File information error.\n"
szMessRead: .asciz "File read error.\n"
szMessClose: .asciz "File close error.\n"
szMessDecryptText: .asciz "Decrypted text :\n"
szMessCryptText: .asciz "Encrypted text :\n"
szMessErrorChar: .asciz "Character text not Ok!\n"
szFileName: .asciz "unixdict.txt"
szMessPolybius: .asciz "6 x 6 Polybius square:\n"
szTitle: .asciz " | A D F G V X\n---------------\n"
szLine1: .asciz "A | \n"
szLine2: .asciz "D | \n"
szLine3: .asciz "F | \n"
szLine4: .asciz "G | \n"
szLine5: .asciz "V | \n"
szLine6: .asciz "X | \n"
szListCharCode: .asciz "ADFGVX"
szListChar: .asciz "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
.equ LGLISTCHAR, . - szListChar - 1
szMessStart: .asciz "Program 64 bits start.\n"
szCarriageReturn: .asciz "\n"
.align 4
qGraine: .quad 1234567 // random init
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sKeyWord: .skip 16
sKeyWordSorted: .skip 16
tabPolybius: .skip SIZE * SIZE + 4
sBuffer: .skip 1000
sBuffex1: .skip 1000
sBuffex2: .skip 1000
tabPosit: .skip 16
tabPositInv: .skip 16
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessStart
bl affichageMess
bl createPolybius // create 6*6 polybius
ldr x0,qAdrsKeyWord
bl generateKey // generate key
cmp x0,#-1 // file error ?
beq 100f
bl affichageMess // display key
ldr x0,qAdrszCarriageReturn
bl affichageMess
ldr x0,qAdrszMessCryptText
bl affichageMess
ldr x0,qAdrszText // text encrypt
ldr x1,qAdrtabPolybius
ldr x2,qAdrsKeyWord
ldr x3,qAdrsBuffer // result buffer
bl encryption
cmp x0,#-1 // error if unknow character in text
beq 100f
bl affichageMess // display text encrypted
ldr x0,qAdrszCarriageReturn
bl affichageMess
ldr x0,qAdrszCarriageReturn
bl affichageMess
ldr x0,qAdrszMessDecryptText
bl affichageMess
ldr x0,qAdrsBuffer // text decrypt
ldr x1,qAdrtabPolybius
ldr x2,qAdrsKeyWord
ldr x3,qAdrsBuffex1 // result buffer
bl decryption
bl affichageMess
ldr x0,qAdrszCarriageReturn
bl affichageMess
100: // standard end of the program
mov x0, #0 // return code
mov x8, #EXIT // request to exit program
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrszMessDecryptText: .quad szMessDecryptText
qAdrszMessCryptText: .quad szMessCryptText
qAdrszMessStart: .quad szMessStart
qAdrsKeyWord: .quad sKeyWord
qAdrszText: .quad szText
/***************************************************/
/* create 6 * 6 polybius */
/***************************************************/
createPolybius:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]!
stp x4,x5,[sp,-16]!
ldr x0,qAdrszListChar // character list address
mov x1,#LGLISTCHAR // character list size
ldr x2,qAdrtabPolybius
bl shufflestrings // shuffle list
ldr x0,qAdrszMessPolybius
bl affichageMess
ldr x0,qAdrszTitle // display polybius lines
bl affichageMess
ldr x0,qAdrszLine1
mov x3,#0
mov x4,#4
1:
ldrb w1,[x2,x3]
strb w1,[x0,x4]
add x4,x4,#2
add x3,x3,#1
cmp x3,#SIZE
blt 1b
bl affichageMess
ldr x0,qAdrszLine2
mov x3,#SIZE
mov x4,#4
2:
ldrb w1,[x2,x3]
strb w1,[x0,x4]
add x4,x4,#2
add x3,x3,#1
cmp x3,#SIZE * 2
blt 2b
bl affichageMess
ldr x0,qAdrszLine3
mov x3,#SIZE * 2
mov x4,#4
3:
ldrb w1,[x2,x3]
strb w1,[x0,x4]
add x4,x4,#2
add x3,x3,#1
cmp x3,#SIZE * 3
blt 3b
bl affichageMess
ldr x0,qAdrszLine4
mov x3,#SIZE * 3
mov x4,#4
4:
ldrb w1,[x2,x3]
strb w1,[x0,x4]
add x4,x4,#2
add x3,x3,#1
cmp x3,#SIZE * 4
blt 4b
bl affichageMess
ldr x0,qAdrszLine5
mov x3,#SIZE * 4
mov x4,#4
5:
ldrb w1,[x2,x3]
strb w1,[x0,x4]
add x4,x4,#2
add x3,x3,#1
cmp x3,#SIZE * 5
blt 5b
bl affichageMess
ldr x0,qAdrszLine6
mov x3,#SIZE * 5
mov x4,#4
6:
ldrb w1,[x2,x3]
strb w1,[x0,x4]
add x4,x4,#2
add x3,x3,#1
cmp x3,#SIZE * 6
blt 6b
bl affichageMess
100:
ldp x4,x5,[sp],16
ldp x2,x3,[sp],16
ldp x1,lr,[sp],16
ret
qAdrszListChar: .quad szListChar
qAdrtabPolybius: .quad tabPolybius
qAdrszMessPolybius: .quad szMessPolybius
qAdrszTitle: .quad szTitle
qAdrszLine1: .quad szLine1
qAdrszLine2: .quad szLine2
qAdrszLine3: .quad szLine3
qAdrszLine4: .quad szLine4
qAdrszLine5: .quad szLine5
qAdrszLine6: .quad szLine6
/***************************************************/
/* generate key word */
/***************************************************/
/* x0 key word address */
generateKey:
stp x1,lr,[sp,-16]!
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]!
stp x12,x13,[sp,-16]!
mov x9,x0
mov x0,AT_FDCWD
ldr x1,qAdrszFileName // file name
mov x2,#O_RDWR // flags
mov x3,#0 // mode
mov x8,#OPEN // file open
svc 0
cmp x0,#0 // error ?
ble 99f
mov x11,x0 // FD save
ldr x1,qAdrsBuffer // buffer address
mov x8, #FSTAT // call systeme NEWFSTAT
svc 0
cmp x0,#0
blt 98f
// load file size
ldr x1,qAdrsBuffer // buffer address
ldr w6,[x1,#Stat_size_t] // file size
//ldr w6,[x1,mbox_data_size]
lsr x12,x6,#5 // align size to multiple 16 for stack alignement
lsl x12,x12,#5
add x12,x12,#32 // add for great buffer
sub sp,sp,x12 // reserve buffer on stack
mov fp,sp // address save
mov x0,x11
mov x1,fp
mov x2,x12
mov x8,#READ // call system read file
svc 0
cmp x0,#0 // error read ?
blt 97f
mov x0,x11
mov x8,#CLOSE // call system close file
svc 0
cmp x0,#0 // error close ?
blt 96f
sub sp,sp,#0x1000 // create array word address on stack
mov x10,sp // save address array
mov x1,#0
mov x2,fp
mov x5,#0 // index word ok
mov x3,#0 // word length
1:
ldrb w4,[fp,x1] // load character
cmp w4,#0x0D // end word ?
beq 2f // yes
add x1,x1,#1
add x3,x3,#1
b 1b
2:
cmp x3,#KEYSIZE // word length = key length ?
bne 3f // no ?
mov x0,x2
bl wordControl // contril if all letters are différent ?
cmp x0,#1
bne 3f
str x2,[x10,x5,lsl #3] // if ok store word address in array on stack
add x5,x5,#1 // increment word counter
3:
add x1,x1,#2
cmp x1,x6 // end ?
beq 4f
add x2,fp,x1 // new word begin
mov x3,#0 // init word length
b 1b // and loop
4:
mov x0,x5 // number random to total words
bl genereraleas
ldr x2,[x10,x0,lsl #3] // load address word
mov x1,#0
5: // copy random word in word result
ldrb w3,[x2,x1]
strb w3,[x9,x1]
add x1,x1,#1
cmp x1,#KEYSIZE
blt 5b
strb wzr,[x9,x1] // zero final
mov x0,x9
b 100f
// display errors
96:
ldr x0,qAdrszMessClose
bl affichageMess
mov x0,#-1 // error
b 100f
97:
ldr x0,qAdrszMessRead
bl affichageMess
mov x0,#-1 // error
b 100f
98:
ldr x0,qAdrszMessStat
bl affichageMess
mov x0,#-1 // error
b 101f
99:
ldr x0,qAdrszMessOpen
bl affichageMess
mov x0,#-1 // error
b 101f
100:
add sp,sp,x12
add sp,sp,#0x1000
101:
ldp x12,x13,[sp],16
ldp x10,x11,[sp],16
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
qAdrszFileName: .quad szFileName
qAdrszMessOpen: .quad szMessOpen
qAdrszMessRead: .quad szMessRead
qAdrszMessStat: .quad szMessStat
qAdrszMessClose: .quad szMessClose
qAdrsBuffer: .quad sBuffer
/******************************************************************/
/* control if letters are diferents */
/******************************************************************/
/* x0 contains the address of the string */
/* x0 return 1 if Ok else return 0 */
wordControl:
stp x1,lr,[sp,-16]!
stp x2,x3,[sp,-16]!
stp x4,x5,[sp,-16]!
mov x1,#0 // init index 1
1:
ldrb w3,[x0,x1] // load one character
cmp x3,#0x0D // end word ?
mov x5,#1
csel x0,x5,x0,eq // yes is ok
//moveq x0,#1 // yes is ok
beq 100f // -> end
add x2,x1,#1 // init index two
2:
ldrb w4,[x0,x2] // load one character
cmp w4,#0x0D // end word ?
add x5,x1,1
csel x1,x5,x1,eq // yes increment index 1
beq 1b // and loop1
cmp x3,x4 // caracters equals ?
csel x0,xzr,x0,eq // yes is not good
beq 100f // and end
add x2,x2,#1 // else increment index 2
b 2b // and loop 2
100:
ldp x4,x5,[sp],16
ldp x2,x3,[sp],16
ldp x1,lr,[sp],16
ret
/******************************************************************/
/* key sort by insertion sort */
/******************************************************************/
/* x0 contains the address of String */
/* x1 contains the first element */
/* x2 contains the number of element */
/* x3 contains result address */
keySort:
stp x1,lr,[sp,-16]!
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]!
ldr x7,qAdrtabPosit
mov x10,x3
mov x3,#0
0: // init position array and copy key
strb w3,[x7,x3] // in result array
ldrb w4,[x0,x3]
strb w4,[x10,x3]
add x3,x3,#1
cmp x3,#KEYSIZE
blt 0b
add x3,x1,#1 // start index i
1: // start loop
ldrb w4,[x10,x3] // load value A[i]
ldrb w8,[x7,x3] // load position
sub x5,x3,#1 // index j
2:
ldrb w6,[x10,x5] // load value A[j]
ldrb w9,[x7,x5] // load position
cmp x6,x4 // compare value
ble 3f
add x5,x5,#1 // increment index j
strb w6,[x10,x5] // store value A[j+1]
strb w9,[x7,x5] // store position
subs x5,x5,#2 // j = j - 1
bge 2b // loop if j >= 0
3:
add x5,x5,#1 // increment index j
strb w4,[x10,x5] // store value A[i] in A[j+1]
strb w8,[x7,x5]
add x3,x3,#1 // increment index i
cmp x3,x2 // end ?
blt 1b // no -> loop
ldr x1,qAdrtabPositInv // inverse position
mov x2,#0 // index
4:
ldrb w3,[x7,x2] // load position index
strb w2,[x1,x3] // store index in position
add x2,x2,#1 // increment index
cmp x2,#KEYSIZE // end ?
blt 4b
mov x0,x10
100:
ldp x10,x11,[sp],16
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 // TODO: retaur à completer
ret
qAdrtabPosit: .quad tabPosit
qAdrtabPositInv: .quad tabPositInv
/******************************************************************/
/* text encryption */
/******************************************************************/
/* x0 contains the address of text */
/* x1 contains polybius address
/* x2 contains the key address */
/* x3 contains result buffer address */
encryption:
stp x1,lr,[sp,-16]!
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 x9,x0 // save text address
mov x8,x3
mov x10,x1 // save address polybius
mov x0,x2 // key address
mov x1,#0 // first character
mov x2,#KEYSIZE // key length
ldr x3,qAdrsKeyWordSorted // result address
bl keySort // sort leters of key
// bl affichageMess // if you want display sorted key
// ldr x0,qAdrszCarriageReturn
// bl affichageMess
ldr x3,qAdrsBuffex1
mov x5,#0 // init text index
mov x4,#0 // init result index
1:
ldrb w0,[x9,x5] // load a byte to text
cmp x0,#0 // end ?
beq 4f
mov x6,#0 // init index polybius
2:
ldrb w7,[x10,x6] // load character polybius
cmp x7,x0 // equal ?
beq 3f
add x6,x6,#1 // increment index
cmp x6,#SIZEC // not find -> error
bge 99f
b 2b // and loop
3:
mov x0,x6
bl convPosCode // convert position in code character
strb w0,[x3,x4] // line code character
add x4,x4,#1
strb w1,[x3,x4] // column code character
add x4,x4,#1
add x5,x5,#1 // increment text index
b 1b
4:
mov x0,#0 // zero final -> text result
strb w0,[x3,x4]
mov x5,x3
mov x1,#0 // index position column
mov x7,#0 // index text
ldr x2,qAdrtabPositInv
5:
ldrb w0,[x2,x1] // load position text
7: // loop to characters transposition
ldrb w6,[x5,x0] // load character
strb w6,[x8,x7] // store position final
add x7,x7,#1 // increment final index
add x0,x0,#KEYSIZE // add size key
cmp x0,x4 // end ?
blt 7b
add x1,x1,#1 // add index column
cmp x1,#KEYSIZE // < key size
blt 5b // yes -> loop
mov x6,#0 // zero final
strb w6,[x8,x7]
mov x0,x8 // return address encrypted text
b 100f
99: // display error
ldr x0,qAdrszMessErrorChar
bl affichageMess
mov x0,#-1
100:
ldp x10,x11,[sp],16
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
qAdrsBuffex1: .quad sBuffex1
qAdrsKeyWordSorted: .quad sKeyWordSorted
qAdrszMessErrorChar: .quad szMessErrorChar
/******************************************************************/
/* text decryption */
/******************************************************************/
/* x0 contains the address of text */
/* x1 contains polybius address
/* x2 contains the key */
/* x3 contains result buffer */
/* x0 return decoded text */
decryption:
stp x1,lr,[sp,-16]!
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]!
stp x12,x13,[sp,-16]!
mov x4,#0
1: // compute text length
ldrb w5,[x0,x4]
cmp x5,#0
add x11,x4,1
csel x4,x11,x4,ne
bne 1b
mov x12,x0
mov x11,x1
mov x10,x2
mov x13,x3
// compute line number and remainder
mov x1,#KEYSIZE // compute line number and remainder
udiv x8,x4,x1 // line number
msub x7,x8,x1,x4 // remainder characters last line
mov x0,x10 // key address
mov x1,#0 // first character
mov x2,#KEYSIZE // size
ldr x3,qAdrsKeyWordSorted // result address
bl keySort // sort key
ldr x10,qAdrtabPositInv // inverse position
mov x2,#0 // index colonne tabposit
mov x5,#0 // text index
mov x0,#0 // index line store text
mov x1,#0 // counter line
ldr x9,qAdrsBuffex2
1:
ldrb w3,[x10,x2] // load position
ldrb w6,[x12,x5] // load text character
add x3,x3,x0 // compute position with index line
strb w6,[x9,x3] // store character in good position
add x5,x5,#1 // increment index text
cmp x5,x4 // end ?
bge 4f
add x1,x1,#1 // increment line
cmp x1,x8 // line < line size
blt 2f
bgt 11f // line = line size
sub x3,x3,x0 // restaure position column
cmp x3,x7 // position < remainder so add character other line
blt 2f
11:
mov x1,#0 // init ligne
mov x0,#0 // init line shift
add x2,x2,#1 // increment index array position inverse
cmp x2,#KEYSIZE // end ?
csel x2,xzr,x2,ge // init index
b 3f
2:
add x0,x0,#KEYSIZE
3:
b 1b
4: // convertir characters with polybius
mov x3,#0
mov x5,#0
5:
mov x0,x11
ldrb w1,[x9,x3] // load a first character
add x3,x3,#1
ldrb w2,[x9,x3] // load a 2ieme character
bl decodPosCode // decode
strb w0,[x13,x5] // store result in final result
add x5,x5,#1 // increment final result index
add x3,x3,#1 // increment index text
cmp x3,x4 // end ?
blt 5b
mov x0,#0 // final zero
strb w0,[x13,x5]
mov x0,x13 // return final result address
100:
ldp x12,x13,[sp],16
ldp x10,x11,[sp],16
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 // TODO: retaur à completer
ret
qAdrsBuffex2: .quad sBuffex2
/******************************************************************/
/* convertir position en code */
/******************************************************************/
/* x0 contains the position in polybius */
/* x0 return code1 */
/* x1 return code2 */
convPosCode:
stp x2,lr,[sp,-16]!
stp x3,x4,[sp,-16]!
ldr x4,qAdrszListCharCode
mov x1,#SIZE
udiv x2,x0,x1
msub x3,x2,x1,x0
//bl division
ldrb w0,[x4,x2]
ldrb w1,[x4,x3]
100:
ldp x3,x4,[sp],16
ldp x2,lr,[sp],16
ret
qAdrszListCharCode: .quad szListCharCode
/******************************************************************/
/* convertir code en character */
/******************************************************************/
/* x0 polybius address */
/* x1 code 1 */
/* x2 code 2 */
/* x0 return character */
decodPosCode:
stp x1,lr,[sp,-16]!
stp x2,x3,[sp,-16]!
stp x4,x5,[sp,-16]!
ldr x4,qAdrszListCharCode
mov x3,#0
1:
ldrb w5,[x4,x3]
cmp x5,#0
beq 2f
cmp x5,x1
csel x1,x3,x1,eq
cmp x5,x2
csel x2,x3,x2,eq
add x3,x3,#1
b 1b
2:
mov x5,#SIZE
mul x1,x5,x1
add x1,x1,x2
ldrb w0,[x0,x1]
100:
ldp x4,x5,[sp],16
ldp x2,x3,[sp],16
ldp x1,lr,[sp],16
ret
/******************************************************************/
/* shuffle strings algorithme Fisher-Yates */
/******************************************************************/
/* x0 contains the address of the string */
/* x1 contains string length */
/* x2 contains address result string */
shufflestrings:
stp x1,lr,[sp,-16]! // TODO: save à completer
stp x2,x3,[sp,-16]!
stp x4,x5,[sp,-16]!
mov x3,#0
1: // loop copy string in result
ldrb w4,[x0,x3]
strb w4,[x2,x3]
add x3,x3,#1
cmp x3,x1
ble 1b
sub x1,x1,#1 // last element
2:
mov x0,x1
bl genereraleas // call random
ldrb w4,[x2,x1] // load byte string index loop
ldrb w3,[x2,x0] // load byte string random index
strb w3,[x2,x1] // and exchange
strb w4,[x2,x0]
subs x1,x1,#1
cmp x1,#1
bge 2b
100:
ldp x4,x5,[sp],16
ldp x2,x3,[sp],16
ldp x1,lr,[sp],16
ret
/***************************************************/
/* Generation random number */
/***************************************************/
/* x0 contains limit */
genereraleas:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
ldr x1,qAdrqGraine
ldr x2,[x1]
ldr x3,qNbDep1
mul x2,x3,x2
ldr x3,qNbDep2
add x2,x2,x3
str x2,[x1] // maj de la graine pour l appel suivant
cmp x0,#0
beq 100f
udiv x3,x2,x0
msub x0,x3,x0,x2 // résult = remainder
100: // end function
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
qAdrqGraine: .quad qGraine
qNbDep1: .quad 0x0019660d
qNbDep2: .quad 0x3c6ef35f
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../includeARM64.inc"
</syntaxhighlight>
{{Out}}
<pre>
Program 64 bits start.
6 x 6 Polybius square:
| A D F G V X
---------------
A | Q A M C 0 X
D | 1 S O Y R E
F | H 7 J T U W
G | V F 3 Z 4 I
V | 8 B 9 K 6 2
X | P D L G 5 N
hailstorm
Encrypted text :
DVXAGVFFGAGAVDDDAAAGFVAGAFDA
Decrypted text :
ATTACKAT1200AM
</pre>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> }}
<syntaxhighlight lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program adfgvx.s */
/* remark 1 : At each launch, the random values are identical.
To change them, modify the value of the seed (graine) */
/* remark 2 : this program not run in android with termux
because the call system stats is not find */
/************************************/
/* Constantes */
/************************************/
/* for constantes see task include a file in arm assembly */
.include "../constantes.inc"
.equ SIZE, 6
.equ SIZEC, SIZE * SIZE
.equ KEYSIZE, 9
.equ READ, 3
.equ WRITE, 4
.equ OPEN, 5
.equ CLOSE, 6
.equ FSTAT, 0x6C
.equ O_RDWR, 0x0002 @ open for reading and writing
/**********************************************/
/* structure de type stat : infos fichier */
/**********************************************/
.struct 0
Stat_dev_t: @ ID of device containing file
.struct Stat_dev_t + 4
Stat_ino_t: @ inode
.struct Stat_ino_t + 2
Stat_mode_t: @ File type and mode
.struct Stat_mode_t + 2
Stat_nlink_t: @ Number of hard links
.struct Stat_nlink_t + 2
Stat_uid_t: @ User ID of owner
.struct Stat_uid_t + 2
Stat_gid_t: @ Group ID of owner
.struct Stat_gid_t + 2
Stat_rdev_t: @ Device ID (if special file)
.struct Stat_rdev_t + 2
Stat_size_deb: @ la taille est sur 8 octets si gros fichiers
.struct Stat_size_deb + 4
Stat_size_t: @ Total size, in bytes
.struct Stat_size_t + 4
Stat_blksize_t: @ Block size for filesystem I/O
.struct Stat_blksize_t + 4
Stat_blkcnt_t: @ Number of 512B blocks allocated
.struct Stat_blkcnt_t + 4
Stat_atime: @ date et heure fichier
.struct Stat_atime + 8
Stat_mtime: @ date et heure modif fichier
.struct Stat_atime + 8
Stat_ctime: @ date et heure creation fichier
.struct Stat_atime + 8
Stat_Fin:
/*********************************/
/* Initialized data */
/*********************************/
.data
szText: .asciz "ATTACKAT1200AM"
//szText: .asciz "ABCDEFGHIJ"
szMessOpen: .asciz "File open error.\n"
szMessStat: .asciz "File information error.\n"
szMessRead: .asciz "File read error.\n"
szMessClose: .asciz "File close error.\n"
szMessDecryptText: .asciz "Decrypted text :\n"
szMessCryptText: .asciz "Encrypted text :\n"
szMessErrorChar: .asciz "Character text not Ok!\n"
szFileName: .asciz "unixdict.txt"
szMessPolybius: .asciz "6 x 6 Polybius square:\n"
szTitle: .asciz " | A D F G V X\n---------------\n"
szLine1: .asciz "A | \n"
szLine2: .asciz "D | \n"
szLine3: .asciz "F | \n"
szLine4: .asciz "G | \n"
szLine5: .asciz "V | \n"
szLine6: .asciz "X | \n"
szListCharCode: .asciz "ADFGVX"
szListChar: .asciz "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
.equ LGLISTCHAR, . - szListChar - 1
szMessStart: .asciz "Program 32 bits start.\n"
szCarriageReturn: .asciz "\n"
.align 4
iGraine: .int 1234567 // random init
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sKeyWord: .skip 16
sKeyWordSorted: .skip 16
tabPolybius: .skip SIZE * SIZE + 4
sBuffer: .skip 1000
sBuffer1: .skip 1000
sBuffer2: .skip 1000
tabPosit: .skip 16
tabPositInv: .skip 16
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
ldr r0,iAdrszMessStart
bl affichageMess
bl createPolybius @ create 6*6 polybius
ldr r0,iAdrsKeyWord
bl generateKey @ generate key
cmp r0,#-1 @ file error ?
beq 100f
bl affichageMess @ display key
ldr r0,iAdrszCarriageReturn
bl affichageMess
ldr r0,iAdrszMessCryptText
bl affichageMess
ldr r0,iAdrszText @ text encrypt
ldr r1,iAdrtabPolybius
ldr r2,iAdrsKeyWord
ldr r3,iAdrsBuffer @ result buffer
bl encryption
cmp r0,#-1 @ error if unknow character in text
beq 100f
bl affichageMess @ display text encrypted
ldr r0,iAdrszCarriageReturn
bl affichageMess
ldr r0,iAdrszCarriageReturn
bl affichageMess
ldr r0,iAdrszMessDecryptText
bl affichageMess
ldr r0,iAdrsBuffer @ text decrypt
ldr r1,iAdrtabPolybius
ldr r2,iAdrsKeyWord
ldr r3,iAdrsBuffer1 @ result buffer
bl decryption
bl affichageMess
ldr r0,iAdrszCarriageReturn
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessDecryptText: .int szMessDecryptText
iAdrszMessCryptText: .int szMessCryptText
iAdrszMessStart: .int szMessStart
iAdrsKeyWord: .int sKeyWord
iAdrszText: .int szText
/***************************************************/
/* create 6 * 6 polybius */
/***************************************************/
createPolybius:
push {r1-r4,lr} @ save des registres
ldr r0,iAdrszListChar @ character list address
mov r1,#LGLISTCHAR @ character list size
ldr r2,iAdrtabPolybius
bl shufflestrings @ shuffle list
ldr r0,iAdrszMessPolybius
bl affichageMess
ldr r0,iAdrszTitle @ display polybius lines
bl affichageMess
ldr r0,iAdrszLine1
mov r3,#0
mov r4,#4
1:
ldrb r1,[r2,r3]
strb r1,[r0,r4]
add r4,r4,#2
add r3,r3,#1
cmp r3,#SIZE
blt 1b
bl affichageMess
ldr r0,iAdrszLine2
mov r3,#SIZE
mov r4,#4
2:
ldrb r1,[r2,r3]
strb r1,[r0,r4]
add r4,r4,#2
add r3,r3,#1
cmp r3,#SIZE * 2
blt 2b
bl affichageMess
ldr r0,iAdrszLine3
mov r3,#SIZE * 2
mov r4,#4
3:
ldrb r1,[r2,r3]
strb r1,[r0,r4]
add r4,r4,#2
add r3,r3,#1
cmp r3,#SIZE * 3
blt 3b
bl affichageMess
ldr r0,iAdrszLine4
mov r3,#SIZE * 3
mov r4,#4
4:
ldrb r1,[r2,r3]
strb r1,[r0,r4]
add r4,r4,#2
add r3,r3,#1
cmp r3,#SIZE * 4
blt 4b
bl affichageMess
ldr r0,iAdrszLine5
mov r3,#SIZE * 4
mov r4,#4
5:
ldrb r1,[r2,r3]
strb r1,[r0,r4]
add r4,r4,#2
add r3,r3,#1
cmp r3,#SIZE * 5
blt 5b
bl affichageMess
ldr r0,iAdrszLine6
mov r3,#SIZE * 5
mov r4,#4
6:
ldrb r1,[r2,r3]
strb r1,[r0,r4]
add r4,r4,#2
add r3,r3,#1
cmp r3,#SIZE * 6
blt 6b
bl affichageMess
100:
pop {r1-r4,pc}
iAdrszListChar: .int szListChar
iAdrtabPolybius: .int tabPolybius
iAdrszMessPolybius: .int szMessPolybius
iAdrszTitle: .int szTitle
iAdrszLine1: .int szLine1
iAdrszLine2: .int szLine2
iAdrszLine3: .int szLine3
iAdrszLine4: .int szLine4
iAdrszLine5: .int szLine5
iAdrszLine6: .int szLine6
/***************************************************/
/* generate key word */
/***************************************************/
/* r0 key word address */
generateKey:
push {r1-r12,lr} @ save registers
mov r9,r0
ldr r0,iAdrszFileName @ file name
mov r1,#O_RDWR @ flags
mov r2,#0 @ mode
mov r7,#OPEN @ file open
svc 0
cmp r0,#0 @ error ?
ble 99f
mov r8,r0 @ FD save
ldr r1,iAdrsBuffer @ buffer address
mov r7, #FSTAT @ call systeme NEWFSTAT
svc 0
cmp r0,#0
blt 98f
@ load file size
ldr r1,iAdrsBuffer @ buffer address
ldr r6,[r1,#Stat_size_t] @ file size
lsr r12,r6,#3 @ align size to multiple 4
lsl r12,#3
add r12,#8 @ add for great buffer
sub sp,sp,r12 @ reserve buffer on stack
mov fp,sp @ address save
mov r0,r8
mov r1,fp
mov r2,r12
mov r7,#READ @ call system read file
svc 0
cmp r0,#0 @ error read ?
blt 97f
mov r0,r8
mov r7,#CLOSE @ call system close file
svc 0
cmp r0,#0 @ error close ?
blt 96f
sub sp,sp,#0x1000 @ create array word address on stack
mov r10,sp @ save address array
mov r1,#0
mov r2,fp
mov r5,#0 @ index word ok
mov r3,#0 @ word length
1:
ldrb r4,[fp,r1] @ load character
cmp r4,#0x0D @ end word ?
beq 2f @ yes
add r1,r1,#1
add r3,r3,#1
b 1b
2:
cmp r3,#KEYSIZE @ word length = key length ?
bne 3f @ no ?
mov r0,r2
bl wordControl @ contril if all letters are différent ?
cmp r0,#1
streq r2,[r10,r5,lsl #2] @ if ok store word address in array on stack
addeq r5,r5,#1 @ increment word counter
3:
add r1,r1,#2
cmp r1,r6 @ end ?
beq 4f
add r2,fp,r1 @ new word begin
mov r3,#0 @ init word length
b 1b @ and loop
4:
mov r0,r5 @ number random to total words
bl genereraleas
ldr r2,[r10,r0,lsl #2] @ load address word
mov r1,#0
5: @ copy random word in word result
ldrb r3,[r2,r1]
strb r3,[r9,r1]
add r1,r1,#1
cmp r1,#KEYSIZE
blt 5b
mov r3,#0 @ zero final
strb r3,[r9,r1]
mov r0,r9
b 100f
@ display errors
96:
ldr r0,iAdrszMessClose
bl affichageMess
mov r0,#-1 @ error
b 100f
97:
ldr r0,iAdrszMessRead
bl affichageMess
mov r0,#-1 @ error
b 100f
98:
ldr r0,iAdrszMessStat
bl affichageMess
mov r0,#-1 @ error
b 101f
99:
ldr r0,iAdrszMessOpen
bl affichageMess
mov r0,#-1 @ error
b 101f
100:
add sp,sp,r12
add sp,sp,#0x1000
101:
pop {r1-r12,pc}
iAdrszFileName: .int szFileName
iAdrszMessOpen: .int szMessOpen
iAdrszMessRead: .int szMessRead
iAdrszMessStat: .int szMessStat
iAdrszMessClose: .int szMessClose
iAdrsBuffer: .int sBuffer
/******************************************************************/
/* control if letters are diferents */
/******************************************************************/
/* r0 contains the address of the string */
/* r0 return 1 if Ok else return 0 */
wordControl:
push {r1-r4,lr} @ save registers
mov r1,#0 @ init index 1
1:
ldrb r3,[r0,r1] @ load one character
cmp r3,#0x0D @ end word ?
moveq r0,#1 @ yes is ok
beq 100f @ -> end
add r2,r1,#1 @ init index two
2:
ldrb r4,[r0,r2] @ load one character
cmp r4,#0x0D @ end word ?
addeq r1,r1,#1 @ yes increment index 1
beq 1b @ and loop1
cmp r3,r4 @ caracters equals ?
moveq r0,#0 @ yes is not good
beq 100f @ and end
add r2,r2,#1 @ else increment index 2
b 2b @ and loop 2
100:
pop {r1-r4,pc}
/******************************************************************/
/* key sort by insertion sort */
/******************************************************************/
/* r0 contains the address of String */
/* r1 contains the first element */
/* r2 contains the number of element */
/* r3 contains result address */
keySort:
push {r2-r10,lr} @ save registers
ldr r7,iAdrtabPosit
mov r10,r3
mov r3,#0
0: @ init position array and copy key
strb r3,[r7,r3] @ in result array
ldrb r4,[r0,r3]
strb r4,[r10,r3]
add r3,r3,#1
cmp r3,#KEYSIZE
blt 0b
add r3,r1,#1 @ start index i
1: @ start loop
ldrb r4,[r10,r3] @ load value A[i]
ldrb r8,[r7,r3] @ load position
sub r5,r3,#1 @ index j
2:
ldrb r6,[r10,r5] @ load value A[j]
ldrb r9,[r7,r5] @ load position
cmp r6,r4 @ compare value
ble 3f
add r5,#1 @ increment index j
strb r6,[r10,r5] @ store value A[j+1]
strb r9,[r7,r5] @ store position
subs r5,#2 @ j = j - 1
bge 2b @ loop if j >= 0
3:
add r5,#1 @ increment index j
strb r4,[r10,r5] @ store value A[i] in A[j+1]
strb r8,[r7,r5]
add r3,#1 @ increment index i
cmp r3,r2 @ end ?
blt 1b @ no -> loop
ldr r1,iAdrtabPositInv @ inverse position
mov r2,#0 @ index
4:
ldrb r3,[r7,r2] @ load position index
strb r2,[r1,r3] @ store index in position
add r2,r2,#1 @ increment index
cmp r2,#KEYSIZE @ end ?
blt 4b
mov r0,r10
100:
pop {r2-r10,pc}
iAdrtabPosit: .int tabPosit
iAdrtabPositInv: .int tabPositInv
/******************************************************************/
/* text encryption */
/******************************************************************/
/* r0 contains the address of text */
/* r1 contains polybius address
/* r2 contains the key address */
/* r3 contains result buffer address */
encryption:
push {r2-r10,lr} @ save registers
mov r9,r0 @ save text address
mov r8,r3
mov r10,r1 @ save address polybius
mov r0,r2 @ key address
mov r1,#0 @ first character
mov r2,#KEYSIZE @ key length
ldr r3,iAdrsKeyWordSorted @ result address
bl keySort @ sort leters of key
//bl affichageMess @ if you want display sorted key
//ldr r0,iAdrszCarriageReturn
//bl affichageMess
ldr r3,iAdrsBuffer1
mov r5,#0 @ init text index
mov r4,#0 @ init result index
1:
ldrb r0,[r9,r5] @ load a byte to text
cmp r0,#0 @ end ?
beq 4f
mov r6,#0 @ init index polybius
2:
ldrb r7,[r10,r6] @ load character polybius
cmp r7,r0 @ equal ?
beq 3f
add r6,r6,#1 @ increment index
cmp r6,#SIZEC @ not find -> error
bge 99f
b 2b @ and loop
3:
mov r0,r6
bl convPosCode @ convert position in code character
strb r0,[r3,r4] @ line code character
add r4,r4,#1
strb r1,[r3,r4] @ column code character
add r4,r4,#1
add r5,r5,#1 @ increment text index
b 1b
4:
mov r0,#0 @ zero final -> text result
strb r0,[r3,r4]
mov r5,r3
mov r1,#0 @ index position column
mov r7,#0 @ index text
ldr r2,iAdrtabPositInv
5:
ldrb r0,[r2,r1] @ load position text
7: @ loop to characters transposition
ldrb r6,[r5,r0] @ load character
strb r6,[r8,r7] @ store position final
add r7,r7,#1 @ increment final index
add r0,r0,#KEYSIZE @ add size key
cmp r0,r4 @ end ?
blt 7b
add r1,r1,#1 @ add index column
cmp r1,#KEYSIZE @ < key size
blt 5b @ yes -> loop
mov r6,#0 @ zero final
strb r6,[r8,r7]
mov r0,r8 @ return address encrypted text
b 100f
99: @ display error
ldr r0,iAdrszMessErrorChar
bl affichageMess
mov r0,#-1
100:
pop {r2-r10,pc}
iAdrsBuffer1: .int sBuffer1
iAdrsKeyWordSorted: .int sKeyWordSorted
iAdrszMessErrorChar: .int szMessErrorChar
/******************************************************************/
/* text decryption */
/******************************************************************/
/* r0 contains the address of text */
/* r1 contains polybius address
/* r2 contains the key */
/* r3 contains result buffer */
/* r0 return decoded text */
decryption:
push {r1-r12,lr} @ save registers
mov r4,#0
1: @ compute text length
ldrb r5,[r0,r4]
cmp r5,#0
addne r4,r4,#1
bne 1b
mov r12,r0
mov r11,r1
mov r10,r2
mov r9,r3
mov r0,r4 @ compute line number and remainder
mov r1,#KEYSIZE
bl division
mov r8,r2 @ line number
mov r7,r3 @ remainder characters last line
mov r0,r10 @ key address
mov r1,#0 @ first character
mov r2,#KEYSIZE @ size
ldr r3,iAdrsKeyWordSorted @ result address
bl keySort @ sort key
ldr r10,iAdrtabPositInv @ inverse position
mov r2,#0 @ index colonne tabposit
mov r5,#0 @ text index
mov r0,#0 @ index line store text
mov r1,#0 @ counter line
push {r9} @ save final result address
ldr r9,iAdrsBuffer2
1:
ldrb r3,[r10,r2] @ load position
ldrb r6,[r12,r5] @ load text character
add r3,r3,r0 @ compute position with index line
strb r6,[r9,r3] @ store character in good position
add r5,r5,#1 @ increment index text
cmp r5,r4 @ end ?
bge 4f
add r1,r1,#1 @ increment line
cmp r1,r8 @ line < line size
blt 2f
bgt 11f @ line = line size
sub r3,r3,r0 @ restaure position column
cmp r3,r7 @ position < remainder so add character other line
blt 2f
11:
mov r1,#0 @ init ligne
mov r0,#0 @ init line shift
add r2,r2,#1 @ increment index array position inverse
cmp r2,#KEYSIZE @ end ?
movge r2,#0 @ init index
b 3f
2:
add r0,#KEYSIZE
3:
b 1b
4: @ convertir characters with polybius
mov r3,#0
mov r5,#0
pop {r6} @ restaur final address result
5:
mov r0,r11
ldrb r1,[r9,r3] @ load a first character
add r3,r3,#1
ldrb r2,[r9,r3] @ load a 2ieme character
bl decodPosCode @ decode
strb r0,[r6,r5] @ store result in final result
add r5,r5,#1 @ increment final result index
add r3,r3,#1 @ increment index text
cmp r3,r4 @ end ?
blt 5b
mov r0,#0 @ final zero
strb r0,[r6,r5]
mov r0,r6 @ return final result address
100:
pop {r1-r12,pc}
iAdrsBuffer2: .int sBuffer2
/******************************************************************/
/* convertir position en code */
/******************************************************************/
/* r0 contains the position in polybius */
/* r0 return code1 */
/* r1 return code2 */
convPosCode:
push {r2-r4,lr} @ save registers
ldr r4,iAdrszListCharCode
mov r1,#SIZE
bl division
ldrb r0,[r4,r2]
ldrb r1,[r4,r3]
100:
pop {r2-r4,pc}
iAdrszListCharCode: .int szListCharCode
/******************************************************************/
/* convertir code en character */
/******************************************************************/
/* r0 polybius address */
/* r1 code 1 */
/* r2 code 2 */
/* r0 return character */
decodPosCode:
push {r1-r5,lr} @ save registers
ldr r4,iAdrszListCharCode
mov r3,#0
1:
ldrb r5,[r4,r3]
cmp r5,#0
beq 2f
cmp r5,r1
moveq r1,r3
cmp r5,r2
moveq r2,r3
add r3,r3,#1
b 1b
2:
mov r5,#SIZE
mul r1,r5,r1
add r1,r1,r2
ldrb r0,[r0,r1]
100:
pop {r1-r5,pc}
/******************************************************************/
/* shuffle strings algorithme Fisher-Yates */
/******************************************************************/
/* r0 contains the address of the string */
/* r1 contains string length */
/* r2 contains address result string */
shufflestrings:
push {r1-r4,lr} @ save registers
mov r3,#0
1: @ loop copy string in result
ldrb r4,[r0,r3]
strb r4,[r2,r3]
add r3,r3,#1
cmp r3,r1
ble 1b
sub r1,r1,#1 @ last element
2:
mov r0,r1 @ limit random number
bl genereraleas @ call random
ldrb r4,[r2,r1] @ load byte string index loop
ldrb r3,[r2,r0] @ load byte string random index
strb r3,[r2,r1] @ and exchange
strb r4,[r2,r0]
subs r1,r1,#1
cmp r1,#1
bge 2b
100:
pop {r1-r4,pc} @ restaur registers
/***************************************************/
/* Generation random number */
/***************************************************/
/* r0 contains limit */
genereraleas:
push {r1-r4,lr} @ save registers
ldr r4,iAdriGraine
ldr r2,[r4]
ldr r3,iNbDep1
mul r2,r3,r2
ldr r3,iNbDep1
add r2,r2,r3
str r2,[r4] @ save seed for next call
cmp r0,#0
beq 100f
mov r1,r0 @ divisor
mov r0,r2 @ dividende
bl division
mov r0,r3 @ résult = remainder
100: @ end function
pop {r1-r4,pc} @ restaur registers
iAdriGraine: .int iGraine
iNbDep1: .int 0x343FD
iNbDep2: .int 0x269EC3
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
{{Out}}
<pre>
Program 32 bits start.
6 x 6 Polybius square:
| A D F G V X
---------------
A | C 7 S J 3 Y
D | 8 F K A Q U
F | 0 E W R 4 I
G | B O P Z 9 2
V | 6 M 5 L H 1
X | D X T G V N
switchman
Encrypted text :
DFDAXVFDAGVGGDXXFFXGFDAGDFXA
Decrypted text :
ATTACKAT1200AM
</pre>
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_set>
#include <vector>
const std::string ADFGVX = "ADFGVX";
const std::string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
std::random_device random;
std::mt19937 mersenne_twister(random());
std::vector<std::vector<char>> initialise_polybius_square() {
std::vector<char> letters(ALPHABET.begin(), ALPHABET.end());
std::shuffle(letters.begin(), letters.end(), mersenne_twister);
std::vector<std::vector<char>> result = { 6, std::vector<char>(6, 0) };
for ( int32_t row = 0; row < 6; ++row ) {
for ( int32_t column = 0; column < 6; ++column ) {
result[row][column] = letters[6 * row + column];
}
}
return result;
}
// Create a key using a word from the dictionary 'unixdict.txt'
std::string create_key(const uint64_t& size) {
if ( size < 7 || size > 12 ) {
throw std::invalid_argument("Key should contain between 7 and 12 letters, both inclusive.");
}
std::vector<std::string> candidates;
std::fstream file_stream;
file_stream.open("../unixdict.txt");
std::string word;
while ( file_stream >> word ) {
if ( word.length() == size &&
word.length() == std::unordered_set<char>{ word.begin(), word.end() }.size() ) {
std::transform(word.begin(), word.end(), word.begin(), [](const char& ch){ return std::toupper(ch); });
if ( word.find_first_not_of(ALPHABET) == std::string::npos ) {
candidates.emplace_back(word);
}
}
}
std::shuffle(candidates.begin(), candidates.end(), mersenne_twister);
std::string key = candidates[0];
return key;
}
std::string encrypt(const std::string& plain_text,
const std::vector<std::vector<char>>& polybius,
const std::string& key) {
std::string code = "";
for ( const char& ch : plain_text ) {
for ( int32_t row = 0; row < 6; ++row ) {
for ( int32_t column = 0; column < 6; ++column ) {
if ( polybius[row][column] == ch ) {
code += ADFGVX[row];
code += ADFGVX[column];
}
}
}
}
std::string encrypted = "";
for ( const char& ch : key ) {
for ( uint64_t i = key.find(ch); i < code.length(); i += key.length() ) {
encrypted += code[i];
}
encrypted += " ";
}
return encrypted;
}
std::string decrypt(const std::string& encrypted_text,
const std::vector<std::vector<char>>& polybius,
const std::string& key) {
const uint64_t space_count = std::count(encrypted_text.begin(), encrypted_text.end(), ' ');
const uint64_t code_size = encrypted_text.length() - space_count;
std::vector<std::string> blocks;
std::stringstream stream(encrypted_text);
std:: string word;
while ( stream >> word ) {
blocks.emplace_back(word);
}
std::string code = "";
for ( int32_t i = 0; code.length() < code_size; ++i ) {
for ( const std::string& block : blocks ) {
if ( code.length() < code_size ) {
code += block[i];
}
}
}
std::string plain_text = "";
for ( uint64_t i = 0; i < code_size - 1; i += 2 ) {
int32_t row = ADFGVX.find(code[i]);
int32_t column = ADFGVX.find(code[i + 1]);
plain_text += polybius[row][column];
}
return plain_text;
}
int main() {
const std::vector<std::vector<char>> polybius = initialise_polybius_square();
std::cout << "The 6 x 6 Polybius square:" << std::endl;
std::cout << " | A D F G V X" << std::endl;
std::cout << "--------------" << std::endl;
for ( int32_t row = 0; row < 6; ++row ) {
std::cout << ADFGVX[row] << "|";
for ( int32_t column = 0; column < 6; ++column ) {
std::cout << " " << polybius[row][column];
}
std::cout << std::endl;
}
std::cout << std::endl;
const std::string key = create_key(9);
std::cout << "The key is " << key << std::endl << std::endl;
const std::string plain_text = "ATTACKAT1200AM";
std::cout << "Plain text: " << plain_text <<std::endl << std::endl;
const std::string encrypted_text = encrypt(plain_text, polybius, key);
std::cout << "Encrypted: " << encrypted_text << std::endl << std::endl;
const std::string decrypted_text = decrypt(encrypted_text, polybius, key);
std::cout << "Decrypted: " << decrypted_text << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>
The 6 x 6 Polybius square:
| A D F G V X
--------------
A| X Q I Z 1 K
D| A O Y 2 S 5
F| G 7 J R 9 T
G| 0 3 E P C V
V| N 6 D L H B
X| U 4 F W M 8
The key is HORSETAIL
Plain text: ATTACKAT1200AM
Encrypted: DVDV AAG FXG XDA FAG XFA DXD AAA GVX
Decrypted: ATTACKAT1200AM
</pre>
=={{header|F_Sharp|F#}}==
<
// ADFGVX cipher. Nigel Galloway: August 23rd., 2021
let polybus=let n=[|yield! {'A'..'Z'}; yield! {'0'..'9'}|] in MathNet.Numerics.Combinatorics.GeneratePermutation 36|>Array.map(fun g->n.[g]),[|'A';'D';'F';'G';'V';'X'|]
Line 137 ⟶ 1,849:
let encrypt,decrypt=ADFGVX polybus "nigel" //Using "nigel" as the key no hacker will guess that!
let n=encrypt "ATTACKAT1200AM" in printfn $"\nATTACKAT1200AM encrypted is %s{n} which decrypted is %s{decrypt n}"
</syntaxhighlight>
{{out}}
<pre>
Line 154 ⟶ 1,866:
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 336 ⟶ 2,048:
plainText2 := decrypt(polybius, key, cipherText)
fmt.Println("\nDecrypted :", plainText2)
}</
{{out}}
Line 359 ⟶ 2,071:
Decrypted : ATTACKAT1200AM
</pre>
=={{header|J}}==
Implementation:<syntaxhighlight lang="j">polybius=: {{6 6$8 u:({~?~&#)(48+i.10),65+i.26}}
lenword=: {{ ;({~ ?@#)(#~ (-:~.)@>)(#~ y=#@>)cutLF fread'unixdict.txt'}}
ADFGVX=: {{ deb,' ',.n/:~|:(-#n)]\'ADFGVX'{~,($m)#:(,m)i.y([-.-.),m }}
XVGFDA=: {{ (,m){~($m)#.'ADFGVX'i._2]\deb,|:(>cut y)/:/:n }}</syntaxhighlight>
Example:<syntaxhighlight lang="j"> echo W=: lenword 9
roughcast
echo P=: polybius ''
PV5M6Q
KR0391
4ZS7LA
FUT28E
GXOBYW
ICJDNH
echo E=: P ADFGVX W 'ATTACKAT1200AM'
FFF FGF FFF GXD XDG FDGG XDX XXA GAD
echo D=: P XVGFDA W E
ATTACKAT1200AM</syntaxhighlight>
That said, note that we could also eliminate spaces from the encrypted text, as they are recoverable if we have the key word.
<syntaxhighlight lang="j">spaces=: {{deb y#inv~,0,.(/:n){*|:_9]\>:i.#y=.y-.' '}}
echo S=: E-.' '
FFFFGFFFFGXDXDGFDGGXDXXXAGAD
P spaces W S
FFF FGF FFF GXD XDG FDGG XDX XXA GAD</syntaxhighlight>
(Technically, we do not need the Polybius square to recover the spaces, but it's passed as an argument here for symmetry.)
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public final class ADFGVXCipher {
public static void main(String[] args) throws IOException {
final char[][] polybius = initialisePolybiusSquare();
System.out.println("The 6 x 6 Polybius square:");
System.out.println(" | A D F G V X");
System.out.println("--------------");
for ( int row = 0; row < 6; row++ ) {
System.out.print(ADFGVX.charAt(row) + "|");
for ( int column = 0; column < 6; column++ ) {
System.out.print(" " + polybius[row][column]);
}
System.out.println();
}
System.out.println();
final String key = createKey(9);
System.out.println("The key is " + key);
System.out.println();
final String plainText = "ATTACKAT1200AM";
System.out.println("Plain text: " + plainText);
System.out.println();
final String encryptedText = encrypt(plainText, polybius, key);
System.out.println("Encrypted: " + encryptedText);
System.out.println();
final String decryptedText = decrypt(encryptedText, polybius, key);
System.out.println("Decrypted: " + decryptedText);
}
private static String encrypt(String plainText, char[][] polybius, String key) {
String code = "";
for ( char ch : plainText.toCharArray() ) {
for ( int row = 0; row < 6; row++ ) {
for ( int column = 0; column < 6; column++ ) {
if ( polybius[row][column] == ch ) {
code += ADFGVX.charAt(row) + "" + ADFGVX.charAt(column);
}
}
}
}
String encrypted = "";
for ( char ch : key.toCharArray() ) {
for ( int i = key.indexOf(ch); i < code.length(); i += key.length() ) {
encrypted += code.charAt(i);
}
encrypted += " ";
}
return encrypted;
}
private static String decrypt(String encryptedText, char[][] polybius, String key) {
final int codeSize = encryptedText.replace(" ", "").length();
String code = "";
for ( int i = 0; code.length() < codeSize; i++ ) {
for ( String block : encryptedText.split(" ") ) {
if ( code.length() < codeSize ) {
code += block.charAt(i);
}
}
}
String plainText = "";
for ( int i = 0; i < codeSize - 1; i += 2 ) {
int row = ADFGVX.indexOf(code.substring(i, i + 1));
int column = ADFGVX.indexOf(code.substring(i + 1, i + 2));
plainText += polybius[row][column];
}
return plainText;
}
// Create a key using a word from the dictionary 'unixdict.txt'
private static String createKey(int size) throws IOException {
if ( size < 7 || size > 12 ) {
throw new AssertionError("Key should contain between 7 and 12 letters, both inclusive.");
}
List<String> candidates = Files.lines(Path.of("unixdict.txt"))
.filter( word -> word.length() == size )
.filter( word -> word.chars().distinct().count() == word.length() )
.filter( word -> word.chars().allMatch(Character::isLetterOrDigit) )
.collect(Collectors.toList());
Collections.shuffle(candidates);
return candidates.get(0).toUpperCase();
}
private static char[][] initialisePolybiusSquare() {
List<String> letters = Arrays.asList("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split(""));
Collections.shuffle(letters);
char[][] result = new char[6][6];
for ( int row = 0; row < 6; row++ ) {
for ( int column = 0; column < 6; column++ ) {
result[row][column] = letters.get(6 * row + column).charAt(0);
}
}
return result;
}
private static final String ADFGVX = "ADFGVX";
}
</syntaxhighlight>
{{ out }}
<pre>
The 6 x 6 Polybius square:
| A D F G V X
--------------
A| 2 K 4 Q 9 8
D| W C I M V A
F| 5 S 0 Y B D
G| F T O U J E
V| 6 1 R X 3 G
X| H L Z P 7 N
The key is DISHWATER
Plaintext: ATTACKAT1200AM
Encrypted: DDAG XAA GDF DDF GXF DGF DDD XVX DDD
Decrypted: ATTACKAT1200AM
</pre>
=={{header|Julia}}==
<
The ADFGVX cipher.
See also eg. https://www.nku.edu/~christensen/092hnr304%20ADFGVX.pdf
Line 433 ⟶ 2,310:
println("Encoded: $encoded")
println("Decoded: $decoded")
</
<pre>
Polybius: L4VZJIB8OXGFM1H3CTNKU9PE75WQ2DAYRS06, Key: SUNFLOWER
Line 445 ⟶ 2,322:
It started as a translation, but actually we use a different method, better suited to Nim, to encrypt and decrypt. And there are many other differences. Output is similar though.
<
const Adfgvx = "ADFGVX"
Line 545 ⟶ 2,422:
let plainText = cipherText.decrypt(polybius, key)
echo "\nDecrypted : ", plainText</
{{out}}
Line 568 ⟶ 2,445:
=={{header|Perl}}==
<
use strict; # https://rosettacode.org/wiki/ADFGVX_cipher
Line 618 ⟶ 2,495:
}
sub transpose { map join('', map {s/.// ? $& : ''} @_), 1 .. length $_[0] }</
{{out}}
<pre>
Line 643 ⟶ 2,520:
=={{header|Phix}}==
{{trans|Wren}} We can make some nice use of the standard builtin routines here, with only a modest amount of whitespace cleanup.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">ADFGVX</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"ADFGVX"</span><span style="color: #0000FF;">,</span>
Line 716 ⟶ 2,593:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nPlainText : %s\n\nEncrypted : %s\n\nDecrypted : %s\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">plaintext</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">encrypted</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">decrypted</span><span style="color: #0000FF;">})</span>
<!--</
Output matches Wren/Nim/wp when the appropriate lines are uncommented.
Line 724 ⟶ 2,601:
According to historical sources the original encrypted text was spaced in 5 character blocks regardless
of message and key length, which means that decryption should not rely on spacing.
<
The ADFGVX cipher implemented as a Python class
See also eg. https://www.nku.edu/~christensen/092hnr304%20ADFGVX.pdf
Line 784 ⟶ 2,661:
SECRET, MESSAGE = ADFGVX(POLYBIUS, KEY), 'ATTACKAT1200AM'
print(f'Polybius: {POLYBIUS}, key: {KEY}')
print('Message: ', MESSAGE)
ENCODED = SECRET.encrypt(MESSAGE)
Line 790 ⟶ 2,667:
print('Encoded: ', ENCODED)
print('Decoded: ', DECODED)
</
<pre>
Polybius: A9GKMF1DQRSBVX8Z0WTEJLOPY5U4CN2H76I3, key: volcanism
Line 800 ⟶ 2,677:
=={{header|Raku}}==
Slightly different results from every other entry so far. See discussion page for reasons. It is impossible to tell from casual observation which column comes first in the Raku example. In every other (so far), the sub group with 4 characters is the first column.
<syntaxhighlight lang="raku"
my @header = < A D F G V X >;
Line 832 ⟶ 2,709:
my @order = $key.comb.pairs.sort( *.value )».key.pairs.sort( *.value )».key;
%cypher{ ( grep { /\w/ }, flat [Z] @order.map( { |@text.batch($key-length)».[$_] } ) ).batch(2)».join }.join;
}</
{{out}}
<pre style="line-height:1.2em;">Key: GHOSTLIKE
Line 856 ⟶ 2,733:
Encoded: DVVA FVX XXA FGF XVX GXA XXD GFA XXD
</pre>
=={{header|REXX}}==
<syntaxhighlight lang="rexx">
/* REXX */
cls
eol=x2c(0D0A) ; msg="ATTACKAT1200AM"
keyword= upper('lifeguard') ; cyph= 'ADFGVX'
s_sort= keyword ; new_key= ''
do while length(s_sort) > 0
nmax= 0
do i=1 to length(s_sort)
ch= substr(s_sort,i,1)
num= c2d(ch)
if num > nmax then do
nmax= num
max_i = i
end
end
s_sort= delstr(s_sort,max_i,1)
new_key= d2c(nmax)||new_key
end /* Alphabetical sorting */
j=0 ; num_str= '' ; rnd_s= ''
do while j < 36
num= random(0,35)
if wordpos(num,num_str) = 0 then do
j= j + 1
num_str= num_str||num||' '
if num >= 10 then do
num= num - 10 + x2d(41)
num= d2c(num)
end
rnd_s= rnd_s||num
end
end /* say 'Generated string: '||rnd_s||eol */
say 'Polybius square:'||eol
call tab cyph, rnd_s ,1
say "Only characters from the '"|| msg||"'"||eol
t= translate(rnd_s,' ',msg)
_t= translate(rnd_s,' ',t)
call tab cyph, _t ,1
len_c= length(cyph) ; cyph_T=''
do i=1 to len_c
ch_i= substr(cyph,i,1)
do j=1 to len_c
ch_j= substr(cyph,j,1)
cyph_T= cyph_T||ch_i||ch_j||' '
end
end
enc_msg= ''
do i=1 to length(msg)
ch= substr(msg,i,1)
j= pos(ch,rnd_s,1)
enc_msg= enc_msg||word(cyph_T,j)
end
say "Conversion by table: "||eol||eol||msg||" ==> "||enc_msg||eol
call tab keyword, enc_msg
len= length(keyword)
n_row= 0 ; column.= ''
do while enc_msg <> ''
parse var enc_msg 1 s1 +(len) enc_msg
n_row= n_row+1
do m= 1 to len
ch_m= substr(s1,m,1)
column.m= column.m||ch_m
end
end
s_lst= ''
do m= 1 to len
ch= substr(new_key,m,1)
i= pos(ch,keyword,1)
w_i= column.i
s_lst= s_lst||w_i||' '
end
row.= '' ; t_row= ''
do i=1 to len
w_i= word(s_lst,i)
do j=1 to n_row
row.j= row.j||substr(w_i,j,1)
end
end
do j=1 to n_row; t_row= t_row||row.j; end
say "Sorted by columns:"||eol
call tab new_key, t_row
say '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
say 'Encrypted message: '||s_lst
say '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
exit
tab:
parse arg h, s, p /* header, string, param */
lh= length(h) ;
s= h||copies('-',lh)||s ; ls= length(s)
h=' -'||h
t= '' ; j= 1
do i= 1 to ls by lh
row= substr(s,i,lh)
r_ch= ''
do l=1 to lh
ch= substr(row,l,1)
r_ch= r_ch||ch||' '
end
row= r_ch
if p <> '' then row= row||'|'||substr(h,j,1)
t= t||row||eol
j= j + 1
end
say t
return
</syntaxhighlight>
{{out}}
<pre>
Polybius square:
A D F G V X |
- - - - - - |-
D H 8 P U 4 |A
E T S C 5 9 |D
A 3 Z F R I |F
Y O 1 Q 7 W |G
J 2 G V N X |V
L 6 0 K M B |X
Only characters from the 'ATTACKAT1200AM'
A D F G V X |
- - - - - - |-
|A
T C |D
A |F
1 |G
2 |V
0 K M |X
Conversion by table:
ATTACKAT1200AM ==> FADDDDFADGXGFADDGFVDXFXFFAXV
L I F E G U A R D
- - - - - - - - -
F A D D D D F A D
G X G F A D D G F
V D X F X F F A X
V
Sorted by columns:
A D E F G I L R U
- - - - - - - - -
F D D D D A F A D
D F F G A X G G D
F X F X X D V A F
V
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Encrypted message: FDF DFX DFF DGX DAX AXD FGVV AGA DDF
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
</pre>
=={{header|Rust}}==
<syntaxhighlight lang="rust">// This version formats the encrypted text in 5 character blocks, as the historical version apparently did.
use fastrand::shuffle;
use std::collections::HashMap;
static ADFGVX: &str = "ADFGVX";
#[derive(Clone, Eq, Hash, PartialEq)]
struct CPair(char, char);
/// The WWI German ADFGVX cipher.
struct AdfgvxCipher {
polybius: Vec<char>,
key: Vec<char>,
encode: HashMap<char, CPair>,
decode: HashMap<CPair, char>,
}
/// Set up the encoding and decoding for the ADFGVX cipher.
fn cipher(allowed_chars: String, encrypt_key: String) -> AdfgvxCipher {
let alphabet = allowed_chars.to_uppercase().chars().collect::<Vec<_>>();
assert!(alphabet.len() == ADFGVX.len() * ADFGVX.len());
let mut polybius = alphabet.clone();
shuffle(&mut polybius);
let key = encrypt_key.to_uppercase().chars().collect::<Vec<_>>();
let adfgvx: Vec<char> = String::from(ADFGVX).chars().collect();
let mut pairs: Vec<CPair> = [CPair(' ', ' '); 0].to_vec();
for c1 in &adfgvx {
for c2 in &adfgvx {
pairs.push(CPair(*c1, *c2));
}
}
let mut encode: HashMap<char, CPair> = HashMap::new();
for i in 0..pairs.len() {
encode.insert(polybius[i], pairs[i].clone());
}
let mut decode = HashMap::new();
for (k, v) in &encode {
decode.insert(v.clone(), *k);
}
return AdfgvxCipher {
polybius,
key,
encode,
decode,
};
}
/// Encrypt with the ADFGVX cipher.
fn encrypt(a: &AdfgvxCipher, msg: String) -> String {
let umsg: Vec<char> = msg
.clone()
.to_uppercase()
.chars()
.filter(|c| a.polybius.contains(c))
.collect();
let mut fractionated = vec![' '; 0].to_vec();
for c in umsg {
let cp = a.encode.get(&c).unwrap();
fractionated.push(cp.0);
fractionated.push(cp.1);
}
let ncols = a.key.len();
let extra = fractionated.len() % ncols;
if extra > 0 {
fractionated.append(&mut vec!['\u{00}'; ncols - extra]);
}
let nrows = fractionated.len() / ncols;
let mut sortedkey = a.key.clone();
sortedkey.sort();
let mut ciphertext = String::from("");
let mut textlen = 0;
for j in 0..ncols {
let k = a.key.iter().position(|c| *c == sortedkey[j]).unwrap();
for i in 0..nrows {
let ch: char = fractionated[i * ncols + k];
if ch != '\u{00}' {
ciphertext.push(ch);
textlen += 1;
if textlen % 5 == 0 {
ciphertext.push(' ');
}
}
}
}
return ciphertext;
}
/// Decrypt with the ADFGVX cipher. Does not depend on spacing of encoded text
fn decrypt(a: &AdfgvxCipher, cod: String) -> String {
let chars: Vec<char> = cod.chars().filter(|c| *c != ' ').collect();
let mut sortedkey = a.key.clone();
sortedkey.sort();
let order: Vec<usize> = sortedkey
.iter()
.map(|c| a.key.iter().position(|kc| kc == c).unwrap())
.collect();
let originalorder: Vec<usize> = a
.key
.iter()
.map(|c| sortedkey.iter().position(|kc| kc == c).unwrap())
.collect();
let q = chars.len() / a.key.len();
let r = chars.len() % a.key.len();
let strides: Vec<usize> = order
.iter()
.map(|i| {q + {if r > *i {1} else {0}}}).collect();
let mut starts: Vec<usize> = vec![0_usize; 1].to_vec();
let mut stridesum = 0;
for i in 0..strides.len() - 1 {
stridesum += strides[i];
starts.push(stridesum);
}
let ends: Vec<usize> = (0..a.key.len()).map(|i| (starts[i] + strides[i])).collect(); // shuffled ends of columns
let cols: Vec<Vec<char>> = originalorder
.iter()
.map(|i| (chars[starts[*i]..ends[*i]]).to_vec())
.collect(); // get reordered columns
let nrows = (chars.len() - 1) / a.key.len() + 1;
let mut fractionated = vec![' '; 0].to_vec();
for i in 0..nrows {
for j in 0..a.key.len() {
if i < cols[j].len() {
fractionated.push(cols[j][i]);
}
}
}
let mut decoded = String::from("");
for i in 0..fractionated.len() - 1 {
if i % 2 == 0 {
let cp = CPair(fractionated[i], fractionated[i + 1]);
decoded.push(*a.decode.get(&cp).unwrap());
}
}
return decoded;
}
fn main() {
let msg = String::from("ATTACKAT1200AM");
let encrypt_key = String::from("volcanism");
let allowed_chars: String = String::from("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
let adf = cipher(allowed_chars, encrypt_key.clone());
println!("Message: {msg}");
println!("Polybius: {:?}", adf.polybius.iter().collect::<String>());
println!("Key: {encrypt_key}");
let encrypted_message = encrypt(&adf, msg.clone());
println!("Encoded: {encrypted_message}");
let decoded = decrypt(&adf, encrypted_message);
println!("Decoded: {decoded:?}");
}
</syntaxhighlight>{{out}}
<pre>
Message: ATTACKAT1200AM
Polybius: "EKFG7HOYZQPUD536SI2NT0W4A1XJLM8CBRV9"
Key: volcanism
Encoded: GAGFV GVFVG DGXDV FGGAA AAVAV DGX
Decoded: "ATTACKAT1200AM"
</pre>
Decoded: ATTACKAT1200AM</pre>
=={{header|Uiua}}==
Quick and simple solution for demonstration purposes.
<syntaxhighlight lang="uiua">
A ← ↯∞_2⊞⊟."ADFGVX"
P ← ⊏⍏[⍥⚂]⧻."ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
K ← "PACKHORSE"
Sw ← ⊜□≠" ".
Polybius ← setinv(/⊂⊡≡(⊚⌕):⊙:¤P A|⊏:P≡(⊢⊚⌕)⊙¤∩≡□↯∞_2:A)
SortByK ← setinv(⊏⍏⟜(⍉⬚" "↯⊟∞⧻)K|▽≠" "./⊂⍉⊏⍏⍏K)
Str ← setinv(/$"_ _"Sw/⊂≡(⊂:" ")|≡(⊢°□◇⬚" "↯⊟∞)/↥≡◇⧻.Sw)
Pt ← "ATTACKAT1200AM"
&p $"Key = _\nPolybius:\n_\nPlaintext = _"K↯⊟.√⧻.P Pt
&p$"Crypttext = _". Str SortByK Polybius Pt
&p$"Plaintext = _" °Polybius °SortByK °Str
</syntaxhighlight>
{{out}}
<pre>
Key = PACKHORSE
Polybius:
╭─
╷ "7ENLUB"
"WOJ69G"
"8ZFAKP"
"I4R3C1"
"V5TMHD"
"SY0XQ2"
╯
Plaintext = ATTACKAT1200AM
Crypttext = GFX VVX GXV VGX FFF FVF FVXG FFF GGG
Plaintext = ATTACKAT1200AM
</pre>
=={{header|Wren}}==
Line 863 ⟶ 3,105:
{{libheader|Wren-seq}}
{{libheader|Wren-str}}
<
import "./ioutil" for FileUtil
import "./seq" for Lst
import "./str" for Char, Str
var rand = Random.new()
Line 966 ⟶ 3,208:
System.print("\nEncrypted : %(cipherText)")
var plainText2 = decrypt.call(polybius, key, cipherText)
System.print("\nDecrypted : %(plainText2)")</
{{out}}
|