Data update
This commit is contained in:
parent
ed705008a8
commit
0df55f9f24
2196 changed files with 32999 additions and 3075 deletions
130
Task/Bifid-cipher/C-sharp/bifid-cipher.cs
Normal file
130
Task/Bifid-cipher/C-sharp/bifid-cipher.cs
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
|
||||
public class BifidCipher
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
string message1 = "ATTACKATDAWN";
|
||||
string message2 = "FLEEATONCE";
|
||||
string message3 = "The invasion will start on the first of January".ToUpper().Replace(" ", "");
|
||||
|
||||
Bifid bifid1 = new Bifid(5, "ABCDEFGHIKLMNOPQRSTUVWXYZ");
|
||||
Bifid bifid2 = new Bifid(5, "BGWKZQPNDSIOAXEFCLUMTHYVR");
|
||||
|
||||
RunTest(bifid1, message1);
|
||||
RunTest(bifid2, message2);
|
||||
RunTest(bifid2, message1);
|
||||
RunTest(bifid1, message2);
|
||||
|
||||
Bifid bifid3 = new Bifid(6, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
|
||||
RunTest(bifid3, message3);
|
||||
}
|
||||
|
||||
private static void RunTest(Bifid bifid, string message)
|
||||
{
|
||||
Console.WriteLine("Using Polybius square:");
|
||||
bifid.Display();
|
||||
Console.WriteLine("Message: " + message);
|
||||
string encrypted = bifid.Encrypt(message);
|
||||
Console.WriteLine("Encrypted: " + encrypted);
|
||||
string decrypted = bifid.Decrypt(encrypted);
|
||||
Console.WriteLine("Decrypted: " + decrypted);
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
public class Bifid
|
||||
{
|
||||
private char[,] grid;
|
||||
private Dictionary<char, Point> coordinates = new Dictionary<char, Point>();
|
||||
|
||||
public Bifid(int n, string text)
|
||||
{
|
||||
if (text.Length != n * n)
|
||||
{
|
||||
throw new ArgumentException("Incorrect length of text");
|
||||
}
|
||||
|
||||
grid = new char[n, n];
|
||||
int row = 0;
|
||||
int col = 0;
|
||||
|
||||
foreach (char ch in text)
|
||||
{
|
||||
grid[row, col] = ch;
|
||||
coordinates[ch] = new Point(row, col);
|
||||
col += 1;
|
||||
if (col == n)
|
||||
{
|
||||
col = 0;
|
||||
row += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (n == 5)
|
||||
{
|
||||
coordinates['J'] = coordinates['I'];
|
||||
}
|
||||
}
|
||||
|
||||
public string Encrypt(string text)
|
||||
{
|
||||
List<int> rowOne = new List<int>();
|
||||
List<int> rowTwo = new List<int>();
|
||||
|
||||
foreach (char ch in text)
|
||||
{
|
||||
Point coordinate = coordinates[ch];
|
||||
rowOne.Add(coordinate.X);
|
||||
rowTwo.Add(coordinate.Y);
|
||||
}
|
||||
|
||||
rowOne.AddRange(rowTwo);
|
||||
var result = new System.Text.StringBuilder();
|
||||
|
||||
for (int i = 0; i < rowOne.Count - 1; i += 2)
|
||||
{
|
||||
result.Append(grid[rowOne[i], rowOne[i + 1]]);
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
public string Decrypt(string text)
|
||||
{
|
||||
List<int> row = new List<int>();
|
||||
|
||||
foreach (char ch in text)
|
||||
{
|
||||
Point coordinate = coordinates[ch];
|
||||
row.Add(coordinate.X);
|
||||
row.Add(coordinate.Y);
|
||||
}
|
||||
|
||||
int middle = row.Count / 2;
|
||||
List<int> rowOne = row.GetRange(0, middle);
|
||||
List<int> rowTwo = row.GetRange(middle, row.Count - middle);
|
||||
var result = new System.Text.StringBuilder();
|
||||
|
||||
for (int i = 0; i < middle; i++)
|
||||
{
|
||||
result.Append(grid[rowOne[i], rowTwo[i]]);
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
public void Display()
|
||||
{
|
||||
for (int i = 0; i < grid.GetLength(0); i++)
|
||||
{
|
||||
for (int j = 0; j < grid.GetLength(1); j++)
|
||||
{
|
||||
Console.Write(grid[i, j] + " ");
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
62
Task/Bifid-cipher/FutureBasic/bifid-cipher.basic
Normal file
62
Task/Bifid-cipher/FutureBasic/bifid-cipher.basic
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
clear local fn recode( t as CFStringRef, code as CFStringRef ) as CFStringRef
|
||||
CFStringRef s = @""
|
||||
Short i, k, w = sqr( len( code ) )
|
||||
|
||||
for i = 0 to len( t ) - 1 step 2
|
||||
k = intval( mid( t, i, 2 ) ) // Get ‘coordinates’ of char in code string
|
||||
k = w * ( k / 10 ) + k mod 10
|
||||
s = fn StringByAppendingString( s, mid( code, k, 1 ) )
|
||||
next
|
||||
|
||||
end fn = s
|
||||
|
||||
//
|
||||
|
||||
clear local fn encode( s as CFStringRef, code as CFStringRef ) as CFStringRef
|
||||
CFStringRef a = @"", b = @"", c
|
||||
Short i, k, w = sqr( len( code ) )
|
||||
if w == 5 then s = fn StringByReplacingOccurrencesOfString( s, @"J", @"I" )
|
||||
print s
|
||||
|
||||
for i = 0 to len( s ) - 1
|
||||
c = mid( s, i, 1 )
|
||||
k = instr( 0, code, c ) // Put row in one string, column in the other
|
||||
a = fn StringByAppendingString( a, fn StringWithFormat( @"%d", k / w ) )
|
||||
b = fn StringByAppendingString( b, fn StringWithFormat( @"%d", k mod w ) )
|
||||
next
|
||||
|
||||
a = fn StringByAppendingString( a, b ) // Combine the two strings, and recode
|
||||
|
||||
end fn = fn recode( a, code )
|
||||
|
||||
//
|
||||
|
||||
clear local fn decode( s as CFStringRef, code as CFStringRef ) as CFStringRef
|
||||
CFStringRef a = @"", b = @"", c
|
||||
Short i, k, w = sqr( len( code ) )
|
||||
|
||||
for i = 0 to ( len( s ) - 1 )
|
||||
c = mid( s, i, 1 )
|
||||
k = instr( 0, code, c ) // Put row and columm in one long string
|
||||
a = fn StringByAppendingString( a, fn StringWithFormat( @"%d%d", k / w, k mod w ) )
|
||||
next
|
||||
|
||||
for i = 0 to len( a ) / 2 - 1 // Take row from first half of string, column from second
|
||||
c = fn StringByAppendingString( mid( a, i, 1 ), mid( a, i + len( a ) / 2 , 1 ) )
|
||||
b = fn StringByAppendingString( b , c ) // Combine, and recode
|
||||
next
|
||||
|
||||
end fn = fn recode( b, code )
|
||||
|
||||
//
|
||||
|
||||
print fn encode( @"ATTACKATDAWN", @"ABCDEFGHIKLMNOPQRSTUVWXYZ" )
|
||||
print fn decode( @"DQBDAXDQPDQH", @"ABCDEFGHIKLMNOPQRSTUVWXYZ" )
|
||||
print
|
||||
print fn encode( @"FLEEATONCE", @"BGWKZQPDNSIOAXEFCLUMTHYVR" )
|
||||
print fn decode( @"UAEOLWRINS", @"BGWKZQPDNSIOAXEFCLUMTHYVR" )
|
||||
print
|
||||
print fn encode( @"HAPPY40THDAD", @"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )
|
||||
print fn decode( @"GO31GAGVANJD", @"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )
|
||||
|
||||
handleevents
|
||||
Loading…
Add table
Add a link
Reference in a new issue