Skip to content

Commit 2128c7a

Browse files
committed
Columnar Transposition Cipher with example usage
1 parent e44016c commit 2128c7a

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed
+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/**
2+
* Columnar Transposition Cipher Encryption and Decryption.
3+
* @author <a href="https://github.com/freitzzz">freitzzz</a>
4+
*/
5+
public class ColumnarTranspositionCipher {
6+
private static String keyword;
7+
private static Object[][] table;
8+
private static String abecedarium;
9+
public static final String ABECEDARIUM="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.;:-@";
10+
private static final String ENCRYPTION_FIELD="≈";
11+
private static final char ENCRYPTION_FIELD_CHAR='≈';
12+
/**
13+
* Encrypts a certain String with the Columnar Transposition Cipher Rule
14+
* @param word Word being encrypted
15+
* @param keyword String with keyword being used
16+
* @return a String with the word encrypted by the Columnar Transposition Cipher Rule
17+
*/
18+
public static String encrpyter(String word,String keyword){
19+
ColumnarTranspositionCipher.keyword=keyword;
20+
abecedariumBuilder(500);
21+
table=tableBuilder(word);
22+
Object[][] sortedTable=sortTable(table);
23+
String wordEncrypted="";
24+
for(int i=0;i<sortedTable[0].length;i++){
25+
for(int j=1;j<sortedTable.length;j++){
26+
wordEncrypted+=sortedTable[j][i];
27+
}
28+
}
29+
return wordEncrypted;
30+
}
31+
32+
/**
33+
* Encrypts a certain String with the Columnar Transposition Cipher Rule
34+
* @param word Word being encrypted
35+
* @param keyword String with keyword being used
36+
* @param abecedarium String with the abecedarium being used. null for default one
37+
* @return a String with the word encrypted by the Columnar Transposition Cipher Rule
38+
*/
39+
public static String encrpyter(String word,String keyword,String abecedarium){
40+
ColumnarTranspositionCipher.keyword=keyword;
41+
if(abecedarium!=null){
42+
ColumnarTranspositionCipher.abecedarium=abecedarium;
43+
}else{
44+
ColumnarTranspositionCipher.abecedarium=ABECEDARIUM;
45+
}
46+
table=tableBuilder(word);
47+
Object[][] sortedTable=sortTable(table);
48+
String wordEncrypted="";
49+
for(int i=0;i<sortedTable[0].length;i++){
50+
for(int j=1;j<sortedTable.length;j++){
51+
wordEncrypted+=sortedTable[j][i];
52+
}
53+
}
54+
return wordEncrypted;
55+
}
56+
/**
57+
* Decryps a certain encrypted String with the Columnar Transposition Cipher Rule
58+
* @return a String decrypted with the word ecrypted by the Columnar Transpositiion Cipher Rule
59+
*/
60+
public static String decrypter(){
61+
String wordDecrypted="";
62+
for(int i=1;i<table.length;i++){
63+
for(int j=0;j<table[i].length;j++){
64+
wordDecrypted+=table[i][j];
65+
}
66+
}
67+
return wordDecrypted.replaceAll(ENCRYPTION_FIELD,"");
68+
}
69+
/**
70+
* Builds a table with the word to be encrpyted in rows by the Columnar Transposition Cipher Rule
71+
* @return An Object[][] with the word to be encrypted filled in rows and columns
72+
*/
73+
private static Object[][] tableBuilder(String word){
74+
Object[][] table=new Object[rows(word)+1][keyword.length()];
75+
char[] wordInChards=word.toCharArray();
76+
//Fils in the respective numbers
77+
table[0]=findElements();
78+
int charElement=0;
79+
for(int i=1;i<table.length;i++){
80+
for(int j=0;j<table[i].length;j++){
81+
if(charElement<wordInChards.length){
82+
table[i][j]=(char)wordInChards[charElement];
83+
charElement++;
84+
}else{
85+
table[i][j]=ENCRYPTION_FIELD_CHAR;
86+
}
87+
}
88+
}
89+
return table;
90+
}
91+
/**
92+
* Determines the number of rows the table should have regarding the Columnar Transposition Cipher Rule
93+
* @return an int with the number of rows that the table should have in order to respect the Columnar Transposition Cipher Rule.
94+
*/
95+
private static int rows(String word){
96+
if((double)word.length()/keyword.length()>word.length()/keyword.length()){
97+
return (word.length()/keyword.length())+1;
98+
}else{
99+
return word.length()/keyword.length();
100+
}
101+
}
102+
private static Object[] findElements(){
103+
Object[] charValues=new Object[keyword.length()];
104+
for(int i=0;i<charValues.length;i++){
105+
for(int j=0;j<abecedarium.length();j++){
106+
if(keyword.charAt(i)==abecedarium.charAt(j))charValues[i]=j;
107+
}
108+
}
109+
return charValues;
110+
}
111+
private static Object[][] sortTable(Object[][] table){
112+
Object[][] tableSorted=new Object[table.length][table[0].length];
113+
for(int i=0;i<tableSorted.length;i++){
114+
for(int j=0;j<tableSorted[i].length;j++){
115+
tableSorted[i][j]=table[i][j];
116+
}
117+
}
118+
for(int i=0;i<tableSorted[0].length;i++){
119+
for(int j=i+1;j<tableSorted[0].length;j++){
120+
if((int)tableSorted[0][i]>(int)table[0][j]){
121+
Object[] column=getColumn(tableSorted,tableSorted.length,i);
122+
switchColumns(tableSorted,j,i,column);
123+
}
124+
}
125+
}
126+
return tableSorted;
127+
}
128+
private static Object[] getColumn(Object[][] table,int rows,int column){
129+
Object[] columnArray=new Object[rows];
130+
for(int i=0;i<rows;i++){
131+
columnArray[i]=table[i][column];
132+
}
133+
return columnArray;
134+
}
135+
private static void switchColumns(Object[][] table, int firstColumnIndex,int secondColumnIndex,Object[] columnToSwitch){
136+
for(int i=0;i<table.length;i++){
137+
table[i][secondColumnIndex]=table[i][firstColumnIndex];
138+
table[i][firstColumnIndex]=columnToSwitch[i];
139+
}
140+
}
141+
/**
142+
* Creates an abecedarium with a specified ascii inded
143+
* @param value Number of characters being used based on the ASCII Table
144+
*/
145+
private static void abecedariumBuilder(int value){
146+
abecedarium="";
147+
for(int i=0;i<value;i++){
148+
abecedarium+=(char)i;
149+
}
150+
}
151+
private static void showTable(){
152+
for(int i=0;i<table.length;i++){
153+
for(int j=0;j<table[i].length;j++){
154+
System.out.print(table[i][j]+" ");
155+
}
156+
System.out.println();
157+
}
158+
}
159+
public static void main(String[] args){
160+
String keywordForExample="asd215";
161+
String wordBeingEncrypted="This is a test of the Columnar Transposition Cipher";
162+
System.out.println("### Example of Columnar Transposition Cipher ###\n");
163+
System.out.println("Word being encryped ->>> "+wordBeingEncrypted);
164+
System.out.println("Word encrypted ->>> "+ColumnarTranspositionCipher.encrpyter(wordBeingEncrypted,keywordForExample));
165+
System.out.println("Word decryped ->>> "+ColumnarTranspositionCipher.decrypter());
166+
System.out.println("\n### Encrypted Table ###");
167+
showTable();
168+
}
169+
}

0 commit comments

Comments
 (0)