Skip to content

Commit 520ee69

Browse files
author
khalil2535
committed
Update chiphers
1 parent ca8f6db commit 520ee69

File tree

2 files changed

+178
-112
lines changed

2 files changed

+178
-112
lines changed

ciphers/ColumnarTranspositionCipher.java

+123-93
Original file line numberDiff line numberDiff line change
@@ -1,168 +1,198 @@
1-
/**
1+
package ciphers;
2+
3+
/**
24
* Columnar Transposition Cipher Encryption and Decryption.
5+
*
36
* @author <a href="https://github.com/freitzzz">freitzzz</a>
47
*/
58
public class ColumnarTranspositionCipher {
9+
610
private static String keyword;
711
private static Object[][] table;
812
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='≈';
13+
public static final String ABECEDARIUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.;:-@";
14+
private static final String ENCRYPTION_FIELD = "≈";
15+
private static final char ENCRYPTION_FIELD_CHAR = '≈';
16+
1217
/**
1318
* Encrypts a certain String with the Columnar Transposition Cipher Rule
19+
*
1420
* @param word Word being encrypted
1521
* @param keyword String with keyword being used
16-
* @return a String with the word encrypted by the Columnar Transposition Cipher Rule
22+
* @return a String with the word encrypted by the Columnar Transposition
23+
* Cipher Rule
1724
*/
18-
public static String encrpyter(String word,String keyword){
19-
ColumnarTranspositionCipher.keyword=keyword;
25+
public static String encrpyter(String word, String keyword) {
26+
ColumnarTranspositionCipher.keyword = keyword;
2027
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];
28+
table = tableBuilder(word);
29+
Object[][] sortedTable = sortTable(table);
30+
String wordEncrypted = "";
31+
for (int i = 0; i < sortedTable[i].length; i++) {
32+
for (int j = 1; j < sortedTable.length; j++) {
33+
wordEncrypted += sortedTable[j][i];
2734
}
2835
}
2936
return wordEncrypted;
3037
}
3138

3239
/**
3340
* Encrypts a certain String with the Columnar Transposition Cipher Rule
41+
*
3442
* @param word Word being encrypted
3543
* @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
44+
* @param abecedarium String with the abecedarium being used. null for
45+
* default one
46+
* @return a String with the word encrypted by the Columnar Transposition
47+
* Cipher Rule
3848
*/
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;
49+
public static String encrpyter(String word, String keyword, String abecedarium) {
50+
ColumnarTranspositionCipher.keyword = keyword;
51+
if (abecedarium != null) {
52+
ColumnarTranspositionCipher.abecedarium = abecedarium;
53+
} else {
54+
ColumnarTranspositionCipher.abecedarium = ABECEDARIUM;
4555
}
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];
56+
table = tableBuilder(word);
57+
Object[][] sortedTable = sortTable(table);
58+
String wordEncrypted = "";
59+
for (int i = 0; i < sortedTable[0].length; i++) {
60+
for (int j = 1; j < sortedTable.length; j++) {
61+
wordEncrypted += sortedTable[j][i];
5262
}
5363
}
5464
return wordEncrypted;
5565
}
66+
5667
/**
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
68+
* Decrypts a certain encrypted String with the Columnar Transposition
69+
* Cipher Rule
70+
*
71+
* @return a String decrypted with the word encrypted by the Columnar
72+
* Transposition Cipher Rule
5973
*/
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];
74+
public static String decrypter() {
75+
String wordDecrypted = "";
76+
for (int i = 1; i < table.length; i++) {
77+
for (Object item : table[i]) {
78+
wordDecrypted += item;
6579
}
6680
}
67-
return wordDecrypted.replaceAll(ENCRYPTION_FIELD,"");
81+
return wordDecrypted.replaceAll(ENCRYPTION_FIELD, "");
6882
}
83+
6984
/**
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
85+
* Builds a table with the word to be encrypted in rows by the Columnar
86+
* Transposition Cipher Rule
87+
*
88+
* @return An Object[][] with the word to be encrypted filled in rows and
89+
* columns
7290
*/
73-
private static Object[][] tableBuilder(String word){
74-
Object[][] table=new Object[rows(word)+1][keyword.length()];
75-
char[] wordInChards=word.toCharArray();
91+
private static Object[][] tableBuilder(String word) {
92+
Object[][] table = new Object[numberOfRows(word) + 1][keyword.length()];
93+
char[] wordInChards = word.toCharArray();
7694
//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];
95+
table[0] = findElements();
96+
int charElement = 0;
97+
for (int i = 1; i < table.length; i++) {
98+
for (int j = 0; j < table[i].length; j++) {
99+
if (charElement < wordInChards.length) {
100+
table[i][j] = wordInChards[charElement];
83101
charElement++;
84-
}else{
85-
table[i][j]=ENCRYPTION_FIELD_CHAR;
102+
} else {
103+
table[i][j] = ENCRYPTION_FIELD_CHAR;
86104
}
87105
}
88106
}
89107
return table;
90108
}
109+
91110
/**
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.
111+
* Determines the number of rows the table should have regarding the
112+
* Columnar Transposition Cipher Rule
113+
*
114+
* @return an int with the number of rows that the table should have in
115+
* order to respect the Columnar Transposition Cipher Rule.
94116
*/
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();
117+
private static int numberOfRows(String word) {
118+
if ((double) word.length() / keyword.length() > word.length() / keyword.length()) {
119+
return (word.length() / keyword.length()) + 1;
120+
} else {
121+
return word.length() / keyword.length();
100122
}
101123
}
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;
124+
125+
private static Object[] findElements() {
126+
Object[] charValues = new Object[keyword.length()];
127+
for (int i = 0; i < charValues.length; i++) {
128+
for (int j = 0; j < abecedarium.length(); j++) {
129+
if (keyword.charAt(i) == abecedarium.charAt(j)) {
130+
charValues[i] = j;
131+
}
107132
}
108133
}
109134
return charValues;
110135
}
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-
}
136+
137+
private static Object[][] sortTable(Object[][] table) {
138+
Object[][] tableSorted = new Object[table.length][table[0].length];
139+
for (int i = 0; i < tableSorted.length; i++) {
140+
System.arraycopy(table[i], 0, tableSorted[i], 0, tableSorted[i].length);
117141
}
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);
142+
for (int i = 0; i < tableSorted[0].length; i++) {
143+
for (int j = i + 1; j < tableSorted[0].length; j++) {
144+
if ((int) tableSorted[0][i] > (int) table[0][j]) {
145+
Object[] column = getColumn(tableSorted, tableSorted.length, i);
146+
switchColumns(tableSorted, j, i, column);
123147
}
124148
}
125149
}
126150
return tableSorted;
127151
}
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];
152+
153+
private static Object[] getColumn(Object[][] table, int rows, int column) {
154+
Object[] columnArray = new Object[rows];
155+
for (int i = 0; i < rows; i++) {
156+
columnArray[i] = table[i][column];
132157
}
133158
return columnArray;
134159
}
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];
160+
161+
private static void switchColumns(Object[][] table, int firstColumnIndex, int secondColumnIndex, Object[] columnToSwitch) {
162+
for (int i = 0; i < table.length; i++) {
163+
table[i][secondColumnIndex] = table[i][firstColumnIndex];
164+
table[i][firstColumnIndex] = columnToSwitch[i];
139165
}
140166
}
167+
141168
/**
142169
* Creates an abecedarium with a specified ascii inded
170+
*
143171
* @param value Number of characters being used based on the ASCII Table
144172
*/
145-
private static void abecedariumBuilder(int value){
146-
abecedarium="";
147-
for(int i=0;i<value;i++){
148-
abecedarium+=(char)i;
173+
private static void abecedariumBuilder(int value) {
174+
abecedarium = "";
175+
for (int i = 0; i < value; i++) {
176+
abecedarium += (char) i;
149177
}
150178
}
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]+" ");
179+
180+
private static void showTable() {
181+
for (Object[] table1 : table) {
182+
for (Object item : table1) {
183+
System.out.print(item + " ");
155184
}
156185
System.out.println();
157186
}
158187
}
159-
public static void main(String[] args){
160-
String keywordForExample="asd215";
161-
String wordBeingEncrypted="This is a test of the Columnar Transposition Cipher";
188+
189+
public static void main(String[] args) {
190+
String keywordForExample = "asd215";
191+
String wordBeingEncrypted = "This is a test of the Columnar Transposition Cipher";
162192
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());
193+
System.out.println("Word being encryped ->>> " + wordBeingEncrypted);
194+
System.out.println("Word encrypted ->>> " + ColumnarTranspositionCipher.encrpyter(wordBeingEncrypted, keywordForExample));
195+
System.out.println("Word decryped ->>> " + ColumnarTranspositionCipher.decrypter());
166196
System.out.println("\n### Encrypted Table ###");
167197
showTable();
168198
}

