Data update
This commit is contained in:
parent
8e4e15fa56
commit
72eb4943cb
1853 changed files with 35514 additions and 9441 deletions
180
Task/Bifid-cipher/Go/bifid-cipher.go
Normal file
180
Task/Bifid-cipher/Go/bifid-cipher.go
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
/*
|
||||
Only use ASCII letters between A and Z or the Characters in square... . If the row with 'J' is removed from the squares,
|
||||
then the square is a 'Polybios square' and you must use removeSpaceI(text) to encrypt and decrypt
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
squareRosetta [][]byte = [][]byte{ //rosettacode
|
||||
{'A', 'B', 'C', 'D', 'E'},
|
||||
{'F', 'G', 'H', 'I', 'K'},
|
||||
{'L', 'M', 'N', 'O', 'P'},
|
||||
{'Q', 'R', 'S', 'T', 'U'},
|
||||
{'V', 'W', 'X', 'Y', 'Z'},
|
||||
{'J', '1', '2', '3', '4'},
|
||||
}
|
||||
|
||||
squareWikipedia [][]byte = [][]byte{ // wikipedia
|
||||
|
||||
{'B', 'G', 'W', 'K', 'Z'},
|
||||
{'Q', 'P', 'N', 'D', 'S'},
|
||||
{'I', 'O', 'A', 'X', 'E'},
|
||||
{'F', 'C', 'L', 'U', 'M'},
|
||||
{'T', 'H', 'Y', 'V', 'R'},
|
||||
{'J', '1', '2', '3', '4'},
|
||||
}
|
||||
|
||||
textRosetta string = "0ATTACKATDAWN"
|
||||
textRosettaEncoded string = "DQBDAXDQPDQH" // only for test
|
||||
textWikipedia string = "FLEEATONCE"
|
||||
textWikipediaEncoded string = "UAEOLWRINS" // only for test
|
||||
textTest string = "The invasion will start on the first of January"
|
||||
textTextEncoded string = "RASOAQXFIOORXESXADETSWLTNIAZQOISBRGBALY" // only for test
|
||||
)
|
||||
|
||||
type koord struct {
|
||||
X byte
|
||||
Y byte
|
||||
}
|
||||
|
||||
func (k *koord) LessThen(other *koord) bool {
|
||||
if k.Y > other.Y {
|
||||
return false
|
||||
}
|
||||
if k.Y < other.Y {
|
||||
return true
|
||||
}
|
||||
if k.X < other.X {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (k *koord) EqualTo(other koord) bool {
|
||||
if k.X == other.X && k.Y == other.Y {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var encryptMap map[byte]koord
|
||||
var decryptMap map[koord]byte
|
||||
|
||||
func squareToMaps(square [][]byte) (map[byte]koord, map[koord]byte) {
|
||||
eMap := make(map[byte]koord)
|
||||
dMap := make(map[koord]byte)
|
||||
for x, col := range square {
|
||||
for y, v := range col {
|
||||
eMap[v] = koord{byte(x), byte(y)}
|
||||
dMap[koord{byte(x), byte(y)}] = v
|
||||
|
||||
}
|
||||
}
|
||||
return eMap, dMap
|
||||
}
|
||||
|
||||
func removeSpaceI(text string) string {
|
||||
var n string
|
||||
s := strings.ToUpper(text)
|
||||
for _, b := range []byte(s) {
|
||||
//use only ASCII Characters from A to Z
|
||||
if b < 'A' || b > 'Z' {
|
||||
continue
|
||||
}
|
||||
if b == 'J' {
|
||||
b = 'I'
|
||||
}
|
||||
n = n + string(b)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func removeSpace(text string, square map[byte]koord) string {
|
||||
var n string
|
||||
//to UpperCase and then remove all Spaces an Characters witch are not in square
|
||||
s := strings.ReplaceAll(strings.ToUpper(text), " ", "")
|
||||
for _, b := range []byte(s) {
|
||||
_, ok := square[b]
|
||||
if ok {
|
||||
n = n + string(b)
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func encrypt(text string, emap map[byte]koord, dmap map[koord]byte) string {
|
||||
text = removeSpace(text, emap)
|
||||
var row0, row1 []byte
|
||||
for _, b := range []byte(text) {
|
||||
xy := emap[b]
|
||||
row0 = append(row0, xy.X)
|
||||
row1 = append(row1, xy.Y)
|
||||
}
|
||||
row0 = append(row0, row1...)
|
||||
|
||||
var s string
|
||||
for i := 0; i < len(row0); i += 2 {
|
||||
s = s + string(dmap[koord{row0[i], row0[i+1]}])
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func decrypt(text string, emap map[byte]koord, dmap map[koord]byte) string {
|
||||
text = removeSpace(text, emap)
|
||||
k := make([]koord, len(text))
|
||||
|
||||
for i, b := range []byte(text) {
|
||||
k[i] = emap[b]
|
||||
}
|
||||
|
||||
kl := make([]byte, len(k)*2)
|
||||
i := int(0)
|
||||
for _, ki := range k {
|
||||
kl[i] = ki.X
|
||||
kl[i+1] = ki.Y
|
||||
i += 2
|
||||
}
|
||||
l := len(kl) / 2
|
||||
k1 := kl[:l]
|
||||
k2 := kl[l:]
|
||||
var s string
|
||||
|
||||
for i := 0; i < l; i++ {
|
||||
s = s + string(dmap[koord{k1[i], k2[i]}])
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
encryptMap, decryptMap = squareToMaps(squareRosetta)
|
||||
fmt.Println("from Rosettacode")
|
||||
fmt.Println("original:\t", textRosetta)
|
||||
s := encrypt(textRosetta, encryptMap, decryptMap)
|
||||
fmt.Println("codiert:\t", s)
|
||||
s = decrypt(s, encryptMap, decryptMap)
|
||||
fmt.Println("and back:\t", s)
|
||||
|
||||
fmt.Println("from Wikipedia")
|
||||
encryptMap, decryptMap = squareToMaps(squareWikipedia)
|
||||
fmt.Println("original:\t", textWikipedia)
|
||||
s = encrypt(textWikipedia, encryptMap, decryptMap)
|
||||
fmt.Println("codiert:\t", s)
|
||||
s = decrypt(s, encryptMap, decryptMap)
|
||||
fmt.Println("and back:\t", s)
|
||||
|
||||
encryptMap, decryptMap = squareToMaps(squareWikipedia)
|
||||
fmt.Println("from Rosettacode long part")
|
||||
fmt.Println("original:\t", textTest)
|
||||
s = encrypt(textTest, encryptMap, decryptMap)
|
||||
fmt.Println("codiert:\t", s)
|
||||
// Wenn der Text eine ungerade Anzahl Buchstaben hat, funktioniert der Algorithmus nicht!!!
|
||||
s = decrypt(s, encryptMap, decryptMap)
|
||||
fmt.Println("and back:\t", s)
|
||||
|
||||
}
|
||||
60
Task/Bifid-cipher/Haskell/bifid-cipher.hs
Normal file
60
Task/Bifid-cipher/Haskell/bifid-cipher.hs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import Data.List
|
||||
import Data.Char
|
||||
|
||||
-- Defining all the grids I'll be using in one go.
|
||||
|
||||
rcBifid = [
|
||||
'A','B','C','D','E',
|
||||
'F','G','H','I','K',
|
||||
'L','M','N','O','P',
|
||||
'Q','R','S','T','U',
|
||||
'V','W','X','Y','Z'
|
||||
] -- We will use strings here onwards. This was just a demonstration.
|
||||
|
||||
wikiBifid = "POLYBIUSCHERADFGKMNQTVWXZ"
|
||||
cmiBifid = "CMIHASKELQWRTYUOPDFGZXVBN"
|
||||
|
||||
-- Convert a character to its grid coordinates (1-based index)
|
||||
chr2pair square x = if x == 'J' then chr2pair square 'I' else case elemIndex x square of
|
||||
Nothing -> error "char is not in cipher grid"
|
||||
Just a -> (\(x,y) -> (x+1,y+1)) (divMod a 5) -- Convert flat index to (row, col)
|
||||
|
||||
pair2chr square (x,y) = square !! ((x-1)*5+y-1)
|
||||
|
||||
-- Pairs up elements from a list, used in Bifid encoding process
|
||||
pairUp :: [a] -> [(a,a)]
|
||||
pairUp [] = []
|
||||
pairUp [a] = error "Odd number of elements"
|
||||
pairUp (x:y:ys) = (x,y):(pairUp ys)
|
||||
|
||||
encrypt square message = map (pair2chr square) (pairUp (l ++ r)) where
|
||||
(l,r) = unzip $ map (chr2pair square) message
|
||||
|
||||
decrypt square message = map (pair2chr square) (zip u d) where
|
||||
(u,d) = splitAt (length message) $ concatMap (\(x,y) -> [x,y]) $ map (chr2pair square) message
|
||||
|
||||
main = do
|
||||
let
|
||||
message1 = "ATTACKATDAWN"
|
||||
message2 = "FLEEATONCE"
|
||||
encrypt1 = encrypt rcBifid message1
|
||||
encrypt2 = encrypt wikiBifid message2
|
||||
encrypt3 = encrypt wikiBifid message1
|
||||
myMessage = "The invasion will start on the first of January"
|
||||
encrypt4 = encrypt cmiBifid $ map toUpper $ filter (/= ' ') myMessage -- Remove spaces, uppercase
|
||||
|
||||
putStrLn $ "Message 1: " ++ message1
|
||||
putStrLn $ "Encryption wrt RC's square: " ++ encrypt1
|
||||
putStrLn $ "Decrypt: " ++ show (decrypt rcBifid encrypt1)
|
||||
|
||||
putStrLn $ "Message 2: " ++ message2
|
||||
putStrLn $ "Encryption wrt Wiki's square: " ++ encrypt2
|
||||
putStrLn $ "Decrypt: " ++ show (decrypt wikiBifid encrypt2)
|
||||
|
||||
putStrLn $ "Message 3: " ++ message1
|
||||
putStrLn $ "Encryption wrt Wiki's square: " ++ encrypt3
|
||||
putStrLn $ "Decrypt: " ++ show (decrypt wikiBifid encrypt3)
|
||||
|
||||
putStrLn $ "Message 4: " ++ myMessage
|
||||
putStrLn $ "Encryption wrt CMI square: " ++ encrypt4
|
||||
putStrLn $ "Decrypt: " ++ show (decrypt cmiBifid encrypt4)
|
||||
68
Task/Bifid-cipher/M2000-Interpreter/bifid-cipher.m2000
Normal file
68
Task/Bifid-cipher/M2000-Interpreter/bifid-cipher.m2000
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
module Bifid_cipher (f, code$) {
|
||||
n=sqrt(len(code$))
|
||||
print #f," ";
|
||||
for i=1 to n
|
||||
print #f, i+" ";
|
||||
next
|
||||
print #f
|
||||
for i=0 to n-1
|
||||
Print #f, (i+1)+" "+STR$(mid$(code$,1+i*n, n),STRING$("@ ", 5))
|
||||
next
|
||||
tables=lambda (a$)->{
|
||||
n=sqrt(len(a$))
|
||||
a=list
|
||||
for i=1 to len(a$)
|
||||
append a, mid$(a$,i, 1):=((i-1) mod n+1, (i-1) div n+1 )
|
||||
next
|
||||
b=list
|
||||
m=each(a)
|
||||
while m
|
||||
z=eval(m)
|
||||
append b, z#val$(1)+z#val$(0):=eval$(m!)
|
||||
end while
|
||||
=a, b
|
||||
}(code$)
|
||||
encode= lambda (a, b)->{
|
||||
=lambda a,b (mess as string) -> {
|
||||
document code$
|
||||
for n=1 to 0
|
||||
for i=1 to len(mess)
|
||||
q=mid$(mess, i,1)
|
||||
if not exist(a, q) then
|
||||
if q="J" then q="I" else q="A"
|
||||
end if
|
||||
code$=a(q)#val$(n)
|
||||
next
|
||||
next
|
||||
document final$
|
||||
for i=1 to len(code$) step 2
|
||||
final$=b$(mid$(code$, i, 2))
|
||||
next
|
||||
= final$
|
||||
}
|
||||
}(!tables)
|
||||
decode= lambda (a, b)->{
|
||||
=lambda a,b (final as string) -> {
|
||||
document code$, mess$
|
||||
for i=1 to len(final)
|
||||
q=a(mid$(final, i, 1))
|
||||
code$=q#val$(1)+q#val$(0)
|
||||
next
|
||||
offset=len(code$) div 2
|
||||
for i=1 to offset
|
||||
mess$=b$(mid$(code$,i,1)+mid$(code$,i+offset,1))
|
||||
next
|
||||
= mess$
|
||||
}
|
||||
}(!tables)
|
||||
Print #f, encode("ATTACKATDAWN")
|
||||
Print #f, decode(encode("ATTACKATDAWN"))="ATTACKATDAWN"
|
||||
Print #f, encode(ucase$(filter$("The invasion will start on the first of January", " ")))
|
||||
Print #f, decode(encode(ucase$(filter$("The invasion will start on the first of January", " "))))
|
||||
}
|
||||
open "out.txt" for output as #a
|
||||
Bifid_cipher a, "ABCDEFGHIKLMNOPQRSTUVWXYZ"
|
||||
Bifid_cipher a, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
Bifid_cipher a, "BGWKZQPNDSIOAXEFCLUMTHYVR"
|
||||
close #a
|
||||
win "out.txt"
|
||||
Loading…
Add table
Add a link
Reference in a new issue