Bifid cipher: Difference between revisions
Content added Content deleted
(Promoted to 'full' task.) |
(New post.) |
||
Line 239: | Line 239: | ||
(_81{.123{.a.)difib(_81{.123{.a.)bifid 'The invasion will start on the first of January' |
(_81{.123{.a.)difib(_81{.123{.a.)bifid 'The invasion will start on the first of January' |
||
TheinvasionwillstartonthefirstofJanuary</syntaxhighlight> |
TheinvasionwillstartonthefirstofJanuary</syntaxhighlight> |
||
=={{header|Java}}== |
|||
<syntaxhighlight lang="java"> |
|||
import java.awt.Point; |
|||
import java.util.ArrayList; |
|||
import java.util.Arrays; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
public final class BifidCipher { |
|||
public static void main(String[] aArgs) { |
|||
final String message1 = "ATTACKATDAWN"; |
|||
final String message2 = "FLEEATONCE"; |
|||
final String message3 = "The invasion will start on the first of January".toUpperCase().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 aBifid, String aMessage) { |
|||
System.out.println("Using Polybius square:"); |
|||
aBifid.display(); |
|||
System.out.println("Message: " + aMessage); |
|||
String encrypted = aBifid.encrypt(aMessage); |
|||
System.out.println("Encrypted: " + encrypted); |
|||
String decrypted = aBifid.decrypt(encrypted); |
|||
System.out.println("Decrypted: " + decrypted); |
|||
System.out.println(); |
|||
} |
|||
} |
|||
final class Bifid { |
|||
public Bifid(int aN, String aText) { |
|||
if ( aText.length() != aN * aN ) { |
|||
throw new IllegalArgumentException("Incorrect length of text"); |
|||
} |
|||
grid = new char[aN][aN]; |
|||
int row = 0; |
|||
int col = 0; |
|||
for ( char ch : aText.toCharArray() ) { |
|||
grid[row][col] = ch; |
|||
coordinates.put(ch, new Point(row, col) ); |
|||
col += 1; |
|||
if ( col == aN ) { |
|||
col = 0; |
|||
row += 1; |
|||
} |
|||
} |
|||
if ( aN == 5 ) { |
|||
coordinates.put('J', coordinates.get('I')); |
|||
} |
|||
} |
|||
public void display() { |
|||
Arrays.stream(grid).forEach( row -> System.out.println(Arrays.toString(row)) ); |
|||
} |
|||
public String encrypt(String aText) { |
|||
List<Integer> rowOne = new ArrayList<Integer>(); |
|||
List<Integer> rowTwo = new ArrayList<Integer>(); |
|||
for ( char ch : aText.toCharArray() ) { |
|||
Point coordinate = coordinates.get(ch); |
|||
rowOne.add(coordinate.x); |
|||
rowTwo.add(coordinate.y); |
|||
} |
|||
rowOne.addAll(rowTwo); |
|||
StringBuilder result = new StringBuilder(); |
|||
for ( int i = 0; i < rowOne.size() - 1; i += 2 ) { |
|||
result.append(grid[rowOne.get(i)][rowOne.get(i + 1)]); |
|||
} |
|||
return result.toString(); |
|||
} |
|||
public String decrypt(String aText) { |
|||
List<Integer> row = new ArrayList<Integer>(); |
|||
for ( char ch : aText.toCharArray() ) { |
|||
Point coordinate = coordinates.get(ch); |
|||
row.add(coordinate.x); |
|||
row.add(coordinate.y); |
|||
} |
|||
final int middle = row.size() / 2; |
|||
List<Integer> rowOne = row.subList(0, middle); |
|||
List<Integer> rowTwo = row.subList(middle, row.size()); |
|||
StringBuilder result = new StringBuilder(); |
|||
for ( int i = 0; i < middle; i++ ) { |
|||
result.append(grid[rowOne.get(i)][rowTwo.get(i)]); |
|||
} |
|||
return result.toString(); |
|||
} |
|||
private char[][] grid; |
|||
private Map<Character, Point> coordinates = new HashMap<Character, Point>(); |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
Using Polybius square: |
|||
[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] |
|||
Message: ATTACKATDAWN |
|||
Encrypted: DQBDAXDQPDQH |
|||
Decrypted: ATTACKATDAWN |
|||
Using Polybius square: |
|||
[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] |
|||
Message: FLEEATONCE |
|||
Encrypted: UAEOLWRINS |
|||
Decrypted: FLEEATONCE |
|||
Using Polybius square: |
|||
[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] |
|||
Message: ATTACKATDAWN |
|||
Encrypted: EYFENGIWDILA |
|||
Decrypted: ATTACKATDAWN |
|||
Using Polybius square: |
|||
[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] |
|||
Message: FLEEATONCE |
|||
Encrypted: HADNAAZDSP |
|||
Decrypted: FLEEATONCE |
|||
Using Polybius square: |
|||
[A, B, C, D, E, F] |
|||
[G, H, I, J, K, L] |
|||
[M, N, O, P, Q, R] |
|||
[S, T, U, V, W, X] |
|||
[Y, Z, 0, 1, 2, 3] |
|||
[4, 5, 6, 7, 8, 9] |
|||
Message: THEINVASIONWILLSTARTONTHEFIRSTOFJANUARY |
|||
Encrypted: TBPDIPHJSPOTAIVMGPCZKNSCN09BFIHK64I7BM4 |
|||
Decrypted: THEINVASIONWILLSTARTONTHEFIRSTOFJANUARY |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Using the Raku example's test messages. |
Using the Raku example's test messages. |