ciphers/RSA.java

+55-19
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,82 @@
22

33
import java.math.BigInteger;
44
import java.security.SecureRandom;
5+
import javax.swing.JOptionPane;
56

67
/**
7-
* Created by Nguyen Duy Tiep on 23-Oct-17.
8+
* @author Nguyen Duy Tiep on 23-Oct-17.
89
*/
9-
public class RSA {
10+
public final class RSA {
11+
12+
/**
13+
* Trivial test program.
14+
*
15+
* @param args
16+
* @deprecated TODO remove main and make JUnit Testing or any other
17+
* methodology
18+
*/
19+
public static void main(String[] args) {
20+
21+
RSA rsa = new RSA(1024);
22+
String text1 = JOptionPane.showInputDialog("Enter a message to encrypt :");
23+
24+
String ciphertext = rsa.encrypt(text1);
25+
JOptionPane.showMessageDialog(null, "Your encrypted message : " + ciphertext);
26+
27+
JOptionPane.showMessageDialog(null, "Your message after decrypt : " + rsa.decrypt(ciphertext));
28+
}
29+
1030
private BigInteger modulus, privateKey, publicKey;
1131

32+
/**
33+
*
34+
* @param bits
35+
*/
1236
public RSA(int bits) {
1337
generateKeys(bits);
1438
}
1539

40+
/**
41+
*
42+
* @param message
43+
* @return encrypted message
44+
*/
1645
public synchronized String encrypt(String message) {
1746
return (new BigInteger(message.getBytes())).modPow(publicKey, modulus).toString();
1847
}
1948

49+
/**
50+
*
51+
* @param message
52+
* @return encrypted message as big integer
53+
*/
2054
public synchronized BigInteger encrypt(BigInteger message) {
2155
return message.modPow(publicKey, modulus);
2256
}
2357

24-
public synchronized String decrypt(String message) {
25-
return new String((new BigInteger(message)).modPow(privateKey, modulus).toByteArray());
58+
/**
59+
*
60+
* @param encryptedMessage
61+
* @return plain message
62+
*/
63+
public synchronized String decrypt(String encryptedMessage) {
64+
return new String((new BigInteger(encryptedMessage)).modPow(privateKey, modulus).toByteArray());
2665
}
2766

28-
public synchronized BigInteger decrypt(BigInteger message) {
29-
return message.modPow(privateKey, modulus);
67+
/**
68+
*
69+
* @param encryptedMessage
70+
* @return plain message as big integer
71+
*/
72+
public synchronized BigInteger decrypt(BigInteger encryptedMessage) {
73+
return encryptedMessage.modPow(privateKey, modulus);
3074
}
3175

32-
/** Generate a new public and private key set. */
76+
/**
77+
* Generate a new public and private key set.
78+
*
79+
* @param bits
80+
*/
3381
public synchronized void generateKeys(int bits) {
3482
SecureRandom r = new SecureRandom();
3583
BigInteger p = new BigInteger(bits / 2, 100, r);
@@ -47,16 +95,4 @@ public synchronized void generateKeys(int bits) {
4795
privateKey = publicKey.modInverse(m);
4896
}
4997

50-
/** Trivial test program. */
51-
public static void main(String[] args) {
52-
RSA rsa = new RSA(1024);
53-
54-
String text1 = "This is a message";
55-
System.out.println("Plaintext: " + text1);
56-
57-
String ciphertext = rsa.encrypt(text1);
58-
System.out.println("Ciphertext: " + ciphertext);
59-
60-
System.out.println("Plaintext: " + rsa.decrypt(ciphertext));
61-
}
6298
}

0 commit comments

Comments
 (0)