Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
104
Task/Playfair-cipher/Java/playfair-cipher-1.java
Normal file
104
Task/Playfair-cipher/Java/playfair-cipher-1.java
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
import java.awt.Point;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class PlayfairCipher {
|
||||
private static char[][] charTable;
|
||||
private static Point[] positions;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
String key = prompt("Enter an encryption key (min length 6): ", sc, 6);
|
||||
String txt = prompt("Enter the message: ", sc, 1);
|
||||
String jti = prompt("Replace J with I? y/n: ", sc, 1);
|
||||
|
||||
boolean changeJtoI = jti.equalsIgnoreCase("y");
|
||||
|
||||
createTable(key, changeJtoI);
|
||||
|
||||
String enc = encode(prepareText(txt, changeJtoI));
|
||||
|
||||
System.out.printf("%nEncoded message: %n%s%n", enc);
|
||||
System.out.printf("%nDecoded message: %n%s%n", decode(enc));
|
||||
}
|
||||
|
||||
private static String prompt(String promptText, Scanner sc, int minLen) {
|
||||
String s;
|
||||
do {
|
||||
System.out.print(promptText);
|
||||
s = sc.nextLine().trim();
|
||||
} while (s.length() < minLen);
|
||||
return s;
|
||||
}
|
||||
|
||||
private static String prepareText(String s, boolean changeJtoI) {
|
||||
s = s.toUpperCase().replaceAll("[^A-Z]", "");
|
||||
return changeJtoI ? s.replace("J", "I") : s.replace("Q", "");
|
||||
}
|
||||
|
||||
private static void createTable(String key, boolean changeJtoI) {
|
||||
charTable = new char[5][5];
|
||||
positions = new Point[26];
|
||||
|
||||
String s = prepareText(key + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", changeJtoI);
|
||||
|
||||
int len = s.length();
|
||||
for (int i = 0, k = 0; i < len; i++) {
|
||||
char c = s.charAt(i);
|
||||
if (positions[c - 'A'] == null) {
|
||||
charTable[k / 5][k % 5] = c;
|
||||
positions[c - 'A'] = new Point(k % 5, k / 5);
|
||||
k++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String encode(String s) {
|
||||
StringBuilder sb = new StringBuilder(s);
|
||||
|
||||
for (int i = 0; i < sb.length(); i += 2) {
|
||||
|
||||
if (i == sb.length() - 1)
|
||||
sb.append(sb.length() % 2 == 1 ? 'X' : "");
|
||||
|
||||
else if (sb.charAt(i) == sb.charAt(i + 1))
|
||||
sb.insert(i + 1, 'X');
|
||||
}
|
||||
return codec(sb, 1);
|
||||
}
|
||||
|
||||
private static String decode(String s) {
|
||||
return codec(new StringBuilder(s), 4);
|
||||
}
|
||||
|
||||
private static String codec(StringBuilder text, int direction) {
|
||||
int len = text.length();
|
||||
for (int i = 0; i < len; i += 2) {
|
||||
char a = text.charAt(i);
|
||||
char b = text.charAt(i + 1);
|
||||
|
||||
int row1 = positions[a - 'A'].y;
|
||||
int row2 = positions[b - 'A'].y;
|
||||
int col1 = positions[a - 'A'].x;
|
||||
int col2 = positions[b - 'A'].x;
|
||||
|
||||
if (row1 == row2) {
|
||||
col1 = (col1 + direction) % 5;
|
||||
col2 = (col2 + direction) % 5;
|
||||
|
||||
} else if (col1 == col2) {
|
||||
row1 = (row1 + direction) % 5;
|
||||
row2 = (row2 + direction) % 5;
|
||||
|
||||
} else {
|
||||
int tmp = col1;
|
||||
col1 = col2;
|
||||
col2 = tmp;
|
||||
}
|
||||
|
||||
text.setCharAt(i, charTable[row1][col1]);
|
||||
text.setCharAt(i + 1, charTable[row2][col2]);
|
||||
}
|
||||
return text.toString();
|
||||
}
|
||||
}
|
||||
205
Task/Playfair-cipher/Java/playfair-cipher-2.java
Normal file
205
Task/Playfair-cipher/Java/playfair-cipher-2.java
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
import java.util.Scanner;
|
||||
|
||||
public class PlayfairCipherEncryption
|
||||
{
|
||||
private String KeyWord = new String();
|
||||
private String Key = new String();
|
||||
private char matrix_arr[][] = new char[5][5];
|
||||
|
||||
public void setKey(String k)
|
||||
{
|
||||
String K_adjust = new String();
|
||||
boolean flag = false;
|
||||
K_adjust = K_adjust + k.charAt(0);
|
||||
for (int i = 1; i < k.length(); i++)
|
||||
{
|
||||
for (int j = 0; j < K_adjust.length(); j++)
|
||||
{
|
||||
if (k.charAt(i) == K_adjust.charAt(j))
|
||||
{
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
if (flag == false)
|
||||
K_adjust = K_adjust + k.charAt(i);
|
||||
flag = false;
|
||||
}
|
||||
KeyWord = K_adjust;
|
||||
}
|
||||
|
||||
public void KeyGen()
|
||||
{
|
||||
boolean flag = true;
|
||||
char current;
|
||||
Key = KeyWord;
|
||||
for (int i = 0; i < 26; i++)
|
||||
{
|
||||
current = (char) (i + 97);
|
||||
if (current == 'j')
|
||||
continue;
|
||||
for (int j = 0; j < KeyWord.length(); j++)
|
||||
{
|
||||
if (current == KeyWord.charAt(j))
|
||||
{
|
||||
flag = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag)
|
||||
Key = Key + current;
|
||||
flag = true;
|
||||
}
|
||||
System.out.println(Key);
|
||||
matrix();
|
||||
}
|
||||
|
||||
private void matrix()
|
||||
{
|
||||
int counter = 0;
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
for (int j = 0; j < 5; j++)
|
||||
{
|
||||
matrix_arr[i][j] = Key.charAt(counter);
|
||||
System.out.print(matrix_arr[i][j] + " ");
|
||||
counter++;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
private String format(String old_text)
|
||||
{
|
||||
int i = 0;
|
||||
int len = 0;
|
||||
String text = new String();
|
||||
len = old_text.length();
|
||||
for (int tmp = 0; tmp < len; tmp++)
|
||||
{
|
||||
if (old_text.charAt(tmp) == 'j')
|
||||
{
|
||||
text = text + 'i';
|
||||
}
|
||||
else
|
||||
text = text + old_text.charAt(tmp);
|
||||
}
|
||||
len = text.length();
|
||||
for (i = 0; i < len; i = i + 2)
|
||||
{
|
||||
if (text.charAt(i + 1) == text.charAt(i))
|
||||
{
|
||||
text = text.substring(0, i + 1) + 'x' + text.substring(i + 1);
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
private String[] Divid2Pairs(String new_string)
|
||||
{
|
||||
String Original = format(new_string);
|
||||
int size = Original.length();
|
||||
if (size % 2 != 0)
|
||||
{
|
||||
size++;
|
||||
Original = Original + 'x';
|
||||
}
|
||||
String x[] = new String[size / 2];
|
||||
int counter = 0;
|
||||
for (int i = 0; i < size / 2; i++)
|
||||
{
|
||||
x[i] = Original.substring(counter, counter + 2);
|
||||
counter = counter + 2;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
public int[] GetDiminsions(char letter)
|
||||
{
|
||||
int[] key = new int[2];
|
||||
if (letter == 'j')
|
||||
letter = 'i';
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
for (int j = 0; j < 5; j++)
|
||||
{
|
||||
if (matrix_arr[i][j] == letter)
|
||||
{
|
||||
key[0] = i;
|
||||
key[1] = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
public String encryptMessage(String Source)
|
||||
{
|
||||
String src_arr[] = Divid2Pairs(Source);
|
||||
String Code = new String();
|
||||
char one;
|
||||
char two;
|
||||
int part1[] = new int[2];
|
||||
int part2[] = new int[2];
|
||||
for (int i = 0; i < src_arr.length; i++)
|
||||
{
|
||||
one = src_arr[i].charAt(0);
|
||||
two = src_arr[i].charAt(1);
|
||||
part1 = GetDiminsions(one);
|
||||
part2 = GetDiminsions(two);
|
||||
if (part1[0] == part2[0])
|
||||
{
|
||||
if (part1[1] < 4)
|
||||
part1[1]++;
|
||||
else
|
||||
part1[1] = 0;
|
||||
if (part2[1] < 4)
|
||||
part2[1]++;
|
||||
else
|
||||
part2[1] = 0;
|
||||
}
|
||||
else if (part1[1] == part2[1])
|
||||
{
|
||||
if (part1[0] < 4)
|
||||
part1[0]++;
|
||||
else
|
||||
part1[0] = 0;
|
||||
if (part2[0] < 4)
|
||||
part2[0]++;
|
||||
else
|
||||
part2[0] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
int temp = part1[1];
|
||||
part1[1] = part2[1];
|
||||
part2[1] = temp;
|
||||
}
|
||||
Code = Code + matrix_arr[part1[0]][part1[1]]
|
||||
+ matrix_arr[part2[0]][part2[1]];
|
||||
}
|
||||
return Code;
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
PlayfairCipherEncryption x = new PlayfairCipherEncryption();
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Enter a keyword:");
|
||||
String keyword = sc.next();
|
||||
x.setKey(keyword);
|
||||
x.KeyGen();
|
||||
System.out
|
||||
.println("Enter word to encrypt: (Make sure length of message is even)");
|
||||
String key_input = sc.next();
|
||||
if (key_input.length() % 2 == 0)
|
||||
{
|
||||
System.out.println("Encryption: " + x.encryptMessage(key_input));
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("Message length should be even");
|
||||
}
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue