Playfair cipher: Difference between revisions
Content added Content deleted
(→{{header|APL}}: Add implementation) |
Thundergnat (talk | contribs) m (sntax highlighting fixup automation) |
||
Line 19: | Line 19: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F uniq(seq) |
||
[Char] seen |
[Char] seen |
||
L(x) seq |
L(x) seq |
||
Line 78: | Line 78: | ||
V enc = playfair.encode(orig) |
V enc = playfair.encode(orig) |
||
print(‘Encoded: ’enc) |
print(‘Encoded: ’enc) |
||
print(‘Decoded: ’playfair.decode(enc))</ |
print(‘Decoded: ’playfair.decode(enc))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 89: | Line 89: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">⍝⍝⍝⍝ Utility functions |
||
⍝ convert to uppercase |
⍝ convert to uppercase |
||
Line 165: | Line 165: | ||
plain ← 'Hide the gold in the tree stump.' |
plain ← 'Hide the gold in the tree stump.' |
||
⎕ ← cipher ← key EncodeText plain |
⎕ ← cipher ← key EncodeText plain |
||
⎕ ← key DecodeText cipher</ |
⎕ ← key DecodeText cipher</syntaxhighlight> |
||
{{Out}}<pre> ⎕ ← cipher ← key EncodeText plain |
{{Out}}<pre> ⎕ ← cipher ← key EncodeText plain |
||
Line 179: | Line 179: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <string> |
#include <string> |
||
Line 288: | Line 288: | ||
cout << "Enter the text: "; getline( cin, txt ); |
cout << "Enter the text: "; getline( cin, txt ); |
||
playfair pf; pf.doIt( key, txt, ij, e ); return system( "pause" ); |
playfair pf; pf.doIt( key, txt, ij, e ); return system( "pause" ); |
||
}</ |
}</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
(E)ncode or (D)ecode? e |
(E)ncode or (D)ecode? e |
||
Line 312: | Line 312: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.array, std.algorithm, std.range, std.ascii, |
||
std.conv, std.string, std.regex, std.typecons; |
std.conv, std.string, std.regex, std.typecons; |
||
Line 382: | Line 382: | ||
writeln(" Encoded: ", enc); |
writeln(" Encoded: ", enc); |
||
writeln(" Decoded: ", pf.decode(enc)); |
writeln(" Decoded: ", pf.decode(enc)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Original: Hide the gold in...the TREESTUMP!!! |
<pre>Original: Hide the gold in...the TREESTUMP!!! |
||
Line 389: | Line 389: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Enum PlayFairOption |
Enum PlayFairOption |
||
Line 544: | Line 544: | ||
Print |
Print |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
Sample input/output: |
Sample input/output: |
||
{{out}} |
{{out}} |
||
Line 566: | Line 566: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 773: | Line 773: | ||
decodedText := pf.decode(encodedText) |
decodedText := pf.decode(encodedText) |
||
fmt.Println("Deccoded text is :", decodedText) |
fmt.Println("Deccoded text is :", decodedText) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 797: | Line 797: | ||
{{incorrect|Haskell|TREESTUMP -> TREXSTUMPX, should be TREXESTUMP}} |
{{incorrect|Haskell|TREESTUMP -> TREXSTUMPX, should be TREXESTUMP}} |
||
(My guess is that map (\[x, y] -> if x == y then [x, 'x'] else [x, y]).chunksOf 2 is simply discarding the y. [[User:Petelomax|Pete Lomax]] ([[User talk:Petelomax|talk]]) 05:54, 13 October 2018 (UTC)) |
(My guess is that map (\[x, y] -> if x == y then [x, 'x'] else [x, y]).chunksOf 2 is simply discarding the y. [[User:Petelomax|Pete Lomax]] ([[User talk:Petelomax|talk]]) 05:54, 13 October 2018 (UTC)) |
||
< |
<syntaxhighlight lang="haskell"> |
||
import Control.Monad (guard) |
import Control.Monad (guard) |
||
import Data.Array (Array, assocs, elems, listArray, (!)) |
import Data.Array (Array, assocs, elems, listArray, (!)) |
||
Line 899: | Line 899: | ||
| odd (length str) = str ++ "x" |
| odd (length str) = str ++ "x" |
||
| otherwise = str |
| otherwise = str |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 913: | Line 913: | ||
'''Implementation:''' |
'''Implementation:''' |
||
< |
<syntaxhighlight lang="j">choose=: verb define |
||
sel=. 'Q' e. y |
sel=. 'Q' e. y |
||
alph=: (sel { 'JQ') -.~ a. {~ 65 + i.26 |
alph=: (sel { 'JQ') -.~ a. {~ 65 + i.26 |
||
Line 948: | Line 948: | ||
decrypt=: verb define |
decrypt=: verb define |
||
, ref{~alt i. pairs y |
, ref{~alt i. pairs y |
||
)</ |
)</syntaxhighlight> |
||
'''Example use:''' |
'''Example use:''' |
||
< |
<syntaxhighlight lang="j"> choose 'IJ' |
||
setkey 'playfair example' |
setkey 'playfair example' |
||
Line 957: | Line 957: | ||
BM OD ZB XD NA BE KU DM UI XM MO UV IF |
BM OD ZB XD NA BE KU DM UI XM MO UV IF |
||
decrypt 'BM OD ZB XD NA BE KU DM UI XM MO UV IF' |
decrypt 'BM OD ZB XD NA BE KU DM UI XM MO UV IF' |
||
HIDETHEGOLDINTHETREXESTUMP</ |
HIDETHEGOLDINTHETREXESTUMP</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">import java.awt.Point; |
||
import java.util.Scanner; |
import java.util.Scanner; |
||
Line 1,063: | Line 1,063: | ||
return text.toString(); |
return text.toString(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=== alternative version === |
=== alternative version === |
||
< |
<syntaxhighlight lang="java">import java.util.Scanner; |
||
public class PlayfairCipherEncryption |
public class PlayfairCipherEncryption |
||
Line 1,271: | Line 1,271: | ||
sc.close(); |
sc.close(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">function playfair(key, txt, isencode=true, from = "J", to = "") |
||
to = (to == "" && from == "J") ? "I" : to |
to = (to == "" && from == "J") ? "I" : to |
||
Line 1,334: | Line 1,334: | ||
println("Decoded: ", playfair("Playfair example", encoded, false)) |
println("Decoded: ", playfair("Playfair example", encoded, false)) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Original: Hide the gold in...the TREESTUMP!!! |
Original: Hide the gold in...the TREESTUMP!!! |
||
Line 1,343: | Line 1,343: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="scala">// version 1.0.5-2 |
||
enum class PlayfairOption { |
enum class PlayfairOption { |
||
Line 1,475: | Line 1,475: | ||
val decodedText = playfair.decode(encodedText) |
val decodedText = playfair.decode(encodedText) |
||
println("Decoded text is : $decodedText") |
println("Decoded text is : $decodedText") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,496: | Line 1,496: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[MakeTranslationTable, PlayfairCipher, PlayfairDecipher] |
||
MakeTranslationTable[tt_List] := Module[{poss, in, out}, |
MakeTranslationTable[tt_List] := Module[{poss, in, out}, |
||
poss = Tuples[Tuples[Range[5], 2], 2]; |
poss = Tuples[Tuples[Range[5], 2], 2]; |
||
Line 1,567: | Line 1,567: | ||
] |
] |
||
PlayfairCipher["Hide the gold in...the TREESTUMP!!!", "Playfair example"] |
PlayfairCipher["Hide the gold in...the TREESTUMP!!!", "Playfair example"] |
||
PlayfairDecipher[%, "Playfair example"]</ |
PlayfairDecipher[%, "Playfair example"]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>BM OD ZB XD NA BE KU DM UI XM MO UV IF |
<pre>BM OD ZB XD NA BE KU DM UI XM MO UV IF |
||
Line 1,574: | Line 1,574: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="nim">import pegs, strutils |
||
type |
type |
||
Line 1,672: | Line 1,672: | ||
echo "Encoded message: ", enc |
echo "Encoded message: ", enc |
||
echo "Decoded message: ", dec</ |
echo "Decoded message: ", dec</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,682: | Line 1,682: | ||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 1,847: | Line 1,847: | ||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,870: | Line 1,870: | ||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
< |
<syntaxhighlight lang="oorexx">/*--------------------------------------------------------------------- |
||
* REXX program implements a PLAYFAIR cipher (encryption & decryption). |
* REXX program implements a PLAYFAIR cipher (encryption & decryption). |
||
* 11.11.2013 Walter Pachl revamped, for ooRexx, the REXX program |
* 11.11.2013 Walter Pachl revamped, for ooRexx, the REXX program |
||
Line 2,067: | Line 2,067: | ||
d=d||substr(x,j,2)' ' |
d=d||substr(x,j,2)' ' |
||
End |
End |
||
Return strip(d)</ |
Return strip(d)</syntaxhighlight> |
||
Output (sample): |
Output (sample): |
||
<pre>old cipher key: this is my little key |
<pre>old cipher key: this is my little key |
||
Line 2,090: | Line 2,090: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use Math::Cartesian::Product; |
||
# Pregenerate all forward and reverse translations |
# Pregenerate all forward and reverse translations |
||
Line 2,161: | Line 2,161: | ||
print " orig:\t$orig\n"; |
print " orig:\t$orig\n"; |
||
print "black:\t$black\n"; |
print "black:\t$black\n"; |
||
print " red:\t$red\n";</ |
print " red:\t$red\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> orig: Hide the gold in...the TREESTUMP!!! |
<pre> orig: Hide the gold in...the TREESTUMP!!! |
||
Line 2,170: | Line 2,170: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Originally translated from Kotlin, now uses a combined routine (playfair) for encoding and decoding, and direct char lookups, and x removal. |
Originally translated from Kotlin, now uses a combined routine (playfair) for encoding and decoding, and direct char lookups, and x removal. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">constant</span> <span style="color: #000000;">keyword</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Playfair example"</span><span style="color: #0000FF;">,</span> |
<span style="color: #008080;">constant</span> <span style="color: #000000;">keyword</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Playfair example"</span><span style="color: #0000FF;">,</span> |
||
Line 2,284: | Line 2,284: | ||
<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;">"Encoded text is : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">encoded</span><span style="color: #0000FF;">})</span> |
<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;">"Encoded text is : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">encoded</span><span style="color: #0000FF;">})</span> |
||
<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;">"Decoded text is : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">decoded</span><span style="color: #0000FF;">})</span> |
<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;">"Decoded text is : %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">decoded</span><span style="color: #0000FF;">})</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,306: | Line 2,306: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="python">from string import ascii_uppercase |
||
from itertools import product |
from itertools import product |
||
from re import findall |
from re import findall |
||
Line 2,367: | Line 2,367: | ||
enc = encode(orig) |
enc = encode(orig) |
||
print "Encoded:", enc |
print "Encoded:", enc |
||
print "Decoded:", decode(enc)</ |
print "Decoded:", decode(enc)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Original: Hide the gold in...the TREESTUMP!!! |
<pre>Original: Hide the gold in...the TREESTUMP!!! |
||
Line 2,375: | Line 2,375: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line># Instantiate a specific encoder/decoder. |
||
sub playfair( $key, |
sub playfair( $key, |
||
Line 2,437: | Line 2,437: | ||
my $red = decode $black; |
my $red = decode $black; |
||
say " red:\t$red";</ |
say " red:\t$red";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> orig: Hide the gold in...the TREESTUMP!!! |
<pre> orig: Hide the gold in...the TREESTUMP!!! |
||
Line 2,453: | Line 2,453: | ||
A fair amount of code was added to massage the decrypted encryption to remove doubled '''X'''es so as to match the original text |
A fair amount of code was added to massage the decrypted encryption to remove doubled '''X'''es so as to match the original text |
||
<br>(this is the ''possible text'' part of the REXX code). |
<br>(this is the ''possible text'' part of the REXX code). |
||
< |
<syntaxhighlight lang="rexx">/*REXX program implements a PLAYFAIR cipher (encryption and decryption). */ |
||
@abc= 'abcdefghijklmnopqrstuvwxyz'; @abcU= @abc /*literals for lower and upper ABC's.*/ |
@abc= 'abcdefghijklmnopqrstuvwxyz'; @abcU= @abc /*literals for lower and upper ABC's.*/ |
||
parse arg omit key '(' text /*TEXT is the phrase to be used. */ |
parse arg omit key '(' text /*TEXT is the phrase to be used. */ |
||
Line 2,540: | Line 2,540: | ||
if datatype(_, 'M') then $= $ || _ /*only use Latin letters. */ |
if datatype(_, 'M') then $= $ || _ /*only use Latin letters. */ |
||
end /*j*/ |
end /*j*/ |
||
return $</ |
return $</syntaxhighlight> |
||
Some older REXXes don't have a '''changestr''' bif, so one is included here ──► [[CHANGESTR.REX]]. |
Some older REXXes don't have a '''changestr''' bif, so one is included here ──► [[CHANGESTR.REX]]. |
||
<br><br> |
<br><br> |
||
Line 2,593: | Line 2,593: | ||
Printing the cipher in pairs just advertises the mechanism of encoding; I've gone with the traditional grouping into sequences of five letters instead. |
Printing the cipher in pairs just advertises the mechanism of encoding; I've gone with the traditional grouping into sequences of five letters instead. |
||
< |
<syntaxhighlight lang="ruby">class Playfair |
||
Size = 5 |
Size = 5 |
||
def initialize(key, missing) |
def initialize(key, missing) |
||
Line 2,646: | Line 2,646: | ||
end.join.split('').each_slice(5).map{|s|s.join}.join(' ') |
end.join.split('').each_slice(5).map{|s|s.join}.join(' ') |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 2,656: | Line 2,656: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">func playfair(key, from = 'J', to = (from == 'J' ? 'I' : '')) { |
||
func canon(str) { |
func canon(str) { |
||
Line 2,714: | Line 2,714: | ||
var red = decode(black) |
var red = decode(black) |
||
say " red:\t#{red}"</ |
say " red:\t#{red}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,723: | Line 2,723: | ||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
< |
<syntaxhighlight lang="sql"> |
||
--Clean up previous run |
--Clean up previous run |
||
IF EXISTS (SELECT * |
IF EXISTS (SELECT * |
||
Line 3,037: | Line 3,037: | ||
WHERE NAME = 'FairPlayTable') |
WHERE NAME = 'FairPlayTable') |
||
DROP TYPE FAIRPLAYTABLE |
DROP TYPE FAIRPLAYTABLE |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.6}} |
{{works with|Tcl|8.6}} |
||
< |
<syntaxhighlight lang="tcl">package require TclOO |
||
oo::class create Playfair { |
oo::class create Playfair { |
||
Line 3,127: | Line 3,127: | ||
return $result |
return $result |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Demonstrating: |
Demonstrating: |
||
< |
<syntaxhighlight lang="tcl">Playfair create cypher "Playfair Example" |
||
set plaintext "Hide the gold in...the TREESTUMP!!!" |
set plaintext "Hide the gold in...the TREESTUMP!!!" |
||
set encoded [cypher encode $plaintext] |
set encoded [cypher encode $plaintext] |
||
Line 3,135: | Line 3,135: | ||
puts "Original: $plaintext" |
puts "Original: $plaintext" |
||
puts "Encoded: $encoded" |
puts "Encoded: $encoded" |
||
puts "Decoded: $decoded"</ |
puts "Decoded: $decoded"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,144: | Line 3,144: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Option Explicit |
Option Explicit |
||
Line 3,351: | Line 3,351: | ||
SwapPairsDecoding = myTable(resD1.Row, resD1.Column) & myTable(resD2.Row, resD2.Column) |
SwapPairsDecoding = myTable(resD1.Row, resD1.Column) & myTable(resD2.Row, resD2.Column) |
||
End Select |
End Select |
||
End Function</ |
End Function</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Enter your keyword : Playfair example |
<pre>Enter your keyword : Playfair example |
||
Line 3,377: | Line 3,377: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="vlang">import os |
||
import strings |
import strings |
||
type PlayfairOption = int |
type PlayfairOption = int |
||
Line 3,565: | Line 3,565: | ||
decoded_text := pf.decode(encoded_text) |
decoded_text := pf.decode(encoded_text) |
||
println("Deccoded text is : $decoded_text") |
println("Deccoded text is : $decoded_text") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,592: | Line 3,592: | ||
{{libheader|Wren-trait}} |
{{libheader|Wren-trait}} |
||
{{libheader|Wren-ioutil}} |
{{libheader|Wren-ioutil}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/dynamic" for Enum |
||
import "/str" for Str, Char |
import "/str" for Str, Char |
||
import "/trait" for Stepped |
import "/trait" for Stepped |
||
Line 3,726: | Line 3,726: | ||
System.print("\nEncoded text is : %(encodedText)") |
System.print("\nEncoded text is : %(encodedText)") |
||
var decodedText = playfair.decode(encodedText) |
var decodedText = playfair.decode(encodedText) |
||
System.print("Decoded text is : %(decodedText)")</ |
System.print("Decoded text is : %(decodedText)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,747: | Line 3,747: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn genKeyTable(key,deadChr){ // deadChr=="Q" or "J" |
||
deadChr=deadChr.toUpper(); |
deadChr=deadChr.toUpper(); |
||
key=key.toUpper().unique() - " " - deadChr; |
key=key.toUpper().unique() - " " - deadChr; |
||
return(key + (["A".."Z"].pump(String) - deadChr - key), deadChr); |
return(key + (["A".."Z"].pump(String) - deadChr - key), deadChr); |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">fcn playfair(text,keytable){ // text is a-z only |
||
keyTable,deadChr:=keytable; |
keyTable,deadChr:=keytable; |
||
text=text.toUpper(); |
text=text.toUpper(); |
||
Line 3,775: | Line 3,775: | ||
}) |
}) |
||
.pump(String,Void.Read,"".create.fp(" ")).strip(); // insert blanks |
.pump(String,Void.Read,"".create.fp(" ")).strip(); // insert blanks |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">fcn decodePF(text,keyTable){ |
||
keyTable,_=keyTable; |
keyTable,_=keyTable; |
||
text-=" "; |
text-=" "; |
||
Line 3,792: | Line 3,792: | ||
.pump(String,Void.Read,"".create.fp(" ")).strip(); // insert blanks |
.pump(String,Void.Read,"".create.fp(" ")).strip(); // insert blanks |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="zkl">msg:="Hide the gold in the tree stump!!!"; |
||
keyTable:=genKeyTable("playfair example"); |
keyTable:=genKeyTable("playfair example"); |
||
msg.println(); |
msg.println(); |
||
e:=playfair(msg,keyTable); e.println(); |
e:=playfair(msg,keyTable); e.println(); |
||
decodePF(e,keyTable).println(); |
decodePF(e,keyTable).println(); |
||
playfair("XX",keyTable).println() : decodePF(_,keyTable).println();</ |
playfair("XX",keyTable).println() : decodePF(_,keyTable).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |